]> git.mxchange.org Git - friendica.git/blob - src/Model/Contact/User.php
Fixed:
[friendica.git] / src / Model / Contact / User.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Database\DBStructure;
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('contact', ['uri-id', 'uid'], $condition);
98                         while ($contact = DBA::fetch($contacts)) {
99                                 if (empty($contact['uri-id']) || empty($contact['uid'])) {
100                                         continue;
101                                 }
102                                 $ret = DBA::update('user-contact', $update_fields, ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid']]);
103                                 Logger::info('Updated user contact', ['uid' => $contact['uid'], 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
104                         }
105
106                         DBA::close($contacts);
107                 }
108
109                 DBA::commit();
110         }
111
112         /**
113          * Prepare field data for update/insert
114          *
115          * @param array $fields
116          * @return array prepared fields
117          */
118         private static function preparedFields(array $fields): array
119         {
120                 unset($fields['uid']);
121                 unset($fields['cid']);
122                 unset($fields['uri-id']);
123
124                 if (isset($fields['readonly'])) {
125                         $fields['ignored'] = $fields['readonly'];
126                 }
127
128                 if (!empty($fields['self'])) {
129                         $fields['rel'] = Contact::SELF;
130                 }
131
132                 return DBStructure::getFieldsForTable('user-contact', $fields);
133         }
134
135         /**
136          * Block contact id for user id
137          *
138          * @param int     $cid     Either public contact id or user's contact id
139          * @param int     $uid     User ID
140          * @param boolean $blocked Is the contact blocked or unblocked?
141          * @return void
142          * @throws \Exception
143          */
144         public static function setBlocked(int $cid, int $uid, bool $blocked)
145         {
146                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
147                 if (empty($cdata)) {
148                         return;
149                 }
150
151                 $contact = Contact::getById($cdata['public']);
152                 if ($blocked) {
153                         Protocol::block($contact, $uid);
154                 } else {
155                         Protocol::unblock($contact, $uid);
156                 }
157
158                 if ($cdata['user'] != 0) {
159                         DBA::update('contact', ['blocked' => $blocked], ['id' => $cdata['user'], 'pending' => false]);
160                 }
161
162                 DBA::update('user-contact', ['blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
163         }
164
165         /**
166          * Returns "block" state for contact id and user id
167          *
168          * @param int $cid Either public contact id or user's contact id
169          * @param int $uid User ID
170          *
171          * @return boolean is the contact id blocked for the given user?
172          * @throws \Exception
173          */
174         public static function isBlocked(int $cid, int $uid): bool
175         {
176                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
177                 if (empty($cdata)) {
178                         return false;
179                 }
180
181                 $public_blocked = false;
182
183                 if (!empty($cdata['public'])) {
184                         $public_contact = DBA::selectFirst('user-contact', ['blocked'], ['cid' => $cdata['public'], 'uid' => $uid]);
185                         if (DBA::isResult($public_contact)) {
186                                 $public_blocked = (bool) $public_contact['blocked'];
187                         }
188                 }
189
190                 $user_blocked = $public_blocked;
191
192                 if (!empty($cdata['user'])) {
193                         $user_contact = DBA::selectFirst('contact', ['blocked'], ['id' => $cdata['user'], 'pending' => false]);
194                         if (DBA::isResult($user_contact)) {
195                                 $user_blocked = (bool) $user_contact['blocked'];
196                         }
197                 }
198
199                 if ($user_blocked != $public_blocked) {
200                         DBA::update('user-contact', ['blocked' => $user_blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
201                 }
202
203                 return $user_blocked;
204         }
205
206         /**
207          * Ignore contact id for user id
208          *
209          * @param int     $cid     Either public contact id or user's contact id
210          * @param int     $uid     User ID
211          * @param boolean $ignored Is the contact ignored or unignored?
212          * @return void
213          * @throws \Exception
214          */
215         public static function setIgnored(int $cid, int $uid, bool $ignored)
216         {
217                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
218                 if (empty($cdata)) {
219                         return;
220                 }
221
222                 if ($cdata['user'] != 0) {
223                         DBA::update('contact', ['readonly' => $ignored], ['id' => $cdata['user'], 'pending' => false]);
224                 }
225
226                 DBA::update('user-contact', ['ignored' => $ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
227         }
228
229         /**
230          * Returns "ignore" state for contact id and user id
231          *
232          * @param int $cid Either public contact id or user's contact id
233          * @param int $uid User ID
234          * @return boolean is the contact id ignored for the given user?
235          * @throws \Exception
236          */
237         public static function isIgnored(int $cid, int $uid): bool
238         {
239                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
240                 if (empty($cdata)) {
241                         return false;
242                 }
243
244                 $public_ignored = false;
245
246                 if (!empty($cdata['public'])) {
247                         $public_contact = DBA::selectFirst('user-contact', ['ignored'], ['cid' => $cdata['public'], 'uid' => $uid]);
248                         if (DBA::isResult($public_contact)) {
249                                 $public_ignored = $public_contact['ignored'];
250                         }
251                 }
252
253                 $user_ignored = $public_ignored;
254
255                 if (!empty($cdata['user'])) {
256                         $user_contact = DBA::selectFirst('contact', ['readonly'], ['id' => $cdata['user'], 'pending' => false]);
257                         if (DBA::isResult($user_contact)) {
258                                 $user_ignored = $user_contact['readonly'];
259                         }
260                 }
261
262                 if ($user_ignored != $public_ignored) {
263                         DBA::update('user-contact', ['ignored' => $user_ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
264                 }
265
266                 return $user_ignored;
267         }
268
269         /**
270          * Set "collapsed" for contact id and user id
271          *
272          * @param int     $cid       Either public contact id or user's contact id
273          * @param int     $uid       User ID
274          * @param boolean $collapsed are the contact's posts collapsed or uncollapsed?
275          * @return void
276          * @throws \Exception
277          */
278         public static function setCollapsed(int $cid, int $uid, bool $collapsed)
279         {
280                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
281                 if (empty($cdata)) {
282                         return;
283                 }
284
285                 DBA::update('user-contact', ['collapsed' => $collapsed], ['cid' => $cdata['public'], 'uid' => $uid], true);
286         }
287
288         /**
289          * Returns "collapsed" state for contact id and user id
290          *
291          * @param int $cid Either public contact id or user's contact id
292          * @param int $uid User ID
293          * @return boolean is the contact id blocked for the given user?
294          * @throws HTTPException\InternalServerErrorException
295          * @throws \ImagickException
296          */
297         public static function isCollapsed(int $cid, int $uid): bool
298         {
299                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
300                 if (empty($cdata)) {
301                         return false;
302                 }
303
304                 $collapsed = false;
305
306                 if (!empty($cdata['public'])) {
307                         $public_contact = DBA::selectFirst('user-contact', ['collapsed'], ['cid' => $cdata['public'], 'uid' => $uid]);
308                         if (DBA::isResult($public_contact)) {
309                                 $collapsed = $public_contact['collapsed'];
310                         }
311                 }
312
313                 return $collapsed;
314         }
315
316         /**
317          * Set/Release that the user is blocked by the contact
318          *
319          * @param int     $cid     Either public contact id or user's contact id
320          * @param int     $uid     User ID
321          * @param boolean $blocked Is the user blocked or unblocked by the contact?
322          * @return void
323          * @throws \Exception
324          */
325         public static function setIsBlocked(int $cid, int $uid, bool $blocked)
326         {
327                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
328                 if (empty($cdata)) {
329                         return;
330                 }
331
332                 DBA::update('user-contact', ['is-blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
333         }
334
335         /**
336          * Returns if the user is blocked by the contact
337          *
338          * @param int $cid Either public contact id or user's contact id
339          * @param int $uid User ID
340          * @return boolean Is the user blocked or unblocked by the contact?
341          * @throws \Exception
342          */
343         public static function isIsBlocked(int $cid, int $uid): bool
344         {
345                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
346                 if (empty($cdata)) {
347                         return false;
348                 }
349
350                 if (!empty($cdata['public'])) {
351                         $public_contact = DBA::selectFirst('user-contact', ['is-blocked'], ['cid' => $cdata['public'], 'uid' => $uid]);
352                         if (DBA::isResult($public_contact)) {
353                                 return $public_contact['is-blocked'];
354                         }
355                 }
356
357                 return false;
358         }
359 }