]> git.mxchange.org Git - friendica.git/blob - src/Model/Contact/User.php
Visibility is frequency
[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         const FREQUENCY_DEFAULT = 0;
41         const FREQUENCY_NEVER   = 1;
42         const FREQUENCY_ALWAYS  = 2;
43         const FREQUENCY_REDUCED = 3;
44         /**
45          * Insert a user-contact for a given contact array
46          *
47          * @param array $contact
48          * @return void
49          */
50         public static function insertForContactArray(array $contact)
51         {
52                 if (empty($contact['uid'])) {
53                         // We don't create entries for the public user - by now
54                         return false;
55                 }
56
57                 if (empty($contact['uri-id']) && empty($contact['url'])) {
58                         Logger::info('Missing contact details', ['contact' => $contact, 'callstack' => System::callstack(20)]);
59                         return false;
60                 }
61
62                 if (empty($contact['uri-id'])) {
63                         $contact['uri-id'] = ItemURI::getIdByURI($contact['url']);
64                 }
65
66                 $pcontact = Contact::selectFirst(['id'], ['uri-id' => $contact['uri-id'], 'uid' => 0]);
67                 if (!empty($contact['uri-id']) && DBA::isResult($pcontact)) {
68                         $pcid = $pcontact['id'];
69                 } elseif (empty($contact['url']) || !($pcid = Contact::getIdForURL($contact['url'], 0, false))) {
70                         Logger::info('Public contact for user not found', ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid']]);
71                         return false;
72                 }
73
74                 $fields = self::preparedFields($contact);
75                 $fields['cid'] = $pcid;
76                 $fields['uid'] = $contact['uid'];
77                 $fields['uri-id'] = $contact['uri-id'];
78
79                 $ret = DBA::insert('user-contact', $fields, Database::INSERT_UPDATE);
80
81                 Logger::info('Inserted user contact', ['uid' => $contact['uid'], 'cid' => $pcid, 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
82
83                 return $ret;
84         }
85
86         /**
87          * Apply changes from contact update data to user-contact table
88          *
89          * @param array $fields
90          * @param array $condition
91          * @return void
92          * @throws PDOException
93          * @throws Exception
94          */
95         public static function updateByContactUpdate(array $fields, array $condition)
96         {
97                 DBA::transaction();
98
99                 $update_fields = self::preparedFields($fields);
100                 if (!empty($update_fields)) {
101                         $contacts = DBA::select('account-user-view', ['pid', 'uri-id', 'uid'], $condition);
102                         while ($contact = DBA::fetch($contacts)) {
103                                 if (empty($contact['uri-id']) || empty($contact['uid'])) {
104                                         continue;
105                                 }
106                                 $update_fields['cid'] = $contact['pid'];
107                                 $ret = DBA::update('user-contact', $update_fields, ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid']], true);
108                                 Logger::info('Updated user contact', ['uid' => $contact['uid'], 'id' => $contact['pid'], 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
109                         }
110
111                         DBA::close($contacts);
112                 }
113
114                 DBA::commit();
115         }
116
117         /**
118          * Prepare field data for update/insert
119          *
120          * @param array $fields
121          * @return array prepared fields
122          */
123         private static function preparedFields(array $fields): array
124         {
125                 unset($fields['uid']);
126                 unset($fields['cid']);
127                 unset($fields['uri-id']);
128
129                 if (isset($fields['readonly'])) {
130                         $fields['ignored'] = $fields['readonly'];
131                 }
132
133                 if (!empty($fields['self'])) {
134                         $fields['rel'] = Contact::SELF;
135                 }
136
137                 return DI::dbaDefinition()->truncateFieldsForTable('user-contact', $fields);
138         }
139
140         /**
141          * Block contact id for user id
142          *
143          * @param int     $cid     Either public contact id or user's contact id
144          * @param int     $uid     User ID
145          * @param boolean $blocked Is the contact blocked or unblocked?
146          * @return void
147          * @throws \Exception
148          */
149         public static function setBlocked(int $cid, int $uid, bool $blocked)
150         {
151                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
152                 if (empty($cdata)) {
153                         return;
154                 }
155
156                 $contact = Contact::getById($cdata['public']);
157                 if ($blocked) {
158                         Protocol::block($contact, $uid);
159                 } else {
160                         Protocol::unblock($contact, $uid);
161                 }
162
163                 if ($cdata['user'] != 0) {
164                         DBA::update('contact', ['blocked' => $blocked], ['id' => $cdata['user'], 'pending' => false]);
165                 }
166
167                 DBA::update('user-contact', ['blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
168         }
169
170         /**
171          * Returns "block" state for contact id and user id
172          *
173          * @param int $cid Either public contact id or user's contact id
174          * @param int $uid User ID
175          *
176          * @return boolean is the contact id blocked for the given user?
177          * @throws \Exception
178          */
179         public static function isBlocked(int $cid, int $uid): bool
180         {
181                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
182                 if (empty($cdata)) {
183                         return false;
184                 }
185
186                 $public_blocked = false;
187
188                 if (!empty($cdata['public'])) {
189                         $public_contact = DBA::selectFirst('user-contact', ['blocked'], ['cid' => $cdata['public'], 'uid' => $uid]);
190                         if (DBA::isResult($public_contact)) {
191                                 $public_blocked = (bool) $public_contact['blocked'];
192                         }
193                 }
194
195                 $user_blocked = $public_blocked;
196
197                 if (!empty($cdata['user'])) {
198                         $user_contact = DBA::selectFirst('contact', ['blocked'], ['id' => $cdata['user'], 'pending' => false]);
199                         if (DBA::isResult($user_contact)) {
200                                 $user_blocked = (bool) $user_contact['blocked'];
201                         }
202                 }
203
204                 if ($user_blocked != $public_blocked) {
205                         DBA::update('user-contact', ['blocked' => $user_blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
206                 }
207
208                 return $user_blocked;
209         }
210
211         /**
212          * Ignore contact id for user id
213          *
214          * @param int     $cid     Either public contact id or user's contact id
215          * @param int     $uid     User ID
216          * @param boolean $ignored Is the contact ignored or unignored?
217          * @return void
218          * @throws \Exception
219          */
220         public static function setIgnored(int $cid, int $uid, bool $ignored)
221         {
222                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
223                 if (empty($cdata)) {
224                         return;
225                 }
226
227                 if ($cdata['user'] != 0) {
228                         DBA::update('contact', ['readonly' => $ignored], ['id' => $cdata['user'], 'pending' => false]);
229                 }
230
231                 DBA::update('user-contact', ['ignored' => $ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
232         }
233
234         /**
235          * Returns "ignore" state for contact id and user id
236          *
237          * @param int $cid Either public contact id or user's contact id
238          * @param int $uid User ID
239          * @return boolean is the contact id ignored for the given user?
240          * @throws \Exception
241          */
242         public static function isIgnored(int $cid, int $uid): bool
243         {
244                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
245                 if (empty($cdata)) {
246                         return false;
247                 }
248
249                 $public_ignored = false;
250
251                 if (!empty($cdata['public'])) {
252                         $public_contact = DBA::selectFirst('user-contact', ['ignored'], ['cid' => $cdata['public'], 'uid' => $uid]);
253                         if (DBA::isResult($public_contact)) {
254                                 $public_ignored = (bool) $public_contact['ignored'];
255                         }
256                 }
257
258                 $user_ignored = $public_ignored;
259
260                 if (!empty($cdata['user'])) {
261                         $user_contact = DBA::selectFirst('contact', ['readonly'], ['id' => $cdata['user'], 'pending' => false]);
262                         if (DBA::isResult($user_contact)) {
263                                 $user_ignored = (bool) $user_contact['readonly'];
264                         }
265                 }
266
267                 if ($user_ignored != $public_ignored) {
268                         DBA::update('user-contact', ['ignored' => $user_ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
269                 }
270
271                 return $user_ignored;
272         }
273
274         /**
275          * Set "collapsed" for contact id and user id
276          *
277          * @param int     $cid       Either public contact id or user's contact id
278          * @param int     $uid       User ID
279          * @param boolean $collapsed are the contact's posts collapsed or uncollapsed?
280          * @return void
281          * @throws \Exception
282          */
283         public static function setCollapsed(int $cid, int $uid, bool $collapsed)
284         {
285                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
286                 if (empty($cdata)) {
287                         return;
288                 }
289
290                 DBA::update('user-contact', ['collapsed' => $collapsed], ['cid' => $cdata['public'], 'uid' => $uid], true);
291         }
292
293         /**
294          * Returns "collapsed" state for contact id and user id
295          *
296          * @param int $cid Either public contact id or user's contact id
297          * @param int $uid User ID
298          * @return boolean is the contact id blocked for the given user?
299          * @throws HTTPException\InternalServerErrorException
300          * @throws \ImagickException
301          */
302         public static function isCollapsed(int $cid, int $uid): bool
303         {
304                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
305                 if (empty($cdata)) {
306                         return false;
307                 }
308
309                 $collapsed = false;
310
311                 if (!empty($cdata['public'])) {
312                         $public_contact = DBA::selectFirst('user-contact', ['collapsed'], ['cid' => $cdata['public'], 'uid' => $uid]);
313                         if (DBA::isResult($public_contact)) {
314                                 $collapsed = (bool) $public_contact['collapsed'];
315                         }
316                 }
317
318                 return $collapsed;
319         }
320
321         /**
322          * Set the channel post frequency for contact id and user id
323          *
324          * @param int $cid       Either public contact id or user's contact id
325          * @param int $uid       User ID
326          * @param int $frequency Type of post frequency in channels
327          * @return void
328          * @throws \Exception
329          */
330         public static function setChannelFrequency(int $cid, int $uid, int $frequency)
331         {
332                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
333                 if (empty($cdata)) {
334                         return;
335                 }
336
337                 DBA::update('user-contact', ['channel-frequency' => $frequency], ['cid' => $cdata['public'], 'uid' => $uid], true);
338         }
339
340         /**
341          * Returns the channel frequency state for contact id and user id
342          *
343          * @param int $cid Either public contact id or user's contact id
344          * @param int $uid User ID
345          * @return int Type of post frequency in channels
346          * @throws HTTPException\InternalServerErrorException
347          * @throws \ImagickException
348          */
349         public static function getChannelFrequency(int $cid, int $uid): int
350         {
351                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
352                 if (empty($cdata)) {
353                         return false;
354                 }
355
356                 $frequency = self::FREQUENCY_DEFAULT;
357
358                 if (!empty($cdata['public'])) {
359                         $public_contact = DBA::selectFirst('user-contact', ['channel-frequency'], ['cid' => $cdata['public'], 'uid' => $uid]);
360                         if (DBA::isResult($public_contact)) {
361                                 $frequency = $public_contact['channel-frequency'] ?? self::FREQUENCY_DEFAULT;
362                         }
363                 }
364
365                 return $frequency;
366         }
367
368         /**
369          * Set/Release that the user is blocked by the contact
370          *
371          * @param int     $cid     Either public contact id or user's contact id
372          * @param int     $uid     User ID
373          * @param boolean $blocked Is the user blocked or unblocked by the contact?
374          * @return void
375          * @throws \Exception
376          */
377         public static function setIsBlocked(int $cid, int $uid, bool $blocked)
378         {
379                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
380                 if (empty($cdata)) {
381                         return;
382                 }
383
384                 DBA::update('user-contact', ['is-blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
385         }
386
387         /**
388          * Returns if the user is blocked by the contact
389          *
390          * @param int $cid Either public contact id or user's contact id
391          * @param int $uid User ID
392          * @return boolean Is the user blocked or unblocked by the contact?
393          * @throws \Exception
394          */
395         public static function isIsBlocked(int $cid, int $uid): bool
396         {
397                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
398                 if (empty($cdata)) {
399                         return false;
400                 }
401
402                 if (!empty($cdata['public'])) {
403                         $public_contact = DBA::selectFirst('user-contact', ['is-blocked'], ['cid' => $cdata['public'], 'uid' => $uid]);
404                         if (DBA::isResult($public_contact)) {
405                                 return $public_contact['is-blocked'];
406                         }
407                 }
408
409                 return false;
410         }
411 }