]> git.mxchange.org Git - friendica.git/blob - src/Model/Contact/User.php
eb0b5af096aa73aed342bcb51783703fbc0b082b
[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\System;
27 use Friendica\Database\Database;
28 use Friendica\Database\DBA;
29 use Friendica\Database\DBStructure;
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         /**
40          * Insert a user-contact for a given contact array
41          *
42          * @param array $contact
43          * @return void
44          */
45         public static function insertForContactArray(array $contact)
46         {
47                 if (empty($contact['uid'])) {
48                         // We don't create entries for the public user - by now
49                         return false;
50                 }
51
52                 if (empty($contact['uri-id']) && empty($contact['url'])) {
53                         Logger::info('Missing contact details', ['contact' => $contact, 'callstack' => System::callstack(20)]);
54                         return false;
55                 }
56
57                 if (empty($contact['uri-id'])) {
58                         $contact['uri-id'] = ItemURI::getIdByURI($contact['url']);
59                 }
60
61                 $pcontact = Contact::selectFirst(['id'], ['uri-id' => $contact['uri-id'], 'uid' => 0]);
62                 if (!empty($contact['uri-id']) && DBA::isResult($pcontact)) {
63                         $pcid = $pcontact['id'];
64                 } elseif (empty($contact['url']) || !($pcid = Contact::getIdForURL($contact['url'], 0, false))) {
65                         Logger::info('Public contact for user not found', ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid']]);
66                         return false;
67                 }
68
69                 $fields = self::preparedFields($contact);
70                 $fields['cid'] = $pcid;
71                 $fields['uid'] = $contact['uid'];
72                 $fields['uri-id'] = $contact['uri-id'];
73
74                 $ret = DBA::insert('user-contact', $fields, Database::INSERT_UPDATE);
75
76                 Logger::info('Inserted user contact', ['uid' => $contact['uid'], 'cid' => $pcid, 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
77
78                 return $ret;
79         }
80
81         /**
82          * Apply changes from contact update data to user-contact table
83          *
84          * @param array $fields 
85          * @param array $condition 
86          * @return void 
87          * @throws PDOException 
88          * @throws Exception 
89          */
90         public static function updateByContactUpdate(array $fields, array $condition)
91         {
92                 DBA::transaction();
93
94                 $update_fields = self::preparedFields($fields);
95                 if (!empty($update_fields)) {
96                         $contacts = DBA::select('contact', ['uri-id', 'uid'], $condition);
97                         while ($row = DBA::fetch($contacts)) {
98                                 if (empty($row['uri-id']) || empty($contact['uid'])) {
99                                         continue;
100                                 }
101                                 $ret = DBA::update('user-contact', $update_fields, ['uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
102                                 Logger::info('Updated user contact', ['uid' => $row['uid'], 'uri-id' => $row['uri-id'], 'ret' => $ret]);
103                         }
104
105                         DBA::close($contacts);
106                 }
107                 DBA::commit();  
108         }
109
110         /**
111          * Prepare field data for update/insert
112          *
113          * @param array $fields
114          * @return array prepared fields
115          */
116         private static function preparedFields(array $fields): array
117         {
118                 unset($fields['uid']);
119                 unset($fields['cid']);
120                 unset($fields['uri-id']);
121
122                 if (isset($fields['readonly'])) {
123                         $fields['ignored'] = $fields['readonly'];
124                 }
125
126                 if (!empty($fields['self'])) {
127                         $fields['rel'] = Contact::SELF;
128                 }
129
130                 return DBStructure::getFieldsForTable('user-contact', $fields);
131         }
132
133         /**
134          * Block contact id for user id
135          *
136          * @param int     $cid     Either public contact id or user's contact id
137          * @param int     $uid     User ID
138          * @param boolean $blocked Is the contact blocked or unblocked?
139          * @throws \Exception
140          */
141         public static function setBlocked($cid, $uid, $blocked)
142         {
143                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
144                 if (empty($cdata)) {
145                         return;
146                 }
147
148                 if ($cdata['user'] != 0) {
149                         DBA::update('contact', ['blocked' => $blocked], ['id' => $cdata['user'], 'pending' => false]);
150                 }
151
152                 DBA::update('user-contact', ['blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
153         }
154
155         /**
156          * Returns "block" state for contact id and user id
157          *
158          * @param int $cid Either public contact id or user's contact id
159          * @param int $uid User ID
160          *
161          * @return boolean is the contact id blocked for the given user?
162          * @throws \Exception
163          */
164         public static function isBlocked($cid, $uid)
165         {
166                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
167                 if (empty($cdata)) {
168                         return false;
169                 }
170
171                 $public_blocked = false;
172
173                 if (!empty($cdata['public'])) {
174                         $public_contact = DBA::selectFirst('user-contact', ['blocked'], ['cid' => $cdata['public'], 'uid' => $uid]);
175                         if (DBA::isResult($public_contact)) {
176                                 $public_blocked = $public_contact['blocked'];
177                         }
178                 }
179
180                 $user_blocked = $public_blocked;
181
182                 if (!empty($cdata['user'])) {
183                         $user_contact = DBA::selectFirst('contact', ['blocked'], ['id' => $cdata['user'], 'pending' => false]);
184                         if (DBA::isResult($user_contact)) {
185                                 $user_blocked = $user_contact['blocked'];
186                         }
187                 }
188
189                 if ($user_blocked != $public_blocked) {
190                         DBA::update('user-contact', ['blocked' => $user_blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
191                 }
192
193                 return $user_blocked;
194         }
195
196         /**
197          * Ignore contact id for user id
198          *
199          * @param int     $cid     Either public contact id or user's contact id
200          * @param int     $uid     User ID
201          * @param boolean $ignored Is the contact ignored or unignored?
202          * @throws \Exception
203          */
204         public static function setIgnored($cid, $uid, $ignored)
205         {
206                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
207                 if (empty($cdata)) {
208                         return;
209                 }
210
211                 if ($cdata['user'] != 0) {
212                         DBA::update('contact', ['readonly' => $ignored], ['id' => $cdata['user'], 'pending' => false]);
213                 }
214
215                 DBA::update('user-contact', ['ignored' => $ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
216         }
217
218         /**
219          * Returns "ignore" state for contact id and user id
220          *
221          * @param int $cid Either public contact id or user's contact id
222          * @param int $uid User ID
223          *
224          * @return boolean is the contact id ignored for the given user?
225          * @throws \Exception
226          */
227         public static function isIgnored($cid, $uid)
228         {
229                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
230                 if (empty($cdata)) {
231                         return false;
232                 }
233
234                 $public_ignored = false;
235
236                 if (!empty($cdata['public'])) {
237                         $public_contact = DBA::selectFirst('user-contact', ['ignored'], ['cid' => $cdata['public'], 'uid' => $uid]);
238                         if (DBA::isResult($public_contact)) {
239                                 $public_ignored = $public_contact['ignored'];
240                         }
241                 }
242
243                 $user_ignored = $public_ignored;
244
245                 if (!empty($cdata['user'])) {
246                         $user_contact = DBA::selectFirst('contact', ['readonly'], ['id' => $cdata['user'], 'pending' => false]);
247                         if (DBA::isResult($user_contact)) {
248                                 $user_ignored = $user_contact['readonly'];
249                         }
250                 }
251
252                 if ($user_ignored != $public_ignored) {
253                         DBA::update('user-contact', ['ignored' => $user_ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
254                 }
255
256                 return $user_ignored;
257         }
258
259         /**
260          * Set "collapsed" for contact id and user id
261          *
262          * @param int     $cid       Either public contact id or user's contact id
263          * @param int     $uid       User ID
264          * @param boolean $collapsed are the contact's posts collapsed or uncollapsed?
265          * @throws \Exception
266          */
267         public static function setCollapsed($cid, $uid, $collapsed)
268         {
269                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
270                 if (empty($cdata)) {
271                         return;
272                 }
273
274                 DBA::update('user-contact', ['collapsed' => $collapsed], ['cid' => $cdata['public'], 'uid' => $uid], true);
275         }
276
277         /**
278          * Returns "collapsed" state for contact id and user id
279          *
280          * @param int $cid Either public contact id or user's contact id
281          * @param int $uid User ID
282          *
283          * @return boolean is the contact id blocked for the given user?
284          * @throws HTTPException\InternalServerErrorException
285          * @throws \ImagickException
286          */
287         public static function isCollapsed($cid, $uid)
288         {
289                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
290                 if (empty($cdata)) {
291                         return;
292                 }
293
294                 $collapsed = false;
295
296                 if (!empty($cdata['public'])) {
297                         $public_contact = DBA::selectFirst('user-contact', ['collapsed'], ['cid' => $cdata['public'], 'uid' => $uid]);
298                         if (DBA::isResult($public_contact)) {
299                                 $collapsed = $public_contact['collapsed'];
300                         }
301                 }
302
303                 return $collapsed;
304         }
305 }