]> git.mxchange.org Git - friendica.git/blob - src/Model/Contact/User.php
Individual callstacks are removed from the logger
[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\Database\Database;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Model\Contact;
31 use Friendica\Model\ItemURI;
32 use PDOException;
33
34 /**
35  * This class provides information about user related contacts based on the "user-contact" table.
36  */
37 class User
38 {
39         const FREQUENCY_DEFAULT = 0;
40         const FREQUENCY_NEVER   = 1;
41         const FREQUENCY_ALWAYS  = 2;
42         const FREQUENCY_REDUCED = 3;
43         /**
44          * Insert a user-contact for a given contact array
45          *
46          * @param array $contact
47          * @return void
48          */
49         public static function insertForContactArray(array $contact)
50         {
51                 if (empty($contact['uid'])) {
52                         // We don't create entries for the public user - by now
53                         return false;
54                 }
55
56                 if (empty($contact['uri-id']) && empty($contact['url'])) {
57                         Logger::info('Missing contact details', ['contact' => $contact]);
58                         return false;
59                 }
60
61                 if (empty($contact['uri-id'])) {
62                         $contact['uri-id'] = ItemURI::getIdByURI($contact['url']);
63                 }
64
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']]);
70                         return false;
71                 }
72
73                 $fields = self::preparedFields($contact);
74                 $fields['cid'] = $pcid;
75                 $fields['uid'] = $contact['uid'];
76                 $fields['uri-id'] = $contact['uri-id'];
77
78                 $ret = DBA::insert('user-contact', $fields, Database::INSERT_UPDATE);
79
80                 Logger::info('Inserted user contact', ['uid' => $contact['uid'], 'cid' => $pcid, 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
81
82                 return $ret;
83         }
84
85         /**
86          * Apply changes from contact update data to user-contact table
87          *
88          * @param array $fields
89          * @param array $condition
90          * @return void
91          * @throws PDOException
92          * @throws Exception
93          */
94         public static function updateByContactUpdate(array $fields, array $condition)
95         {
96                 DBA::transaction();
97
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'])) {
103                                         continue;
104                                 }
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]);
108                         }
109
110                         DBA::close($contacts);
111                 }
112
113                 DBA::commit();
114         }
115
116         /**
117          * Prepare field data for update/insert
118          *
119          * @param array $fields
120          * @return array prepared fields
121          */
122         private static function preparedFields(array $fields): array
123         {
124                 unset($fields['uid']);
125                 unset($fields['cid']);
126                 unset($fields['uri-id']);
127
128                 if (isset($fields['readonly'])) {
129                         $fields['ignored'] = $fields['readonly'];
130                 }
131
132                 if (!empty($fields['self'])) {
133                         $fields['rel'] = Contact::SELF;
134                 }
135
136                 return DI::dbaDefinition()->truncateFieldsForTable('user-contact', $fields);
137         }
138
139         /**
140          * Block contact id for user id
141          *
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?
145          * @return void
146          * @throws \Exception
147          */
148         public static function setBlocked(int $cid, int $uid, bool $blocked)
149         {
150                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
151                 if (empty($cdata)) {
152                         return;
153                 }
154
155                 $contact = Contact::getById($cdata['public']);
156                 if ($blocked) {
157                         Protocol::block($contact, $uid);
158                 } else {
159                         Protocol::unblock($contact, $uid);
160                 }
161
162                 if ($cdata['user'] != 0) {
163                         DBA::update('contact', ['blocked' => $blocked], ['id' => $cdata['user'], 'pending' => false]);
164                 }
165
166                 DBA::update('user-contact', ['blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
167         }
168
169         /**
170          * Returns "block" state for contact id and user id
171          *
172          * @param int $cid Either public contact id or user's contact id
173          * @param int $uid User ID
174          *
175          * @return boolean is the contact id blocked for the given user?
176          * @throws \Exception
177          */
178         public static function isBlocked(int $cid, int $uid): bool
179         {
180                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
181                 if (empty($cdata)) {
182                         return false;
183                 }
184
185                 $public_blocked = false;
186
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'];
191                         }
192                 }
193
194                 $user_blocked = $public_blocked;
195
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'];
200                         }
201                 }
202
203                 if ($user_blocked != $public_blocked) {
204                         DBA::update('user-contact', ['blocked' => $user_blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
205                 }
206
207                 return $user_blocked;
208         }
209
210         /**
211          * Ignore contact id for user id
212          *
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?
216          * @return void
217          * @throws \Exception
218          */
219         public static function setIgnored(int $cid, int $uid, bool $ignored)
220         {
221                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
222                 if (empty($cdata)) {
223                         return;
224                 }
225
226                 if ($cdata['user'] != 0) {
227                         DBA::update('contact', ['readonly' => $ignored], ['id' => $cdata['user'], 'pending' => false]);
228                 }
229
230                 DBA::update('user-contact', ['ignored' => $ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
231         }
232
233         /**
234          * Returns "ignore" state for contact id and user id
235          *
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?
239          * @throws \Exception
240          */
241         public static function isIgnored(int $cid, int $uid): bool
242         {
243                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
244                 if (empty($cdata)) {
245                         return false;
246                 }
247
248                 $public_ignored = false;
249
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'];
254                         }
255                 }
256
257                 $user_ignored = $public_ignored;
258
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'];
263                         }
264                 }
265
266                 if ($user_ignored != $public_ignored) {
267                         DBA::update('user-contact', ['ignored' => $user_ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
268                 }
269
270                 return $user_ignored;
271         }
272
273         /**
274          * Set "collapsed" for contact id and user id
275          *
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?
279          * @return void
280          * @throws \Exception
281          */
282         public static function setCollapsed(int $cid, int $uid, bool $collapsed)
283         {
284                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
285                 if (empty($cdata)) {
286                         return;
287                 }
288
289                 DBA::update('user-contact', ['collapsed' => $collapsed], ['cid' => $cdata['public'], 'uid' => $uid], true);
290         }
291
292         /**
293          * Returns "collapsed" state for contact id and user id
294          *
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
300          */
301         public static function isCollapsed(int $cid, int $uid): bool
302         {
303                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
304                 if (empty($cdata)) {
305                         return false;
306                 }
307
308                 $collapsed = false;
309
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'];
314                         }
315                 }
316
317                 return $collapsed;
318         }
319
320         /**
321          * Set the channel post frequency for contact id and user id
322          *
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
326          * @return void
327          * @throws \Exception
328          */
329         public static function setChannelFrequency(int $cid, int $uid, int $frequency)
330         {
331                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
332                 if (empty($cdata)) {
333                         return;
334                 }
335
336                 DBA::update('user-contact', ['channel-frequency' => $frequency], ['cid' => $cdata['public'], 'uid' => $uid], true);
337         }
338
339         /**
340          * Returns the channel frequency state for contact id and user id
341          *
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
347          */
348         public static function getChannelFrequency(int $cid, int $uid): int
349         {
350                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
351                 if (empty($cdata)) {
352                         return false;
353                 }
354
355                 $frequency = self::FREQUENCY_DEFAULT;
356
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;
361                         }
362                 }
363
364                 return $frequency;
365         }
366
367         /**
368          * Set/Release that the user is blocked by the contact
369          *
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?
373          * @return void
374          * @throws \Exception
375          */
376         public static function setIsBlocked(int $cid, int $uid, bool $blocked)
377         {
378                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
379                 if (empty($cdata)) {
380                         return;
381                 }
382
383                 DBA::update('user-contact', ['is-blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
384         }
385
386         /**
387          * Returns if the user is blocked by the contact
388          *
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?
392          * @throws \Exception
393          */
394         public static function isIsBlocked(int $cid, int $uid): bool
395         {
396                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
397                 if (empty($cdata)) {
398                         return false;
399                 }
400
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'];
405                         }
406                 }
407
408                 return false;
409         }
410 }