3 * @copyright Copyright (C) 2010-2023, 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;
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.
40 const FREQUENCY_DEFAULT = 0;
41 const FREQUENCY_NEVER = 1;
42 const FREQUENCY_ALWAYS = 2;
43 const FREQUENCY_REDUCED = 3;
45 * Insert a user-contact for a given contact array
47 * @param array $contact
50 public static function insertForContactArray(array $contact)
52 if (empty($contact['uid'])) {
53 // We don't create entries for the public user - by now
57 if (empty($contact['uri-id']) && empty($contact['url'])) {
58 Logger::info('Missing contact details', ['contact' => $contact, 'callstack' => System::callstack(20)]);
62 if (empty($contact['uri-id'])) {
63 $contact['uri-id'] = ItemURI::getIdByURI($contact['url']);
66 $pcontact = Contact::selectFirst(['id'], ['uri-id' => $contact['uri-id'], 'uid' => 0]);
67 if (!empty($contact['uri-id']) && DBA::isResult($pcontact)) {
68 $pcid = $pcontact['id'];
69 } elseif (empty($contact['url']) || !($pcid = Contact::getIdForURL($contact['url'], 0, false))) {
70 Logger::info('Public contact for user not found', ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid']]);
74 $fields = self::preparedFields($contact);
75 $fields['cid'] = $pcid;
76 $fields['uid'] = $contact['uid'];
77 $fields['uri-id'] = $contact['uri-id'];
79 $ret = DBA::insert('user-contact', $fields, Database::INSERT_UPDATE);
81 Logger::info('Inserted user contact', ['uid' => $contact['uid'], 'cid' => $pcid, 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
87 * Apply changes from contact update data to user-contact table
89 * @param array $fields
90 * @param array $condition
92 * @throws PDOException
95 public static function updateByContactUpdate(array $fields, array $condition)
99 $update_fields = self::preparedFields($fields);
100 if (!empty($update_fields)) {
101 $contacts = DBA::select('account-user-view', ['pid', 'uri-id', 'uid'], $condition);
102 while ($contact = DBA::fetch($contacts)) {
103 if (empty($contact['uri-id']) || empty($contact['uid'])) {
106 $update_fields['cid'] = $contact['pid'];
107 $ret = DBA::update('user-contact', $update_fields, ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid']], true);
108 Logger::info('Updated user contact', ['uid' => $contact['uid'], 'id' => $contact['pid'], 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
111 DBA::close($contacts);
118 * Prepare field data for update/insert
120 * @param array $fields
121 * @return array prepared fields
123 private static function preparedFields(array $fields): array
125 unset($fields['uid']);
126 unset($fields['cid']);
127 unset($fields['uri-id']);
129 if (isset($fields['readonly'])) {
130 $fields['ignored'] = $fields['readonly'];
133 if (!empty($fields['self'])) {
134 $fields['rel'] = Contact::SELF;
137 return DI::dbaDefinition()->truncateFieldsForTable('user-contact', $fields);
141 * Block contact id for user id
143 * @param int $cid Either public contact id or user's contact id
144 * @param int $uid User ID
145 * @param boolean $blocked Is the contact blocked or unblocked?
149 public static function setBlocked(int $cid, int $uid, bool $blocked)
151 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
156 $contact = Contact::getById($cdata['public']);
158 Protocol::block($contact, $uid);
160 Protocol::unblock($contact, $uid);
163 if ($cdata['user'] != 0) {
164 DBA::update('contact', ['blocked' => $blocked], ['id' => $cdata['user'], 'pending' => false]);
167 DBA::update('user-contact', ['blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
171 * Returns "block" state for contact id and user id
173 * @param int $cid Either public contact id or user's contact id
174 * @param int $uid User ID
176 * @return boolean is the contact id blocked for the given user?
179 public static function isBlocked(int $cid, int $uid): bool
181 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
186 $public_blocked = false;
188 if (!empty($cdata['public'])) {
189 $public_contact = DBA::selectFirst('user-contact', ['blocked'], ['cid' => $cdata['public'], 'uid' => $uid]);
190 if (DBA::isResult($public_contact)) {
191 $public_blocked = (bool) $public_contact['blocked'];
195 $user_blocked = $public_blocked;
197 if (!empty($cdata['user'])) {
198 $user_contact = DBA::selectFirst('contact', ['blocked'], ['id' => $cdata['user'], 'pending' => false]);
199 if (DBA::isResult($user_contact)) {
200 $user_blocked = (bool) $user_contact['blocked'];
204 if ($user_blocked != $public_blocked) {
205 DBA::update('user-contact', ['blocked' => $user_blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
208 return $user_blocked;
212 * Ignore contact id for user id
214 * @param int $cid Either public contact id or user's contact id
215 * @param int $uid User ID
216 * @param boolean $ignored Is the contact ignored or unignored?
220 public static function setIgnored(int $cid, int $uid, bool $ignored)
222 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
227 if ($cdata['user'] != 0) {
228 DBA::update('contact', ['readonly' => $ignored], ['id' => $cdata['user'], 'pending' => false]);
231 DBA::update('user-contact', ['ignored' => $ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
235 * Returns "ignore" state for contact id and user id
237 * @param int $cid Either public contact id or user's contact id
238 * @param int $uid User ID
239 * @return boolean is the contact id ignored for the given user?
242 public static function isIgnored(int $cid, int $uid): bool
244 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
249 $public_ignored = false;
251 if (!empty($cdata['public'])) {
252 $public_contact = DBA::selectFirst('user-contact', ['ignored'], ['cid' => $cdata['public'], 'uid' => $uid]);
253 if (DBA::isResult($public_contact)) {
254 $public_ignored = (bool) $public_contact['ignored'];
258 $user_ignored = $public_ignored;
260 if (!empty($cdata['user'])) {
261 $user_contact = DBA::selectFirst('contact', ['readonly'], ['id' => $cdata['user'], 'pending' => false]);
262 if (DBA::isResult($user_contact)) {
263 $user_ignored = (bool) $user_contact['readonly'];
267 if ($user_ignored != $public_ignored) {
268 DBA::update('user-contact', ['ignored' => $user_ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
271 return $user_ignored;
275 * Set "collapsed" for contact id and user id
277 * @param int $cid Either public contact id or user's contact id
278 * @param int $uid User ID
279 * @param boolean $collapsed are the contact's posts collapsed or uncollapsed?
283 public static function setCollapsed(int $cid, int $uid, bool $collapsed)
285 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
290 DBA::update('user-contact', ['collapsed' => $collapsed], ['cid' => $cdata['public'], 'uid' => $uid], true);
294 * Returns "collapsed" state for contact id and user id
296 * @param int $cid Either public contact id or user's contact id
297 * @param int $uid User ID
298 * @return boolean is the contact id blocked for the given user?
299 * @throws HTTPException\InternalServerErrorException
300 * @throws \ImagickException
302 public static function isCollapsed(int $cid, int $uid): bool
304 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
311 if (!empty($cdata['public'])) {
312 $public_contact = DBA::selectFirst('user-contact', ['collapsed'], ['cid' => $cdata['public'], 'uid' => $uid]);
313 if (DBA::isResult($public_contact)) {
314 $collapsed = (bool) $public_contact['collapsed'];
322 * Set the channel post frequency for contact id and user id
324 * @param int $cid Either public contact id or user's contact id
325 * @param int $uid User ID
326 * @param int $frequency Type of post frequency in channels
330 public static function setChannelFrequency(int $cid, int $uid, int $frequency)
332 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
337 DBA::update('user-contact', ['channel-frequency' => $frequency], ['cid' => $cdata['public'], 'uid' => $uid], true);
341 * Returns the channel frequency state for contact id and user id
343 * @param int $cid Either public contact id or user's contact id
344 * @param int $uid User ID
345 * @return int Type of post frequency in channels
346 * @throws HTTPException\InternalServerErrorException
347 * @throws \ImagickException
349 public static function getChannelFrequency(int $cid, int $uid): int
351 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
356 $frequency = self::FREQUENCY_DEFAULT;
358 if (!empty($cdata['public'])) {
359 $public_contact = DBA::selectFirst('user-contact', ['channel-frequency'], ['cid' => $cdata['public'], 'uid' => $uid]);
360 if (DBA::isResult($public_contact)) {
361 $frequency = $public_contact['channel-frequency'] ?? self::FREQUENCY_DEFAULT;
369 * Set/Release that the user is blocked by the contact
371 * @param int $cid Either public contact id or user's contact id
372 * @param int $uid User ID
373 * @param boolean $blocked Is the user blocked or unblocked by the contact?
377 public static function setIsBlocked(int $cid, int $uid, bool $blocked)
379 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
384 DBA::update('user-contact', ['is-blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
388 * Returns if the user is blocked by the contact
390 * @param int $cid Either public contact id or user's contact id
391 * @param int $uid User ID
392 * @return boolean Is the user blocked or unblocked by the contact?
395 public static function isIsBlocked(int $cid, int $uid): bool
397 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
402 if (!empty($cdata['public'])) {
403 $public_contact = DBA::selectFirst('user-contact', ['is-blocked'], ['cid' => $cdata['public'], 'uid' => $uid]);
404 if (DBA::isResult($public_contact)) {
405 return $public_contact['is-blocked'];