]> git.mxchange.org Git - friendica.git/blob - src/Model/Contact/User.php
0283fae7209c90a03cf37009b69ccd3129bd1eb2
[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 Friendica\Core\Logger;
25 use Friendica\Core\System;
26 use Friendica\Database\Database;
27 use Friendica\Database\DBA;
28 use Friendica\Model\Contact;
29 use Friendica\Model\ItemURI;
30
31 /**
32  * This class provides information about user related contacts based on the "user-contact" table.
33  */
34 class User
35 {
36         /**
37          * Insert a user-contact for a given contact array
38          *
39          * @param array $contact
40          * @return void
41          */
42         public static function insertForContactArray(array $contact)
43         {
44                 if (!isset($contact['uid']) || (empty($contact['uri-id']) && empty($contact['url']))) {
45                         Logger::info('Missing contact details', ['contact' => $contact, 'callstack' => System::callstack(20)]);
46                         return false;
47                 }
48
49                 if (empty($contact['uri-id'])) {
50                         $contact['uri-id'] = ItemURI::getIdByURI($contact['url']);
51                 }
52
53                 $pcontact = Contact::selectFirst(['id'], ['uri-id' => $contact['uri-id'], 'uid' => 0]);
54                 if (!DBA::isResult($pcontact)) {
55                         Logger::info('Public contact for user not found', ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid'], 'cid' => $pcontact['id']]);
56                         return false;
57                 }
58
59                 $fields = [
60                         'cid'     => $pcontact['id'],
61                         'uid'     => $contact['uid'],
62                         'uri-id'  => $contact['uri-id'],
63                         'blocked' => $contact['blocked'] ?? false,
64                         'ignored' => $contact['readonly'] ?? false,
65                 ];
66
67                 $ret = DBA::insert('user-contact', $fields, Database::INSERT_IGNORE);
68
69                 Logger::info('Inserted user contact', ['uid' => $contact['uid'], 'cid' => $pcontact['id'], 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
70
71                 return $ret;
72         }
73
74         /**
75          * Block contact id for user id
76          *
77          * @param int     $cid     Either public contact id or user's contact id
78          * @param int     $uid     User ID
79          * @param boolean $blocked Is the contact blocked or unblocked?
80          * @throws \Exception
81          */
82         public static function setBlocked($cid, $uid, $blocked)
83         {
84                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
85                 if (empty($cdata)) {
86                         return;
87                 }
88
89                 if ($cdata['user'] != 0) {
90                         DBA::update('contact', ['blocked' => $blocked], ['id' => $cdata['user'], 'pending' => false]);
91                 }
92
93                 DBA::update('user-contact', ['blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
94         }
95
96         /**
97          * Returns "block" state for contact id and user id
98          *
99          * @param int $cid Either public contact id or user's contact id
100          * @param int $uid User ID
101          *
102          * @return boolean is the contact id blocked for the given user?
103          * @throws \Exception
104          */
105         public static function isBlocked($cid, $uid)
106         {
107                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
108                 if (empty($cdata)) {
109                         return false;
110                 }
111
112                 $public_blocked = false;
113
114                 if (!empty($cdata['public'])) {
115                         $public_contact = DBA::selectFirst('user-contact', ['blocked'], ['cid' => $cdata['public'], 'uid' => $uid]);
116                         if (DBA::isResult($public_contact)) {
117                                 $public_blocked = $public_contact['blocked'];
118                         }
119                 }
120
121                 $user_blocked = $public_blocked;
122
123                 if (!empty($cdata['user'])) {
124                         $user_contact = DBA::selectFirst('contact', ['blocked'], ['id' => $cdata['user'], 'pending' => false]);
125                         if (DBA::isResult($user_contact)) {
126                                 $user_blocked = $user_contact['blocked'];
127                         }
128                 }
129
130                 if ($user_blocked != $public_blocked) {
131                         DBA::update('user-contact', ['blocked' => $user_blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
132                 }
133
134                 return $user_blocked;
135         }
136
137         /**
138          * Ignore contact id for user id
139          *
140          * @param int     $cid     Either public contact id or user's contact id
141          * @param int     $uid     User ID
142          * @param boolean $ignored Is the contact ignored or unignored?
143          * @throws \Exception
144          */
145         public static function setIgnored($cid, $uid, $ignored)
146         {
147                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
148                 if (empty($cdata)) {
149                         return;
150                 }
151
152                 if ($cdata['user'] != 0) {
153                         DBA::update('contact', ['readonly' => $ignored], ['id' => $cdata['user'], 'pending' => false]);
154                 }
155
156                 DBA::update('user-contact', ['ignored' => $ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
157         }
158
159         /**
160          * Returns "ignore" state for contact id and user id
161          *
162          * @param int $cid Either public contact id or user's contact id
163          * @param int $uid User ID
164          *
165          * @return boolean is the contact id ignored for the given user?
166          * @throws \Exception
167          */
168         public static function isIgnored($cid, $uid)
169         {
170                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
171                 if (empty($cdata)) {
172                         return false;
173                 }
174
175                 $public_ignored = false;
176
177                 if (!empty($cdata['public'])) {
178                         $public_contact = DBA::selectFirst('user-contact', ['ignored'], ['cid' => $cdata['public'], 'uid' => $uid]);
179                         if (DBA::isResult($public_contact)) {
180                                 $public_ignored = $public_contact['ignored'];
181                         }
182                 }
183
184                 $user_ignored = $public_ignored;
185
186                 if (!empty($cdata['user'])) {
187                         $user_contact = DBA::selectFirst('contact', ['readonly'], ['id' => $cdata['user'], 'pending' => false]);
188                         if (DBA::isResult($user_contact)) {
189                                 $user_ignored = $user_contact['readonly'];
190                         }
191                 }
192
193                 if ($user_ignored != $public_ignored) {
194                         DBA::update('user-contact', ['ignored' => $user_ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
195                 }
196
197                 return $user_ignored;
198         }
199
200         /**
201          * Set "collapsed" for contact id and user id
202          *
203          * @param int     $cid       Either public contact id or user's contact id
204          * @param int     $uid       User ID
205          * @param boolean $collapsed are the contact's posts collapsed or uncollapsed?
206          * @throws \Exception
207          */
208         public static function setCollapsed($cid, $uid, $collapsed)
209         {
210                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
211                 if (empty($cdata)) {
212                         return;
213                 }
214
215                 DBA::update('user-contact', ['collapsed' => $collapsed], ['cid' => $cdata['public'], 'uid' => $uid], true);
216         }
217
218         /**
219          * Returns "collapsed" 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 blocked for the given user?
225          * @throws HTTPException\InternalServerErrorException
226          * @throws \ImagickException
227          */
228         public static function isCollapsed($cid, $uid)
229         {
230                 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
231                 if (empty($cdata)) {
232                         return;
233                 }
234
235                 $collapsed = false;
236
237                 if (!empty($cdata['public'])) {
238                         $public_contact = DBA::selectFirst('user-contact', ['collapsed'], ['cid' => $cdata['public'], 'uid' => $uid]);
239                         if (DBA::isResult($public_contact)) {
240                                 $collapsed = $public_contact['collapsed'];
241                         }
242                 }
243
244                 return $collapsed;
245         }
246 }