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