]> git.mxchange.org Git - friendica.git/blob - src/Model/Contact/User.php
Merge pull request #13291 from Quix0r/features/config-blocklist-published
[friendica.git] / src / Model / Contact / User.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Model\Contact;
23
24 use Exception;
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\DI;
31 use Friendica\Model\Contact;
32 use Friendica\Model\ItemURI;
33 use PDOException;
34
35 /**
36  * This class provides information about user related contacts based on the "user-contact" table.
37  */
38 class User
39 {
40         /**
41          * Insert a user-contact for a given contact array
42          *
43          * @param array $contact
44          * @return void
45          */
46         public static function insertForContactArray(array $contact)
47         {
48                 if (empty($contact['uid'])) {
49                         // We don't create entries for the public user - by now
50                         return false;
51                 }
52
53                 if (empty($contact['uri-id']) && empty($contact['url'])) {
54                         Logger::info('Missing contact details', ['contact' => $contact, 'callstack' => System::callstack(20)]);
55                         return false;
56                 }
57
58                 if (empty($contact['uri-id'])) {
59                         $contact['uri-id'] = ItemURI::getIdByURI($contact['url']);
60                 }
61
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']]);
67                         return false;
68                 }
69
70                 $fields = self::preparedFields($contact);
71                 $fields['cid'] = $pcid;
72                 $fields['uid'] = $contact['uid'];
73                 $fields['uri-id'] = $contact['uri-id'];
74
75                 $ret = DBA::insert('user-contact', $fields, Database::INSERT_UPDATE);
76
77                 Logger::info('Inserted user contact', ['uid' => $contact['uid'], 'cid' => $pcid, 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
78
79                 return $ret;
80         }
81
82         /**
83          * Apply changes from contact update data to user-contact table
84          *
85          * @param array $fields
86          * @param array $condition
87          * @return void
88          * @throws PDOException
89          * @throws Exception
90          */
91         public static function updateByContactUpdate(array $fields, array $condition)
92         {
93                 DBA::transaction();
94
95                 $update_fields = self::preparedFields($fields);
96                 if (!empty($update_fields)) {
97                         $contacts = DBA::select('account-user-view', ['pid', 'uri-id', 'uid'], $condition);
98                         while ($contact = DBA::fetch($contacts)) {
99                                 if (empty($contact['uri-id']) || empty($contact['uid'])) {
100                                         continue;
101                                 }
102                                 $update_fields['cid'] = $contact['pid'];
103                                 $ret = DBA::update('user-contact', $update_fields, ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid']], true);
104                                 Logger::info('Updated user contact', ['uid' => $contact['uid'], 'id' => $contact['pid'], 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
105                         }
106
107                         DBA::close($contacts);
108                 }
109
110                 DBA::commit();
111         }
112
113         /**
114          * Prepare field data for update/insert
115          *
116          * @param array $fields
117          * @return array prepared fields
118          */
119         private static function preparedFields(array $fields): array
120         {
121                 unset($fields['uid']);
122                 unset($fields['cid']);
123                 unset($fields['uri-id']);
124
125                 if (isset($fields['readonly'])) {
126                         $fields['ignored'] = $fields['readonly'];
127                 }
128
129                 if (!empty($fields['self'])) {
130                         $fields['rel'] = Contact::SELF;
131                 }
132
133                 return DI::dbaDefinition()->truncateFieldsForTable('user-contact', $fields);
134         }
135
136         /**
137          * Block contact id for user id
138          *
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?
142          * @return void
143          * @throws \Exception
144          */
145         public static function setBlocked(int $cid, int $uid, bool $blocked)
146         {
147                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
148                 if (empty($cdata)) {
149                         return;
150                 }
151
152                 $contact = Contact::getById($cdata['public']);
153                 if ($blocked) {
154                         Protocol::block($contact, $uid);
155                 } else {
156                         Protocol::unblock($contact, $uid);
157                 }
158
159                 if ($cdata['user'] != 0) {
160                         DBA::update('contact', ['blocked' => $blocked], ['id' => $cdata['user'], 'pending' => false]);
161                 }
162
163                 DBA::update('user-contact', ['blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
164         }
165
166         /**
167          * Returns "block" state for contact id and user id
168          *
169          * @param int $cid Either public contact id or user's contact id
170          * @param int $uid User ID
171          *
172          * @return boolean is the contact id blocked for the given user?
173          * @throws \Exception
174          */
175         public static function isBlocked(int $cid, int $uid): bool
176         {
177                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
178                 if (empty($cdata)) {
179                         return false;
180                 }
181
182                 $public_blocked = false;
183
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'];
188                         }
189                 }
190
191                 $user_blocked = $public_blocked;
192
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'];
197                         }
198                 }
199
200                 if ($user_blocked != $public_blocked) {
201                         DBA::update('user-contact', ['blocked' => $user_blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
202                 }
203
204                 return $user_blocked;
205         }
206
207         /**
208          * Ignore contact id for user id
209          *
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?
213          * @return void
214          * @throws \Exception
215          */
216         public static function setIgnored(int $cid, int $uid, bool $ignored)
217         {
218                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
219                 if (empty($cdata)) {
220                         return;
221                 }
222
223                 if ($cdata['user'] != 0) {
224                         DBA::update('contact', ['readonly' => $ignored], ['id' => $cdata['user'], 'pending' => false]);
225                 }
226
227                 DBA::update('user-contact', ['ignored' => $ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
228         }
229
230         /**
231          * Returns "ignore" state for contact id and user id
232          *
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?
236          * @throws \Exception
237          */
238         public static function isIgnored(int $cid, int $uid): bool
239         {
240                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
241                 if (empty($cdata)) {
242                         return false;
243                 }
244
245                 $public_ignored = false;
246
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'];
251                         }
252                 }
253
254                 $user_ignored = $public_ignored;
255
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'];
260                         }
261                 }
262
263                 if ($user_ignored != $public_ignored) {
264                         DBA::update('user-contact', ['ignored' => $user_ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
265                 }
266
267                 return $user_ignored;
268         }
269
270         /**
271          * Set "collapsed" for contact id and user id
272          *
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?
276          * @return void
277          * @throws \Exception
278          */
279         public static function setCollapsed(int $cid, int $uid, bool $collapsed)
280         {
281                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
282                 if (empty($cdata)) {
283                         return;
284                 }
285
286                 DBA::update('user-contact', ['collapsed' => $collapsed], ['cid' => $cdata['public'], 'uid' => $uid], true);
287         }
288
289         /**
290          * Returns "collapsed" state for contact id and user id
291          *
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
297          */
298         public static function isCollapsed(int $cid, int $uid): bool
299         {
300                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
301                 if (empty($cdata)) {
302                         return false;
303                 }
304
305                 $collapsed = false;
306
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'];
311                         }
312                 }
313
314                 return $collapsed;
315         }
316
317         /**
318          * Set/Release that the user is blocked by the contact
319          *
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?
323          * @return void
324          * @throws \Exception
325          */
326         public static function setIsBlocked(int $cid, int $uid, bool $blocked)
327         {
328                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
329                 if (empty($cdata)) {
330                         return;
331                 }
332
333                 DBA::update('user-contact', ['is-blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
334         }
335
336         /**
337          * Returns if the user is blocked by the contact
338          *
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?
342          * @throws \Exception
343          */
344         public static function isIsBlocked(int $cid, int $uid): bool
345         {
346                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
347                 if (empty($cdata)) {
348                         return false;
349                 }
350
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'];
355                         }
356                 }
357
358                 return false;
359         }
360 }