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