3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Model\Contact;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\System;
28 use Friendica\Database\Database;
29 use Friendica\Database\DBA;
30 use Friendica\Database\DBStructure;
31 use Friendica\Model\Contact;
32 use Friendica\Model\ItemURI;
36 * This class provides information about user related contacts based on the "user-contact" table.
41 * Insert a user-contact for a given contact array
43 * @param array $contact
46 public static function insertForContactArray(array $contact)
48 if (empty($contact['uid'])) {
49 // We don't create entries for the public user - by now
53 if (empty($contact['uri-id']) && empty($contact['url'])) {
54 Logger::info('Missing contact details', ['contact' => $contact, 'callstack' => System::callstack(20)]);
58 if (empty($contact['uri-id'])) {
59 $contact['uri-id'] = ItemURI::getIdByURI($contact['url']);
62 $pcontact = Contact::selectFirst(['id'], ['uri-id' => $contact['uri-id'], 'uid' => 0]);
63 if (!empty($contact['uri-id']) && DBA::isResult($pcontact)) {
64 $pcid = $pcontact['id'];
65 } elseif (empty($contact['url']) || !($pcid = Contact::getIdForURL($contact['url'], 0, false))) {
66 Logger::info('Public contact for user not found', ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid']]);
70 $fields = self::preparedFields($contact);
71 $fields['cid'] = $pcid;
72 $fields['uid'] = $contact['uid'];
73 $fields['uri-id'] = $contact['uri-id'];
75 $ret = DBA::insert('user-contact', $fields, Database::INSERT_UPDATE);
77 Logger::info('Inserted user contact', ['uid' => $contact['uid'], 'cid' => $pcid, 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
83 * Apply changes from contact update data to user-contact table
85 * @param array $fields
86 * @param array $condition
88 * @throws PDOException
91 public static function updateByContactUpdate(array $fields, array $condition)
95 $update_fields = self::preparedFields($fields);
96 if (!empty($update_fields)) {
97 $contacts = DBA::select('contact', ['uri-id', 'uid'], $condition);
98 while ($contact = DBA::fetch($contacts)) {
99 if (empty($contact['uri-id']) || empty($contact['uid'])) {
102 $ret = DBA::update('user-contact', $update_fields, ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid']]);
103 Logger::info('Updated user contact', ['uid' => $contact['uid'], 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
106 DBA::close($contacts);
113 * Prepare field data for update/insert
115 * @param array $fields
116 * @return array prepared fields
118 private static function preparedFields(array $fields): array
120 unset($fields['uid']);
121 unset($fields['cid']);
122 unset($fields['uri-id']);
124 if (isset($fields['readonly'])) {
125 $fields['ignored'] = $fields['readonly'];
128 if (!empty($fields['self'])) {
129 $fields['rel'] = Contact::SELF;
132 return DBStructure::getFieldsForTable('user-contact', $fields);
136 * Block contact id for user id
138 * @param int $cid Either public contact id or user's contact id
139 * @param int $uid User ID
140 * @param boolean $blocked Is the contact blocked or unblocked?
143 public static function setBlocked($cid, $uid, $blocked)
145 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
150 $contact = Contact::getById($cdata['public']);
152 Protocol::block($contact, $uid);
154 Protocol::unblock($contact, $uid);
157 if ($cdata['user'] != 0) {
158 DBA::update('contact', ['blocked' => $blocked], ['id' => $cdata['user'], 'pending' => false]);
161 DBA::update('user-contact', ['blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
165 * Returns "block" state for contact id and user id
167 * @param int $cid Either public contact id or user's contact id
168 * @param int $uid User ID
170 * @return boolean is the contact id blocked for the given user?
173 public static function isBlocked($cid, $uid)
175 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
180 $public_blocked = false;
182 if (!empty($cdata['public'])) {
183 $public_contact = DBA::selectFirst('user-contact', ['blocked'], ['cid' => $cdata['public'], 'uid' => $uid]);
184 if (DBA::isResult($public_contact)) {
185 $public_blocked = $public_contact['blocked'];
189 $user_blocked = $public_blocked;
191 if (!empty($cdata['user'])) {
192 $user_contact = DBA::selectFirst('contact', ['blocked'], ['id' => $cdata['user'], 'pending' => false]);
193 if (DBA::isResult($user_contact)) {
194 $user_blocked = $user_contact['blocked'];
198 if ($user_blocked != $public_blocked) {
199 DBA::update('user-contact', ['blocked' => $user_blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
202 return $user_blocked;
206 * Ignore contact id for user id
208 * @param int $cid Either public contact id or user's contact id
209 * @param int $uid User ID
210 * @param boolean $ignored Is the contact ignored or unignored?
213 public static function setIgnored($cid, $uid, $ignored)
215 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
220 if ($cdata['user'] != 0) {
221 DBA::update('contact', ['readonly' => $ignored], ['id' => $cdata['user'], 'pending' => false]);
224 DBA::update('user-contact', ['ignored' => $ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
228 * Returns "ignore" state for contact id and user id
230 * @param int $cid Either public contact id or user's contact id
231 * @param int $uid User ID
233 * @return boolean is the contact id ignored for the given user?
236 public static function isIgnored($cid, $uid)
238 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
243 $public_ignored = false;
245 if (!empty($cdata['public'])) {
246 $public_contact = DBA::selectFirst('user-contact', ['ignored'], ['cid' => $cdata['public'], 'uid' => $uid]);
247 if (DBA::isResult($public_contact)) {
248 $public_ignored = $public_contact['ignored'];
252 $user_ignored = $public_ignored;
254 if (!empty($cdata['user'])) {
255 $user_contact = DBA::selectFirst('contact', ['readonly'], ['id' => $cdata['user'], 'pending' => false]);
256 if (DBA::isResult($user_contact)) {
257 $user_ignored = $user_contact['readonly'];
261 if ($user_ignored != $public_ignored) {
262 DBA::update('user-contact', ['ignored' => $user_ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
265 return $user_ignored;
269 * Set "collapsed" for contact id and user id
271 * @param int $cid Either public contact id or user's contact id
272 * @param int $uid User ID
273 * @param boolean $collapsed are the contact's posts collapsed or uncollapsed?
276 public static function setCollapsed($cid, $uid, $collapsed)
278 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
283 DBA::update('user-contact', ['collapsed' => $collapsed], ['cid' => $cdata['public'], 'uid' => $uid], true);
287 * Returns "collapsed" state for contact id and user id
289 * @param int $cid Either public contact id or user's contact id
290 * @param int $uid User ID
292 * @return boolean is the contact id blocked for the given user?
293 * @throws HTTPException\InternalServerErrorException
294 * @throws \ImagickException
296 public static function isCollapsed($cid, $uid)
298 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
305 if (!empty($cdata['public'])) {
306 $public_contact = DBA::selectFirst('user-contact', ['collapsed'], ['cid' => $cdata['public'], 'uid' => $uid]);
307 if (DBA::isResult($public_contact)) {
308 $collapsed = $public_contact['collapsed'];