3 * @copyright Copyright (C) 2010-2022, 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;
32 use Friendica\Model\Contact;
33 use Friendica\Model\ItemURI;
37 * This class provides information about user related contacts based on the "user-contact" table.
42 * Insert a user-contact for a given contact array
44 * @param array $contact
47 public static function insertForContactArray(array $contact)
49 if (empty($contact['uid'])) {
50 // We don't create entries for the public user - by now
54 if (empty($contact['uri-id']) && empty($contact['url'])) {
55 Logger::info('Missing contact details', ['contact' => $contact, 'callstack' => System::callstack(20)]);
59 if (empty($contact['uri-id'])) {
60 $contact['uri-id'] = ItemURI::getIdByURI($contact['url']);
63 $pcontact = Contact::selectFirst(['id'], ['uri-id' => $contact['uri-id'], 'uid' => 0]);
64 if (!empty($contact['uri-id']) && DBA::isResult($pcontact)) {
65 $pcid = $pcontact['id'];
66 } elseif (empty($contact['url']) || !($pcid = Contact::getIdForURL($contact['url'], 0, false))) {
67 Logger::info('Public contact for user not found', ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid']]);
71 $fields = self::preparedFields($contact);
72 $fields['cid'] = $pcid;
73 $fields['uid'] = $contact['uid'];
74 $fields['uri-id'] = $contact['uri-id'];
76 $ret = DBA::insert('user-contact', $fields, Database::INSERT_UPDATE);
78 Logger::info('Inserted user contact', ['uid' => $contact['uid'], 'cid' => $pcid, 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
84 * Apply changes from contact update data to user-contact table
86 * @param array $fields
87 * @param array $condition
89 * @throws PDOException
92 public static function updateByContactUpdate(array $fields, array $condition)
96 $update_fields = self::preparedFields($fields);
97 if (!empty($update_fields)) {
98 $contacts = DBA::select('contact', ['uri-id', 'uid'], $condition);
99 while ($contact = DBA::fetch($contacts)) {
100 if (empty($contact['uri-id']) || empty($contact['uid'])) {
103 $ret = DBA::update('user-contact', $update_fields, ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid']]);
104 Logger::info('Updated user contact', ['uid' => $contact['uid'], 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
107 DBA::close($contacts);
114 * Prepare field data for update/insert
116 * @param array $fields
117 * @return array prepared fields
119 private static function preparedFields(array $fields): array
121 unset($fields['uid']);
122 unset($fields['cid']);
123 unset($fields['uri-id']);
125 if (isset($fields['readonly'])) {
126 $fields['ignored'] = $fields['readonly'];
129 if (!empty($fields['self'])) {
130 $fields['rel'] = Contact::SELF;
133 return DI::dbaDefinition()->truncateFieldsForTable('user-contact', $fields);
137 * Block contact id for user id
139 * @param int $cid Either public contact id or user's contact id
140 * @param int $uid User ID
141 * @param boolean $blocked Is the contact blocked or unblocked?
145 public static function setBlocked(int $cid, int $uid, bool $blocked)
147 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
152 $contact = Contact::getById($cdata['public']);
154 Protocol::block($contact, $uid);
156 Protocol::unblock($contact, $uid);
159 if ($cdata['user'] != 0) {
160 DBA::update('contact', ['blocked' => $blocked], ['id' => $cdata['user'], 'pending' => false]);
163 DBA::update('user-contact', ['blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
167 * Returns "block" state for contact id and user id
169 * @param int $cid Either public contact id or user's contact id
170 * @param int $uid User ID
172 * @return boolean is the contact id blocked for the given user?
175 public static function isBlocked(int $cid, int $uid): bool
177 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
182 $public_blocked = false;
184 if (!empty($cdata['public'])) {
185 $public_contact = DBA::selectFirst('user-contact', ['blocked'], ['cid' => $cdata['public'], 'uid' => $uid]);
186 if (DBA::isResult($public_contact)) {
187 $public_blocked = (bool) $public_contact['blocked'];
191 $user_blocked = $public_blocked;
193 if (!empty($cdata['user'])) {
194 $user_contact = DBA::selectFirst('contact', ['blocked'], ['id' => $cdata['user'], 'pending' => false]);
195 if (DBA::isResult($user_contact)) {
196 $user_blocked = (bool) $user_contact['blocked'];
200 if ($user_blocked != $public_blocked) {
201 DBA::update('user-contact', ['blocked' => $user_blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
204 return $user_blocked;
208 * Ignore contact id for user id
210 * @param int $cid Either public contact id or user's contact id
211 * @param int $uid User ID
212 * @param boolean $ignored Is the contact ignored or unignored?
216 public static function setIgnored(int $cid, int $uid, bool $ignored)
218 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
223 if ($cdata['user'] != 0) {
224 DBA::update('contact', ['readonly' => $ignored], ['id' => $cdata['user'], 'pending' => false]);
227 DBA::update('user-contact', ['ignored' => $ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
231 * Returns "ignore" state for contact id and user id
233 * @param int $cid Either public contact id or user's contact id
234 * @param int $uid User ID
235 * @return boolean is the contact id ignored for the given user?
238 public static function isIgnored(int $cid, int $uid): bool
240 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
245 $public_ignored = false;
247 if (!empty($cdata['public'])) {
248 $public_contact = DBA::selectFirst('user-contact', ['ignored'], ['cid' => $cdata['public'], 'uid' => $uid]);
249 if (DBA::isResult($public_contact)) {
250 $public_ignored = (bool) $public_contact['ignored'];
254 $user_ignored = $public_ignored;
256 if (!empty($cdata['user'])) {
257 $user_contact = DBA::selectFirst('contact', ['readonly'], ['id' => $cdata['user'], 'pending' => false]);
258 if (DBA::isResult($user_contact)) {
259 $user_ignored = (bool) $user_contact['readonly'];
263 if ($user_ignored != $public_ignored) {
264 DBA::update('user-contact', ['ignored' => $user_ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
267 return $user_ignored;
271 * Set "collapsed" for contact id and user id
273 * @param int $cid Either public contact id or user's contact id
274 * @param int $uid User ID
275 * @param boolean $collapsed are the contact's posts collapsed or uncollapsed?
279 public static function setCollapsed(int $cid, int $uid, bool $collapsed)
281 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
286 DBA::update('user-contact', ['collapsed' => $collapsed], ['cid' => $cdata['public'], 'uid' => $uid], true);
290 * Returns "collapsed" state for contact id and user id
292 * @param int $cid Either public contact id or user's contact id
293 * @param int $uid User ID
294 * @return boolean is the contact id blocked for the given user?
295 * @throws HTTPException\InternalServerErrorException
296 * @throws \ImagickException
298 public static function isCollapsed(int $cid, int $uid): bool
300 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
307 if (!empty($cdata['public'])) {
308 $public_contact = DBA::selectFirst('user-contact', ['collapsed'], ['cid' => $cdata['public'], 'uid' => $uid]);
309 if (DBA::isResult($public_contact)) {
310 $collapsed = (bool) $public_contact['collapsed'];
318 * Set/Release that the user is blocked by the contact
320 * @param int $cid Either public contact id or user's contact id
321 * @param int $uid User ID
322 * @param boolean $blocked Is the user blocked or unblocked by the contact?
326 public static function setIsBlocked(int $cid, int $uid, bool $blocked)
328 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
333 DBA::update('user-contact', ['is-blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
337 * Returns if the user is blocked by the contact
339 * @param int $cid Either public contact id or user's contact id
340 * @param int $uid User ID
341 * @return boolean Is the user blocked or unblocked by the contact?
344 public static function isIsBlocked(int $cid, int $uid): bool
346 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
351 if (!empty($cdata['public'])) {
352 $public_contact = DBA::selectFirst('user-contact', ['is-blocked'], ['cid' => $cdata['public'], 'uid' => $uid]);
353 if (DBA::isResult($public_contact)) {
354 return $public_contact['is-blocked'];