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