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\Database\Database;
28 use Friendica\Database\DBA;
30 use Friendica\Model\Contact;
31 use Friendica\Model\ItemURI;
35 * This class provides information about user related contacts based on the "user-contact" table.
39 const FREQUENCY_DEFAULT = 0;
40 const FREQUENCY_NEVER = 1;
41 const FREQUENCY_ALWAYS = 2;
42 const FREQUENCY_REDUCED = 3;
44 * Insert a user-contact for a given contact array
46 * @param array $contact
49 public static function insertForContactArray(array $contact)
51 if (empty($contact['uid'])) {
52 // We don't create entries for the public user - by now
56 if (empty($contact['uri-id']) && empty($contact['url'])) {
57 Logger::info('Missing contact details', ['contact' => $contact]);
61 if (empty($contact['uri-id'])) {
62 $contact['uri-id'] = ItemURI::getIdByURI($contact['url']);
65 $pcontact = Contact::selectFirst(['id'], ['uri-id' => $contact['uri-id'], 'uid' => 0]);
66 if (!empty($contact['uri-id']) && DBA::isResult($pcontact)) {
67 $pcid = $pcontact['id'];
68 } elseif (empty($contact['url']) || !($pcid = Contact::getIdForURL($contact['url'], 0, false))) {
69 Logger::info('Public contact for user not found', ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid']]);
73 $fields = self::preparedFields($contact);
74 $fields['cid'] = $pcid;
75 $fields['uid'] = $contact['uid'];
76 $fields['uri-id'] = $contact['uri-id'];
78 $ret = DBA::insert('user-contact', $fields, Database::INSERT_UPDATE);
80 Logger::info('Inserted user contact', ['uid' => $contact['uid'], 'cid' => $pcid, 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
86 * Apply changes from contact update data to user-contact table
88 * @param array $fields
89 * @param array $condition
91 * @throws PDOException
94 public static function updateByContactUpdate(array $fields, array $condition)
98 $update_fields = self::preparedFields($fields);
99 if (!empty($update_fields)) {
100 $contacts = DBA::select('account-user-view', ['pid', 'uri-id', 'uid'], $condition);
101 while ($contact = DBA::fetch($contacts)) {
102 if (empty($contact['uri-id']) || empty($contact['uid'])) {
105 $update_fields['cid'] = $contact['pid'];
106 $ret = DBA::update('user-contact', $update_fields, ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid']], true);
107 Logger::info('Updated user contact', ['uid' => $contact['uid'], 'id' => $contact['pid'], 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
110 DBA::close($contacts);
117 * Prepare field data for update/insert
119 * @param array $fields
120 * @return array prepared fields
122 private static function preparedFields(array $fields): array
124 unset($fields['uid']);
125 unset($fields['cid']);
126 unset($fields['uri-id']);
128 if (isset($fields['readonly'])) {
129 $fields['ignored'] = $fields['readonly'];
132 if (!empty($fields['self'])) {
133 $fields['rel'] = Contact::SELF;
136 return DI::dbaDefinition()->truncateFieldsForTable('user-contact', $fields);
140 * Block contact id for user id
142 * @param int $cid Either public contact id or user's contact id
143 * @param int $uid User ID
144 * @param boolean $blocked Is the contact blocked or unblocked?
148 public static function setBlocked(int $cid, int $uid, bool $blocked)
150 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
155 $contact = Contact::getById($cdata['public']);
157 Protocol::block($contact, $uid);
159 Protocol::unblock($contact, $uid);
162 if ($cdata['user'] != 0) {
163 DBA::update('contact', ['blocked' => $blocked], ['id' => $cdata['user'], 'pending' => false]);
166 DBA::update('user-contact', ['blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
170 * Returns "block" state for contact id and user id
172 * @param int $cid Either public contact id or user's contact id
173 * @param int $uid User ID
175 * @return boolean is the contact id blocked for the given user?
178 public static function isBlocked(int $cid, int $uid): bool
180 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
185 $public_blocked = false;
187 if (!empty($cdata['public'])) {
188 $public_contact = DBA::selectFirst('user-contact', ['blocked'], ['cid' => $cdata['public'], 'uid' => $uid]);
189 if (DBA::isResult($public_contact)) {
190 $public_blocked = (bool) $public_contact['blocked'];
194 $user_blocked = $public_blocked;
196 if (!empty($cdata['user'])) {
197 $user_contact = DBA::selectFirst('contact', ['blocked'], ['id' => $cdata['user'], 'pending' => false]);
198 if (DBA::isResult($user_contact)) {
199 $user_blocked = (bool) $user_contact['blocked'];
203 if ($user_blocked != $public_blocked) {
204 DBA::update('user-contact', ['blocked' => $user_blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
207 return $user_blocked;
211 * Ignore contact id for user id
213 * @param int $cid Either public contact id or user's contact id
214 * @param int $uid User ID
215 * @param boolean $ignored Is the contact ignored or unignored?
219 public static function setIgnored(int $cid, int $uid, bool $ignored)
221 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
226 if ($cdata['user'] != 0) {
227 DBA::update('contact', ['readonly' => $ignored], ['id' => $cdata['user'], 'pending' => false]);
230 DBA::update('user-contact', ['ignored' => $ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
234 * Returns "ignore" state for contact id and user id
236 * @param int $cid Either public contact id or user's contact id
237 * @param int $uid User ID
238 * @return boolean is the contact id ignored for the given user?
241 public static function isIgnored(int $cid, int $uid): bool
243 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
248 $public_ignored = false;
250 if (!empty($cdata['public'])) {
251 $public_contact = DBA::selectFirst('user-contact', ['ignored'], ['cid' => $cdata['public'], 'uid' => $uid]);
252 if (DBA::isResult($public_contact)) {
253 $public_ignored = (bool) $public_contact['ignored'];
257 $user_ignored = $public_ignored;
259 if (!empty($cdata['user'])) {
260 $user_contact = DBA::selectFirst('contact', ['readonly'], ['id' => $cdata['user'], 'pending' => false]);
261 if (DBA::isResult($user_contact)) {
262 $user_ignored = (bool) $user_contact['readonly'];
266 if ($user_ignored != $public_ignored) {
267 DBA::update('user-contact', ['ignored' => $user_ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
270 return $user_ignored;
274 * Set "collapsed" for contact id and user id
276 * @param int $cid Either public contact id or user's contact id
277 * @param int $uid User ID
278 * @param boolean $collapsed are the contact's posts collapsed or uncollapsed?
282 public static function setCollapsed(int $cid, int $uid, bool $collapsed)
284 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
289 DBA::update('user-contact', ['collapsed' => $collapsed], ['cid' => $cdata['public'], 'uid' => $uid], true);
293 * Returns "collapsed" state for contact id and user id
295 * @param int $cid Either public contact id or user's contact id
296 * @param int $uid User ID
297 * @return boolean is the contact id blocked for the given user?
298 * @throws HTTPException\InternalServerErrorException
299 * @throws \ImagickException
301 public static function isCollapsed(int $cid, int $uid): bool
303 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
310 if (!empty($cdata['public'])) {
311 $public_contact = DBA::selectFirst('user-contact', ['collapsed'], ['cid' => $cdata['public'], 'uid' => $uid]);
312 if (DBA::isResult($public_contact)) {
313 $collapsed = (bool) $public_contact['collapsed'];
321 * Set the channel post frequency for contact id and user id
323 * @param int $cid Either public contact id or user's contact id
324 * @param int $uid User ID
325 * @param int $frequency Type of post frequency in channels
329 public static function setChannelFrequency(int $cid, int $uid, int $frequency)
331 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
336 DBA::update('user-contact', ['channel-frequency' => $frequency], ['cid' => $cdata['public'], 'uid' => $uid], true);
340 * Returns the channel frequency state for contact id and user id
342 * @param int $cid Either public contact id or user's contact id
343 * @param int $uid User ID
344 * @return int Type of post frequency in channels
345 * @throws HTTPException\InternalServerErrorException
346 * @throws \ImagickException
348 public static function getChannelFrequency(int $cid, int $uid): int
350 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
355 $frequency = self::FREQUENCY_DEFAULT;
357 if (!empty($cdata['public'])) {
358 $public_contact = DBA::selectFirst('user-contact', ['channel-frequency'], ['cid' => $cdata['public'], 'uid' => $uid]);
359 if (DBA::isResult($public_contact)) {
360 $frequency = $public_contact['channel-frequency'] ?? self::FREQUENCY_DEFAULT;
368 * Set/Release that the user is blocked by the contact
370 * @param int $cid Either public contact id or user's contact id
371 * @param int $uid User ID
372 * @param boolean $blocked Is the user blocked or unblocked by the contact?
376 public static function setIsBlocked(int $cid, int $uid, bool $blocked)
378 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
383 DBA::update('user-contact', ['is-blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
387 * Returns if the user is blocked by the contact
389 * @param int $cid Either public contact id or user's contact id
390 * @param int $uid User ID
391 * @return boolean Is the user blocked or unblocked by the contact?
394 public static function isIsBlocked(int $cid, int $uid): bool
396 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
401 if (!empty($cdata['public'])) {
402 $public_contact = DBA::selectFirst('user-contact', ['is-blocked'], ['cid' => $cdata['public'], 'uid' => $uid]);
403 if (DBA::isResult($public_contact)) {
404 return $public_contact['is-blocked'];