return false;
}
- if (ContactRelation::isDiscoverable($ret['url'])) {
+ if (Contact\Relation::isDiscoverable($ret['url'])) {
Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']);
}
return array_slice($contacts, $start, $limit);
}
+ /**
+ * Returns a list of all common friends between two given public contact ids.
+ *
+ * @param int $cid1 first public contact id
+ * @param int $cid2 second public contact id
+ * @return int
+ */
+ public static function countContactsOfContact(int $cid)
+ {
+ return DBA::count('contact',
+ ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ?)
+ OR `id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?)",
+ $cid, $cid]
+ );
+ }
+
+ /**
+ * Returns a list of all contacts of a given public contact id.
+ *
+ * @param int $cid public contact id
+ * @param int $start optional, default 0
+ * @param int $limit optional, default 80
+ * @return array
+ */
+ public static function getContactsOfContact(int $cid, int $start = 0, int $limit = 80)
+ {
+ return DBA::selectToArray('contact', [],
+ ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ?)
+ OR `id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?)",
+ $cid, $cid],
+ ['limit' => [$start, $limit]]
+ );
+ }
+
/**
* Add public contacts from an array
*
namespace Friendica\Model;
use Exception;
-use Friendica\Core\Protocol;
use Friendica\Database\DBA;
+ use Friendica\DI;
+ use Friendica\Util\DateTimeFormat;
/**
* This class handles GlobalContact related functions
*/
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)
- );
+ $sourceId = Contact::getPublicIdByUserId($uid);
+
+ $targetIds = Contact::getPublicAndUserContacID($cid, $uid);
+
+ $condition = [
+ 'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
+ $sourceId,
+ ];
- if (DBA::isResult($r)) {
- return $r[0]['total'];
- }
- return 0;
+ return Contact\Relation::countCommonFollows($sourceId, $targetIds['public'] ?? 0, $condition);
}
/**
*/
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)
+ $sourceId = Contact::getPublicIdByUserId($uid);
+
+ $targetPublicContact = DI::dba()->fetchFirst("
+ SELECT `id`
+ FROM `contact` c
+ JOIN `gcontact` z ON z.`nurl` = c.`nurl`
+ AND z.`id` = ?
+ AND c.`uid` = 0
+ LIMIT 1",
+ $zcid
);
- if (DBA::isResult($r)) {
- return $r[0]['total'];
- }
+ $condition = [
+ 'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
+ $sourceId,
+ ];
- return 0;
+ return Contact\Relation::countCommonFollowers($sourceId, $targetPublicContact['id'] ?? 0, $condition);
}
/**
- * @param integer $uid user
- * @param integer $cid cid
+ * Returns the cross-section between the local user contacts and one of their contact's own relationships
+ * as known by the local node.
+ *
+ * @param integer $uid local user id
+ * @param integer $cid user contact id to compare friends with
* @param integer $start optional, default 0
* @param integer $limit optional, default 9999
* @param boolean $shuffle optional, default false
- * @return object
+ * @return array
* @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)
- );
+ $sourceId = Contact::getPublicIdByUserId($uid);
+
+ $targetIds = Contact::getPublicAndUserContacID($cid, $uid);
- /// @TODO Check all calling-findings of this function if they properly use DBA::isResult()
- return $r;
+ $condition = [
+ 'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
+ $sourceId,
+ ];
+
+ return Contact\Relation::listCommonFollows($sourceId, $targetIds['public'] ?? 0, $condition, $limit, $start, $shuffle);
}
/**
- * @param integer $uid user
- * @param integer $zcid zcid
+ * Returns the cross-section between a local user and a remote visitor contact's own relationships
+ * as known by the local node.
+ *
+ * @param integer $uid local user id
+ * @param integer $zcid remote visitor contact zcid
* @param integer $start optional, default 0
* @param integer $limit optional, default 9999
* @param boolean $shuffle optional, default false
- * @return object
+ * @return array
* @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)
+ $sourceId = Contact::getPublicIdByUserId($uid);
+
+ $targetPublicContact = DI::dba()->fetchFirst("
+ SELECT c.`id`
+ FROM `contact` c
+ JOIN `gcontact` z ON z.`nurl` = c.`nurl`
+ AND z.`id` = ?
+ AND c.`uid` = 0
+ LIMIT 1",
+ $zcid
);
- /// @TODO Check all calling-findings of this function if they properly use DBA::isResult()
- return $r;
+ $condition = [
+ 'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
+ $sourceId,
+ ];
+
+ return Contact\Relation::listCommonFollows($sourceId, $targetPublicContact['id'] ?? 0, $condition, $limit, $start, $shuffle);
+ }
+
+ /**
+ * @param integer $uid user
+ * @param integer $cid cid
+ * @return integer
+ * @throws Exception
+ */
+ public static function countAllFriends($uid, $cid)
+ {
+ $cids = Contact::getPublicAndUserContacID($cid, $uid);
+
+ return Contact\Relation::countFollows($cids['public'] ?? 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)
+ {
+ $cids = Contact::getPublicAndUserContacID($cid, $uid);
+
+ return Contact\Relation::listFollows($cids['public'] ?? 0, [], $limit, $start);
}
}