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\Collection\Api\Mastodon\Fields;
26 use Friendica\Core\Logger;
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 (!isset($contact['uid']) || (empty($contact['uri-id']) && empty($contact['url']))) {
49 Logger::info('Missing contact details', ['contact' => $contact, 'callstack' => System::callstack(20)]);
53 if (empty($contact['uri-id'])) {
54 $contact['uri-id'] = ItemURI::getIdByURI($contact['url']);
57 $pcontact = Contact::selectFirst(['id'], ['uri-id' => $contact['uri-id'], 'uid' => 0]);
58 if (!DBA::isResult($pcontact)) {
59 Logger::info('Public contact for user not found', ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid'], 'cid' => $pcontact['id']]);
65 if (isset($fields['readonly'])) {
66 $fields['ignored'] = $fields['readonly'];
69 $fields = DBStructure::getFieldsForTable('user-contact', $fields);
70 $fields['cid'] = $pcontact['id'];
71 $fields['uid'] = $contact['uid'];
72 $fields['uri-id'] = $contact['uri-id'];
74 $ret = DBA::insert('user-contact', $fields, Database::INSERT_IGNORE);
76 Logger::info('Inserted user contact', ['uid' => $contact['uid'], 'cid' => $pcontact['id'], 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
82 * Apply changes from contact update data to user-contact table
84 * @param array $fields
85 * @param array $condition
87 * @throws PDOException
90 public static function UpdateByContactUpdate(array $fields, array $condition)
94 unset($fields['uid']);
95 unset($fields['cid']);
96 unset($fields['uri-id']);
98 if (isset($fields['readonly'])) {
99 $fields['ignored'] = $fields['readonly'];
102 $update_fields = DBStructure::getFieldsForTable('user-contact', $fields);
103 if (!empty($update_fields)) {
104 $contacts = DBA::select('contact', ['uri-id', 'uid'], $condition);
105 while ($row = DBA::fetch($contacts)) {
106 if (empty($row['uri-id'])) {
109 $ret = DBA::update('user-contact', $update_fields, ['uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
110 Logger::info('Updated user contact', ['uid' => $row['uid'], 'uri-id' => $row['uri-id'], 'ret' => $ret]);
113 DBA::close($contacts);
119 * Block contact id for user id
121 * @param int $cid Either public contact id or user's contact id
122 * @param int $uid User ID
123 * @param boolean $blocked Is the contact blocked or unblocked?
126 public static function setBlocked($cid, $uid, $blocked)
128 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
133 if ($cdata['user'] != 0) {
134 DBA::update('contact', ['blocked' => $blocked], ['id' => $cdata['user'], 'pending' => false]);
137 DBA::update('user-contact', ['blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
141 * Returns "block" state for contact id and user id
143 * @param int $cid Either public contact id or user's contact id
144 * @param int $uid User ID
146 * @return boolean is the contact id blocked for the given user?
149 public static function isBlocked($cid, $uid)
151 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
156 $public_blocked = false;
158 if (!empty($cdata['public'])) {
159 $public_contact = DBA::selectFirst('user-contact', ['blocked'], ['cid' => $cdata['public'], 'uid' => $uid]);
160 if (DBA::isResult($public_contact)) {
161 $public_blocked = $public_contact['blocked'];
165 $user_blocked = $public_blocked;
167 if (!empty($cdata['user'])) {
168 $user_contact = DBA::selectFirst('contact', ['blocked'], ['id' => $cdata['user'], 'pending' => false]);
169 if (DBA::isResult($user_contact)) {
170 $user_blocked = $user_contact['blocked'];
174 if ($user_blocked != $public_blocked) {
175 DBA::update('user-contact', ['blocked' => $user_blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
178 return $user_blocked;
182 * Ignore contact id for user id
184 * @param int $cid Either public contact id or user's contact id
185 * @param int $uid User ID
186 * @param boolean $ignored Is the contact ignored or unignored?
189 public static function setIgnored($cid, $uid, $ignored)
191 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
196 if ($cdata['user'] != 0) {
197 DBA::update('contact', ['readonly' => $ignored], ['id' => $cdata['user'], 'pending' => false]);
200 DBA::update('user-contact', ['ignored' => $ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
204 * Returns "ignore" state for contact id and user id
206 * @param int $cid Either public contact id or user's contact id
207 * @param int $uid User ID
209 * @return boolean is the contact id ignored for the given user?
212 public static function isIgnored($cid, $uid)
214 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
219 $public_ignored = false;
221 if (!empty($cdata['public'])) {
222 $public_contact = DBA::selectFirst('user-contact', ['ignored'], ['cid' => $cdata['public'], 'uid' => $uid]);
223 if (DBA::isResult($public_contact)) {
224 $public_ignored = $public_contact['ignored'];
228 $user_ignored = $public_ignored;
230 if (!empty($cdata['user'])) {
231 $user_contact = DBA::selectFirst('contact', ['readonly'], ['id' => $cdata['user'], 'pending' => false]);
232 if (DBA::isResult($user_contact)) {
233 $user_ignored = $user_contact['readonly'];
237 if ($user_ignored != $public_ignored) {
238 DBA::update('user-contact', ['ignored' => $user_ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
241 return $user_ignored;
245 * Set "collapsed" for contact id and user id
247 * @param int $cid Either public contact id or user's contact id
248 * @param int $uid User ID
249 * @param boolean $collapsed are the contact's posts collapsed or uncollapsed?
252 public static function setCollapsed($cid, $uid, $collapsed)
254 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
259 DBA::update('user-contact', ['collapsed' => $collapsed], ['cid' => $cdata['public'], 'uid' => $uid], true);
263 * Returns "collapsed" state for contact id and user id
265 * @param int $cid Either public contact id or user's contact id
266 * @param int $uid User ID
268 * @return boolean is the contact id blocked for the given user?
269 * @throws HTTPException\InternalServerErrorException
270 * @throws \ImagickException
272 public static function isCollapsed($cid, $uid)
274 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
281 if (!empty($cdata['public'])) {
282 $public_contact = DBA::selectFirst('user-contact', ['collapsed'], ['cid' => $cdata['public'], 'uid' => $uid]);
283 if (DBA::isResult($public_contact)) {
284 $collapsed = $public_contact['collapsed'];