3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Model;
25 use Friendica\Core\Protocol;
26 use Friendica\Database\DBA;
27 use Friendica\Util\DateTimeFormat;
30 * This class handles GlobalContact related functions
35 * @param integer $uid id
36 * @param integer $cid id
40 public static function countCommonFriends($uid, $cid)
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) ",
54 if (DBA::isResult($r)) {
55 return $r[0]['total'];
61 * @param integer $uid id
62 * @param integer $zcid zcid
66 public static function countCommonFriendsZcid($uid, $zcid)
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) ",
77 if (DBA::isResult($r)) {
78 return $r[0]['total'];
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
93 public static function commonFriends($uid, $cid, $start = 0, $limit = 9999, $shuffle = false)
96 $sql_extra = " order by rand() ";
98 $sql_extra = " order by `gcontact`.`name` asc ";
102 "SELECT `gcontact`.*, `contact`.`id` AS `cid`
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",
119 /// @TODO Check all calling-findings of this function if they properly use DBA::isResult()
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
132 public static function commonFriendsZcid($uid, $zcid, $start = 0, $limit = 9999, $shuffle = false)
135 $sql_extra = " order by rand() ";
137 $sql_extra = " order by `gcontact`.`name` asc ";
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",
152 /// @TODO Check all calling-findings of this function if they properly use DBA::isResult()
157 * @param integer $uid user
158 * @param integer $cid cid
162 public static function countAllFriends($uid, $cid)
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`",
173 if (DBA::isResult($r)) {
174 return $r[0]['total'];
181 * @param integer $uid user
182 * @param integer $cid cid
183 * @param integer $start optional, default 0
184 * @param integer $limit optional, default 80
188 public static function allFriends($uid, $cid, $start = 0, $limit = 80)
191 "SELECT `gcontact`.*, `contact`.`id` AS `cid`
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 ",
205 /// @TODO Check all calling-findings of this function if they properly use DBA::isResult()
210 * Returns a random, global contact of the current node
212 * @return string The profile URL
215 public static function getRandomUrl()
217 $r = DBA::selectFirst('gcontact', ['url'], [
222 DateTimeFormat::utc('now - 1 month'),
223 ], ['order' => ['RAND()']]);
225 if (DBA::isResult($r)) {