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