]> git.mxchange.org Git - friendica.git/blob - src/Model/Contact/User.php
Merge pull request #8900 from tobiasd/20200718-serverblocklistcsv
[friendica.git] / src / Model / Contact / User.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Database\DBA;
25 use Friendica\Model\Contact;
26
27 /**
28  * This class provides information about user related contacts based on the "user-contact" table.
29  */
30 class User
31 {
32         /**
33          * Block contact id for user id
34          *
35          * @param int     $cid     Either public contact id or user's contact id
36          * @param int     $uid     User ID
37          * @param boolean $blocked Is the contact blocked or unblocked?
38          * @throws \Exception
39          */
40         public static function setBlocked($cid, $uid, $blocked)
41         {
42                 $cdata = Contact::getPublicAndUserContacID($cid, $uid);
43                 if (empty($cdata)) {
44                         return;
45                 }
46
47                 if ($cdata['user'] != 0) {
48                         DBA::update('contact', ['blocked' => $blocked], ['id' => $cdata['user'], 'pending' => false]);
49                 }
50
51                 DBA::update('user-contact', ['blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
52         }
53
54         /**
55          * Returns "block" state for contact id and user id
56          *
57          * @param int $cid Either public contact id or user's contact id
58          * @param int $uid User ID
59          *
60          * @return boolean is the contact id blocked for the given user?
61          * @throws \Exception
62          */
63         public static function isBlocked($cid, $uid)
64         {
65                 $cdata = Contact::getPublicAndUserContacID($cid, $uid);
66                 if (empty($cdata)) {
67                         return;
68                 }
69
70                 $public_blocked = false;
71
72                 if (!empty($cdata['public'])) {
73                         $public_contact = DBA::selectFirst('user-contact', ['blocked'], ['cid' => $cdata['public'], 'uid' => $uid]);
74                         if (DBA::isResult($public_contact)) {
75                                 $public_blocked = $public_contact['blocked'];
76                         }
77                 }
78
79                 $user_blocked = $public_blocked;
80
81                 if (!empty($cdata['user'])) {
82                         $user_contact = DBA::selectFirst('contact', ['blocked'], ['id' => $cdata['user'], 'pending' => false]);
83                         if (DBA::isResult($user_contact)) {
84                                 $user_blocked = $user_contact['blocked'];
85                         }
86                 }
87
88                 if ($user_blocked != $public_blocked) {
89                         DBA::update('user-contact', ['blocked' => $user_blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
90                 }
91
92                 return $user_blocked;
93         }
94
95         /**
96          * Ignore contact id for user id
97          *
98          * @param int     $cid     Either public contact id or user's contact id
99          * @param int     $uid     User ID
100          * @param boolean $ignored Is the contact ignored or unignored?
101          * @throws \Exception
102          */
103         public static function setIgnored($cid, $uid, $ignored)
104         {
105                 $cdata = Contact::getPublicAndUserContacID($cid, $uid);
106                 if (empty($cdata)) {
107                         return;
108                 }
109
110                 if ($cdata['user'] != 0) {
111                         DBA::update('contact', ['readonly' => $ignored], ['id' => $cdata['user'], 'pending' => false]);
112                 }
113
114                 DBA::update('user-contact', ['ignored' => $ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
115         }
116
117         /**
118          * Returns "ignore" state for contact id and user id
119          *
120          * @param int $cid Either public contact id or user's contact id
121          * @param int $uid User ID
122          *
123          * @return boolean is the contact id ignored for the given user?
124          * @throws \Exception
125          */
126         public static function isIgnored($cid, $uid)
127         {
128                 $cdata = Contact::getPublicAndUserContacID($cid, $uid);
129                 if (empty($cdata)) {
130                         return;
131                 }
132
133                 $public_ignored = false;
134
135                 if (!empty($cdata['public'])) {
136                         $public_contact = DBA::selectFirst('user-contact', ['ignored'], ['cid' => $cdata['public'], 'uid' => $uid]);
137                         if (DBA::isResult($public_contact)) {
138                                 $public_ignored = $public_contact['ignored'];
139                         }
140                 }
141
142                 $user_ignored = $public_ignored;
143
144                 if (!empty($cdata['user'])) {
145                         $user_contact = DBA::selectFirst('contact', ['readonly'], ['id' => $cdata['user'], 'pending' => false]);
146                         if (DBA::isResult($user_contact)) {
147                                 $user_ignored = $user_contact['readonly'];
148                         }
149                 }
150
151                 if ($user_ignored != $public_ignored) {
152                         DBA::update('user-contact', ['ignored' => $user_ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
153                 }
154
155                 return $user_ignored;
156         }
157
158         /**
159          * Set "collapsed" for contact id and user id
160          *
161          * @param int     $cid       Either public contact id or user's contact id
162          * @param int     $uid       User ID
163          * @param boolean $collapsed are the contact's posts collapsed or uncollapsed?
164          * @throws \Exception
165          */
166         public static function setCollapsed($cid, $uid, $collapsed)
167         {
168                 $cdata = Contact::getPublicAndUserContacID($cid, $uid);
169                 if (empty($cdata)) {
170                         return;
171                 }
172
173                 DBA::update('user-contact', ['collapsed' => $collapsed], ['cid' => $cdata['public'], 'uid' => $uid], true);
174         }
175
176         /**
177          * Returns "collapsed" state for contact id and user id
178          *
179          * @param int $cid Either public contact id or user's contact id
180          * @param int $uid User ID
181          *
182          * @return boolean is the contact id blocked for the given user?
183          * @throws HTTPException\InternalServerErrorException
184          * @throws \ImagickException
185          */
186         public static function isCollapsed($cid, $uid)
187         {
188                 $cdata = Contact::getPublicAndUserContacID($cid, $uid);
189                 if (empty($cdata)) {
190                         return;
191                 }
192
193                 $collapsed = false;
194
195                 if (!empty($cdata['public'])) {
196                         $public_contact = DBA::selectFirst('user-contact', ['collapsed'], ['cid' => $cdata['public'], 'uid' => $uid]);
197                         if (DBA::isResult($public_contact)) {
198                                 $collapsed = $public_contact['collapsed'];
199                         }
200                 }
201
202                 return $collapsed;
203         }
204 }