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