. * */ namespace Friendica\Model; use Exception; use Friendica\Core\Protocol; use Friendica\Database\DBA; use Friendica\Util\DateTimeFormat; /** * This class handles GlobalContact related functions */ class GContact { /** * @param integer $uid id * @param integer $cid id * @return integer * @throws Exception */ public static function countCommonFriends($uid, $cid) { $r = q( "SELECT count(*) as `total` FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id` WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND NOT `gcontact`.`failed` AND `gcontact`.`nurl` IN (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d) ", intval($cid), intval($uid), intval($uid), intval($cid) ); if (DBA::isResult($r)) { return $r[0]['total']; } return 0; } /** * @param integer $uid id * @param integer $zcid zcid * @return integer * @throws Exception */ public static function countCommonFriendsZcid($uid, $zcid) { $r = q( "SELECT count(*) as `total` FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id` where `glink`.`zcid` = %d and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0) ", intval($zcid), intval($uid) ); if (DBA::isResult($r)) { return $r[0]['total']; } return 0; } /** * @param integer $uid user * @param integer $cid cid * @param integer $start optional, default 0 * @param integer $limit optional, default 9999 * @param boolean $shuffle optional, default false * @return object * @throws Exception */ public static function commonFriends($uid, $cid, $start = 0, $limit = 9999, $shuffle = false) { if ($shuffle) { $sql_extra = " order by rand() "; } else { $sql_extra = " order by `gcontact`.`name` asc "; } $r = q( "SELECT `gcontact`.*, `contact`.`id` AS `cid` FROM `glink` INNER JOIN `gcontact` ON `glink`.`gcid` = `gcontact`.`id` INNER JOIN `contact` ON `gcontact`.`nurl` = `contact`.`nurl` WHERE `glink`.`cid` = %d and `glink`.`uid` = %d AND `contact`.`uid` = %d AND `contact`.`self` = 0 AND `contact`.`blocked` = 0 AND `contact`.`hidden` = 0 AND `contact`.`id` != %d AND NOT `gcontact`.`failed` $sql_extra LIMIT %d, %d", intval($cid), intval($uid), intval($uid), intval($cid), intval($start), intval($limit) ); /// @TODO Check all calling-findings of this function if they properly use DBA::isResult() return $r; } /** * @param integer $uid user * @param integer $zcid zcid * @param integer $start optional, default 0 * @param integer $limit optional, default 9999 * @param boolean $shuffle optional, default false * @return object * @throws Exception */ public static function commonFriendsZcid($uid, $zcid, $start = 0, $limit = 9999, $shuffle = false) { if ($shuffle) { $sql_extra = " order by rand() "; } else { $sql_extra = " order by `gcontact`.`name` asc "; } $r = q( "SELECT `gcontact`.* FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id` where `glink`.`zcid` = %d and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0) $sql_extra limit %d, %d", intval($zcid), intval($uid), intval($start), intval($limit) ); /// @TODO Check all calling-findings of this function if they properly use DBA::isResult() return $r; } /** * @param integer $uid user * @param integer $cid cid * @return integer * @throws Exception */ public static function countAllFriends($uid, $cid) { $r = q( "SELECT count(*) as `total` FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id` where `glink`.`cid` = %d and `glink`.`uid` = %d AND NOT `gcontact`.`failed`", intval($cid), intval($uid) ); if (DBA::isResult($r)) { return $r[0]['total']; } return 0; } /** * @param integer $uid user * @param integer $cid cid * @param integer $start optional, default 0 * @param integer $limit optional, default 80 * @return array * @throws Exception */ public static function allFriends($uid, $cid, $start = 0, $limit = 80) { $r = q( "SELECT `gcontact`.*, `contact`.`id` AS `cid` FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id` LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl` AND `contact`.`uid` = %d WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND NOT `gcontact`.`failed` ORDER BY `gcontact`.`name` ASC LIMIT %d, %d ", intval($uid), intval($cid), intval($uid), intval($start), intval($limit) ); /// @TODO Check all calling-findings of this function if they properly use DBA::isResult() return $r; } /** * Returns a random, global contact of the current node * * @return string The profile URL * @throws Exception */ public static function getRandomUrl() { $r = DBA::selectFirst('gcontact', ['url'], [ '`network` = ? AND NOT `failed` AND `updated` > ?', Protocol::DFRN, DateTimeFormat::utc('now - 1 month'), ], ['order' => ['RAND()']]); if (DBA::isResult($r)) { return $r['url']; } return ''; } }