]> git.mxchange.org Git - friendica.git/blob - src/Model/GContact.php
6549e9efe23de0dbc410ec1340e66593f20845e4
[friendica.git] / src / Model / GContact.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;
23
24 use Exception;
25 use Friendica\Core\Protocol;
26 use Friendica\Database\DBA;
27 use Friendica\Util\DateTimeFormat;
28
29 /**
30  * This class handles GlobalContact related functions
31  */
32 class GContact
33 {
34         /**
35          * @param integer $uid id
36          * @param integer $cid id
37          * @return integer
38          * @throws Exception
39          */
40         public static function countCommonFriends($uid, $cid)
41         {
42                 $r = q(
43                         "SELECT count(*) as `total`
44                         FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
45                         WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND
46                         NOT `gcontact`.`failed`
47                         AND `gcontact`.`nurl` IN (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d) ",
48                         intval($cid),
49                         intval($uid),
50                         intval($uid),
51                         intval($cid)
52                 );
53
54                 if (DBA::isResult($r)) {
55                         return $r[0]['total'];
56                 }
57                 return 0;
58         }
59
60         /**
61          * @param integer $uid  id
62          * @param integer $zcid zcid
63          * @return integer
64          * @throws Exception
65          */
66         public static function countCommonFriendsZcid($uid, $zcid)
67         {
68                 $r = q(
69                         "SELECT count(*) as `total`
70                         FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
71                         where `glink`.`zcid` = %d
72                         and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0) ",
73                         intval($zcid),
74                         intval($uid)
75                 );
76
77                 if (DBA::isResult($r)) {
78                         return $r[0]['total'];
79                 }
80
81                 return 0;
82         }
83
84         /**
85          * @param integer $uid     user
86          * @param integer $cid     cid
87          * @param integer $start   optional, default 0
88          * @param integer $limit   optional, default 9999
89          * @param boolean $shuffle optional, default false
90          * @return object
91          * @throws Exception
92          */
93         public static function commonFriends($uid, $cid, $start = 0, $limit = 9999, $shuffle = false)
94         {
95                 if ($shuffle) {
96                         $sql_extra = " order by rand() ";
97                 } else {
98                         $sql_extra = " order by `gcontact`.`name` asc ";
99                 }
100
101                 $r = q(
102                         "SELECT `gcontact`.*, `contact`.`id` AS `cid`
103                         FROM `glink`
104                         INNER JOIN `gcontact` ON `glink`.`gcid` = `gcontact`.`id`
105                         INNER JOIN `contact` ON `gcontact`.`nurl` = `contact`.`nurl`
106                         WHERE `glink`.`cid` = %d and `glink`.`uid` = %d
107                                 AND `contact`.`uid` = %d AND `contact`.`self` = 0 AND `contact`.`blocked` = 0
108                                 AND `contact`.`hidden` = 0 AND `contact`.`id` != %d
109                                 AND NOT `gcontact`.`failed`
110                                 $sql_extra LIMIT %d, %d",
111                         intval($cid),
112                         intval($uid),
113                         intval($uid),
114                         intval($cid),
115                         intval($start),
116                         intval($limit)
117                 );
118
119                 /// @TODO Check all calling-findings of this function if they properly use DBA::isResult()
120                 return $r;
121         }
122
123         /**
124          * @param integer $uid     user
125          * @param integer $zcid    zcid
126          * @param integer $start   optional, default 0
127          * @param integer $limit   optional, default 9999
128          * @param boolean $shuffle optional, default false
129          * @return object
130          * @throws Exception
131          */
132         public static function commonFriendsZcid($uid, $zcid, $start = 0, $limit = 9999, $shuffle = false)
133         {
134                 if ($shuffle) {
135                         $sql_extra = " order by rand() ";
136                 } else {
137                         $sql_extra = " order by `gcontact`.`name` asc ";
138                 }
139
140                 $r = q(
141                         "SELECT `gcontact`.*
142                         FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
143                         where `glink`.`zcid` = %d
144                         and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0)
145                         $sql_extra limit %d, %d",
146                         intval($zcid),
147                         intval($uid),
148                         intval($start),
149                         intval($limit)
150                 );
151
152                 /// @TODO Check all calling-findings of this function if they properly use DBA::isResult()
153                 return $r;
154         }
155
156         /**
157          * @param integer $uid user
158          * @param integer $cid cid
159          * @return integer
160          * @throws Exception
161          */
162         public static function countAllFriends($uid, $cid)
163         {
164                 $r = q(
165                         "SELECT count(*) as `total`
166                         FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
167                         where `glink`.`cid` = %d and `glink`.`uid` = %d AND
168                         NOT `gcontact`.`failed`",
169                         intval($cid),
170                         intval($uid)
171                 );
172
173                 if (DBA::isResult($r)) {
174                         return $r[0]['total'];
175                 }
176
177                 return 0;
178         }
179
180         /**
181          * @param integer $uid   user
182          * @param integer $cid   cid
183          * @param integer $start optional, default 0
184          * @param integer $limit optional, default 80
185          * @return array
186          * @throws Exception
187          */
188         public static function allFriends($uid, $cid, $start = 0, $limit = 80)
189         {
190                 $r = q(
191                         "SELECT `gcontact`.*, `contact`.`id` AS `cid`
192                         FROM `glink`
193                         INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
194                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl` AND `contact`.`uid` = %d
195                         WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND
196                         NOT `gcontact`.`failed`
197                         ORDER BY `gcontact`.`name` ASC LIMIT %d, %d ",
198                         intval($uid),
199                         intval($cid),
200                         intval($uid),
201                         intval($start),
202                         intval($limit)
203                 );
204
205                 /// @TODO Check all calling-findings of this function if they properly use DBA::isResult()
206                 return $r;
207         }
208
209         /**
210          * Returns a random, global contact of the current node
211          *
212          * @return string The profile URL
213          * @throws Exception
214          */
215         public static function getRandomUrl()
216         {
217                 $r = DBA::selectFirst('gcontact', ['url'], [
218                         '`network` = ?
219                         AND NOT `failed`
220                         AND `updated` > ?',
221                         Protocol::DFRN,
222                         DateTimeFormat::utc('now - 1 month'),
223                 ], ['order' => ['RAND()']]);
224
225                 if (DBA::isResult($r)) {
226                         return $r['url'];
227                 }
228
229                 return '';
230         }
231 }