3 * @copyright Copyright (C) 2010-2022, the Friendica project
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\Contact;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Protocol;
27 use Friendica\Database\Database;
28 use Friendica\Database\DBA;
30 use Friendica\Model\APContact;
31 use Friendica\Model\Contact;
32 use Friendica\Model\Profile;
33 use Friendica\Model\User;
34 use Friendica\Protocol\ActivityPub;
35 use Friendica\Util\DateTimeFormat;
36 use Friendica\Util\Strings;
39 * This class provides relationship information based on the `contact-relation` table.
40 * This table is directional (cid = source, relation-cid = target), references public contacts (with uid=0) and records both
41 * follows and the last interaction (likes/comments) on public posts.
46 * No discovery of followers/followings
48 const DISCOVERY_NONE = 0;
50 * Discover followers/followings of local contacts
52 const DISCOVERY_LOCAL = 1;
54 * Discover followers/followings of local contacts and contacts that visibly interacted on the system
56 const DISCOVERY_INTERACTOR = 2;
58 * Discover followers/followings of all contacts
60 const DISCOVERY_ALL = 3;
62 public static function store(int $target, int $actor, string $interaction_date)
64 if ($actor == $target) {
68 DBA::insert('contact-relation', ['last-interaction' => $interaction_date, 'cid' => $target, 'relation-cid' => $actor], Database::INSERT_UPDATE);
72 * Fetch the followers of a given user
74 * @param integer $uid User ID
77 public static function discoverByUser(int $uid)
79 $contact = Contact::selectFirst(['id', 'url'], ['uid' => $uid, 'self' => true]);
80 if (empty($contact)) {
81 Logger::warning('Self contact for user not found', ['uid' => $uid]);
85 $followers = self::getContacts($uid, [Contact::FOLLOWER, Contact::FRIEND]);
86 $followings = self::getContacts($uid, [Contact::SHARING, Contact::FRIEND]);
88 self::updateFollowersFollowings($contact, $followers, $followings);
92 * Fetches the followers of a given profile and adds them
94 * @param string $url URL of a profile
97 public static function discoverByUrl(string $url)
99 $contact = Contact::getByURL($url);
100 if (empty($contact)) {
101 Logger::info('Contact not found', ['url' => $url]);
105 if (!self::isDiscoverable($url, $contact)) {
106 Logger::info('Contact is not discoverable', ['url' => $url]);
110 $uid = User::getIdForURL($url);
112 Logger::info('Fetch the followers/followings locally', ['url' => $url]);
113 $followers = self::getContacts($uid, [Contact::FOLLOWER, Contact::FRIEND]);
114 $followings = self::getContacts($uid, [Contact::SHARING, Contact::FRIEND]);
115 } elseif (!Contact::isLocal($url)) {
116 Logger::info('Fetch the followers/followings by polling the endpoints', ['url' => $url]);
117 $apcontact = APContact::getByURL($url, false);
119 if (!empty($apcontact['followers']) && is_string($apcontact['followers'])) {
120 $followers = ActivityPub::fetchItems($apcontact['followers']);
125 if (!empty($apcontact['following']) && is_string($apcontact['following'])) {
126 $followings = ActivityPub::fetchItems($apcontact['following']);
131 Logger::warning('Contact seems to be local but could not be found here', ['url' => $url]);
136 self::updateFollowersFollowings($contact, $followers, $followings);
140 * Update followers and followings for the given contact
142 * @param array $contact
143 * @param array $followers
144 * @param array $followings
147 private static function updateFollowersFollowings(array $contact, array $followers, array $followings)
149 if (empty($followers) && empty($followings)) {
150 Contact::update(['last-discovery' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
151 Logger::info('The contact does not offer discoverable data', ['id' => $contact['id'], 'url' => $contact['url'], 'network' => $contact['network']]);
155 $target = $contact['id'];
156 $url = $contact['url'];
158 if (!empty($followers)) {
159 // Clear the follower list, since it will be recreated in the next step
160 DBA::update('contact-relation', ['follows' => false], ['cid' => $target]);
164 foreach (array_merge($followers, $followings) as $contact) {
165 if (is_string($contact)) {
166 $contacts[] = $contact;
167 } elseif (!empty($contact['url']) && is_string($contact['url'])) {
168 $contacts[] = $contact['url'];
171 $contacts = array_unique($contacts);
173 $follower_counter = 0;
174 $following_counter = 0;
176 Logger::info('Discover contacts', ['id' => $target, 'url' => $url, 'contacts' => count($contacts)]);
177 foreach ($contacts as $contact) {
178 $actor = Contact::getIdForURL($contact);
179 if (!empty($actor)) {
180 if (in_array($contact, $followers)) {
181 $fields = ['cid' => $target, 'relation-cid' => $actor, 'follows' => true, 'follow-updated' => DateTimeFormat::utcNow()];
182 DBA::insert('contact-relation', $fields, Database::INSERT_UPDATE);
186 if (in_array($contact, $followings)) {
187 $fields = ['cid' => $actor, 'relation-cid' => $target, 'follows' => true, 'follow-updated' => DateTimeFormat::utcNow()];
188 DBA::insert('contact-relation', $fields, Database::INSERT_UPDATE);
189 $following_counter++;
194 if (!empty($followers)) {
195 // Delete all followers that aren't followers anymore (and aren't interacting)
196 DBA::delete('contact-relation', ['cid' => $target, 'follows' => false, 'last-interaction' => DBA::NULL_DATETIME]);
199 Contact::update(['last-discovery' => DateTimeFormat::utcNow()], ['id' => $target]);
200 Logger::info('Contacts discovery finished', ['id' => $target, 'url' => $url, 'follower' => $follower_counter, 'following' => $following_counter]);
205 * Fetch contact url list from the given local user
207 * @param integer $uid
209 * @return array contact list
211 private static function getContacts(int $uid, array $rel): array
214 $profile = Profile::getByUID($uid);
215 if (!empty($profile['hide-friends'])) {
228 $condition = DBA::mergeConditions($condition, ["`url` IN (SELECT `url` FROM `apcontact`)"]);
229 $contacts = DBA::select('contact', ['url'], $condition);
230 while ($contact = DBA::fetch($contacts)) {
231 $list[] = $contact['url'];
233 DBA::close($contacts);
239 * Tests if a given contact url is discoverable
241 * @param string $url Contact url
242 * @param array $contact Contact array
243 * @return boolean True if contact is discoverable
245 public static function isDiscoverable(string $url, array $contact = []): bool
247 $contact_discovery = DI::config()->get('system', 'contact_discovery');
249 if ($contact_discovery == self::DISCOVERY_NONE) {
253 if (empty($contact)) {
254 $contact = Contact::getByURL($url, false);
257 if (empty($contact)) {
261 if ($contact['last-discovery'] > DateTimeFormat::utc('now - 1 month')) {
262 Logger::info('No discovery - Last was less than a month ago.', ['id' => $contact['id'], 'url' => $url, 'discovery' => $contact['last-discovery']]);
266 if ($contact_discovery != self::DISCOVERY_ALL) {
267 $local = DBA::exists('contact', ["`nurl` = ? AND `uid` != ?", Strings::normaliseLink($url), 0]);
268 if (($contact_discovery == self::DISCOVERY_LOCAL) && !$local) {
269 Logger::info('No discovery - This contact is not followed/following locally.', ['id' => $contact['id'], 'url' => $url]);
273 if ($contact_discovery == self::DISCOVERY_INTERACTOR) {
274 $interactor = DBA::exists('contact-relation', ["`relation-cid` = ? AND `last-interaction` > ?", $contact['id'], DBA::NULL_DATETIME]);
275 if (!$local && !$interactor) {
276 Logger::info('No discovery - This contact is not interacting locally.', ['id' => $contact['id'], 'url' => $url]);
280 } elseif ($contact['created'] > DateTimeFormat::utc('now - 1 day')) {
281 // Newly created contacts are not discovered to avoid DDoS attacks
282 Logger::info('No discovery - Contact record is less than a day old.', ['id' => $contact['id'], 'url' => $url, 'discovery' => $contact['created']]);
286 if (!in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS])) {
287 $apcontact = APContact::getByURL($url, false);
288 if (empty($apcontact)) {
289 Logger::info('No discovery - The contact does not seem to speak ActivityPub.', ['id' => $contact['id'], 'url' => $url, 'network' => $contact['network']]);
298 * Check if the cached suggestion is outdated
300 * @param integer $uid
303 static public function areSuggestionsOutdated(int $uid): bool
305 return DI::pConfig()->get($uid, 'suggestion', 'last_update') + 3600 < time();
309 * Update contact suggestions for a given user
311 * @param integer $uid
314 static public function updateCachedSuggestions(int $uid)
316 if (!self::areSuggestionsOutdated($uid)) {
320 DBA::delete('account-suggestion', ['uid' => $uid, 'ignore' => false]);
322 foreach (self::getSuggestions($uid) as $contact) {
323 DBA::insert('account-suggestion', ['uri-id' => $contact['uri-id'], 'uid' => $uid, 'level' => 1], Database::INSERT_IGNORE);
326 DI::pConfig()->set($uid, 'suggestion', 'last_update', time());
330 * Returns a cached array of suggested contacts for given user id
332 * @param int $uid User id
333 * @param int $start optional, default 0
334 * @param int $limit optional, default 80
337 static public function getCachedSuggestions(int $uid, int $start = 0, int $limit = 80): array
339 $condition = ["`uid` = ? AND `uri-id` IN (SELECT `uri-id` FROM `account-suggestion` WHERE NOT `ignore` AND `uid` = ?)", 0, $uid];
340 $params = ['limit' => [$start, $limit]];
341 $cached = DBA::selectToArray('contact', [], $condition, $params);
343 if (!empty($cached)) {
346 return self::getSuggestions($uid, $start, $limit);
351 * Returns an array of suggested contacts for given user id
353 * @param int $uid User id
354 * @param int $start optional, default 0
355 * @param int $limit optional, default 80
358 static public function getSuggestions(int $uid, int $start = 0, int $limit = 80): array
364 $cid = Contact::getPublicIdByUserId($uid);
365 $totallimit = $start + $limit;
368 Logger::info('Collecting suggestions', ['uid' => $uid, 'cid' => $cid, 'start' => $start, 'limit' => $limit]);
370 $diaspora = DI::config()->get('system', 'diaspora_enabled') ? Protocol::DIASPORA : Protocol::ACTIVITYPUB;
371 $ostatus = !DI::config()->get('system', 'ostatus_disabled') ? Protocol::OSTATUS : Protocol::ACTIVITYPUB;
373 // The query returns contacts where contacts interacted with whom the given user follows.
374 // Contacts who already are in the user's contact table are ignored.
375 $results = DBA::select('contact', [], ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` IN
376 (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ?)
377 AND NOT `cid` IN (SELECT `id` FROM `contact` WHERE `uid` = ? AND `nurl` IN
378 (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))) AND `id` = `cid`)
379 AND NOT `hidden` AND `network` IN (?, ?, ?, ?)
380 AND NOT `uri-id` IN (SELECT `uri-id` FROM `account-suggestion` WHERE `uri-id` = `contact`.`uri-id` AND `uid` = ?)",
383 $uid, Contact::FRIEND, Contact::SHARING,
384 Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus, $uid
386 'order' => ['last-item' => true],
387 'limit' => $totallimit,
391 while ($contact = DBA::fetch($results)) {
392 $contacts[$contact['id']] = $contact;
395 DBA::close($results);
397 Logger::info('Contacts of contacts who are followed by the given user', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
399 if (count($contacts) >= $totallimit) {
400 return array_slice($contacts, $start, $limit);
403 // The query returns contacts where contacts interacted with whom also interacted with the given user.
404 // Contacts who already are in the user's contact table are ignored.
405 $results = DBA::select('contact', [],
406 ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` IN
407 (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?)
408 AND NOT `cid` IN (SELECT `id` FROM `contact` WHERE `uid` = ? AND `nurl` IN
409 (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))) AND `id` = `cid`)
410 AND NOT `hidden` AND `network` IN (?, ?, ?, ?)
411 AND NOT `uri-id` IN (SELECT `uri-id` FROM `account-suggestion` WHERE `uri-id` = `contact`.`uri-id` AND `uid` = ?)",
412 $cid, 0, $uid, Contact::FRIEND, Contact::SHARING,
413 Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus, $uid],
414 ['order' => ['last-item' => true], 'limit' => $totallimit]
417 while ($contact = DBA::fetch($results)) {
418 $contacts[$contact['id']] = $contact;
420 DBA::close($results);
422 Logger::info('Contacts of contacts who are following the given user', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
424 if (count($contacts) >= $totallimit) {
425 return array_slice($contacts, $start, $limit);
428 // The query returns contacts that follow the given user but aren't followed by that user.
429 $results = DBA::select('contact', [],
430 ["`nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` = ?)
431 AND NOT `hidden` AND `uid` = ? AND `network` IN (?, ?, ?, ?)
432 AND NOT `uri-id` IN (SELECT `uri-id` FROM `account-suggestion` WHERE `uri-id` = `contact`.`uri-id` AND `uid` = ?)",
433 $uid, Contact::FOLLOWER, 0,
434 Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus, $uid],
435 ['order' => ['last-item' => true], 'limit' => $totallimit]
438 while ($contact = DBA::fetch($results)) {
439 $contacts[$contact['id']] = $contact;
441 DBA::close($results);
443 Logger::info('Followers that are not followed by the given user', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
445 if (count($contacts) >= $totallimit) {
446 return array_slice($contacts, $start, $limit);
449 // The query returns any contact that isn't followed by that user.
450 $results = DBA::select('contact', [],
451 ["NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?) AND `nurl` = `nurl`)
452 AND NOT `hidden` AND `uid` = ? AND `network` IN (?, ?, ?, ?)
453 AND NOT `uri-id` IN (SELECT `uri-id` FROM `account-suggestion` WHERE `uri-id` = `contact`.`uri-id` AND `uid` = ?)",
454 $uid, Contact::FRIEND, Contact::SHARING, 0,
455 Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus, $uid],
456 ['order' => ['last-item' => true], 'limit' => $totallimit]
459 while ($contact = DBA::fetch($results)) {
460 $contacts[$contact['id']] = $contact;
462 DBA::close($results);
464 Logger::info('Any contact', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
466 return array_slice($contacts, $start, $limit);
470 * Counts all the known follows of the provided public contact
472 * @param int $cid Public contact id
473 * @param array $condition Additional condition on the contact table
477 public static function countFollows(int $cid, array $condition = []): int
479 $condition = DBA::mergeConditions($condition, [
480 '`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)',
484 return DI::dba()->count('contact', $condition);
488 * Returns a paginated list of contacts that are followed the provided public contact.
490 * @param int $cid Public contact id
491 * @param array $condition Additional condition on the contact table
494 * @param bool $shuffle
498 public static function listFollows(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
500 $condition = DBA::mergeConditions($condition,
501 ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)',
505 return DI::dba()->selectToArray('contact', [], $condition,
506 ['limit' => [$offset, $count], 'order' => [$shuffle ? 'RAND()' : 'name']]
511 * Counts all the known followers of the provided public contact
513 * @param int $cid Public contact id
514 * @param array $condition Additional condition on the contact table
518 public static function countFollowers(int $cid, array $condition = [])
520 $condition = DBA::mergeConditions($condition,
521 ['`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)',
525 return DI::dba()->count('contact', $condition);
529 * Returns a paginated list of contacts that follow the provided public contact.
531 * @param int $cid Public contact id
532 * @param array $condition Additional condition on the contact table
535 * @param bool $shuffle
539 public static function listFollowers(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
541 $condition = DBA::mergeConditions($condition,
542 ['`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)', $cid]
545 return DI::dba()->selectToArray('contact', [], $condition,
546 ['limit' => [$offset, $count], 'order' => [$shuffle ? 'RAND()' : 'name']]
551 * Counts the number of contacts that are known mutuals with the provided public contact.
553 * @param int $cid Public contact id
554 * @param array $condition Additional condition array on the contact table
558 public static function countMutuals(int $cid, array $condition = [])
560 $condition = DBA::mergeConditions($condition,
561 ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)
562 AND `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)',
566 return DI::dba()->count('contact', $condition);
570 * Returns a paginated list of contacts that are known mutuals with the provided public contact.
572 * @param int $cid Public contact id
573 * @param array $condition Additional condition on the contact table
576 * @param bool $shuffle
580 public static function listMutuals(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
582 $condition = DBA::mergeConditions($condition,
583 ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)
584 AND `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)',
588 return DI::dba()->selectToArray('contact', [], $condition,
589 ['limit' => [$offset, $count], 'order' => [$shuffle ? 'RAND()' : 'name']]
595 * Counts the number of contacts with any relationship with the provided public contact.
597 * @param int $cid Public contact id
598 * @param array $condition Additional condition array on the contact table
602 public static function countAll(int $cid, array $condition = [])
604 $condition = DBA::mergeConditions($condition,
605 ['(`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)
606 OR `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`))',
610 return DI::dba()->count('contact', $condition);
614 * Returns a paginated list of contacts with any relationship with the provided public contact.
616 * @param int $cid Public contact id
617 * @param array $condition Additional condition on the contact table
620 * @param bool $shuffle
624 public static function listAll(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
626 $condition = DBA::mergeConditions($condition,
627 ['(`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)
628 OR `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`))',
632 return DI::dba()->selectToArray('contact', [], $condition,
633 ['limit' => [$offset, $count], 'order' => [$shuffle ? 'RAND()' : 'name']]
638 * Counts the number of contacts that both provided public contacts have interacted with at least once.
639 * Interactions include follows and likes and comments on public posts.
641 * @param int $sourceId Public contact id
642 * @param int $targetId Public contact id
643 * @param array $condition Additional condition array on the contact table
647 public static function countCommon(int $sourceId, int $targetId, array $condition = [])
649 $condition = DBA::mergeConditions($condition,
650 ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?)
651 AND `id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?)',
652 $sourceId, $targetId]
655 return DI::dba()->count('contact', $condition);
659 * Returns a paginated list of contacts that both provided public contacts have interacted with at least once.
660 * Interactions include follows and likes and comments on public posts.
662 * @param int $sourceId Public contact id
663 * @param int $targetId Public contact id
664 * @param array $condition Additional condition on the contact table
667 * @param bool $shuffle
668 * @return array|bool Array on success, false on failure
671 public static function listCommon(int $sourceId, int $targetId, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
673 $condition = DBA::mergeConditions($condition,
674 ["`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?)
675 AND `id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?)",
676 $sourceId, $targetId]
679 return DI::dba()->selectToArray('contact', [], $condition,
680 ['limit' => [$offset, $count], 'order' => [$shuffle ? 'RAND()' : 'name']]
685 * Counts the number of contacts that are followed by both provided public contacts.
687 * @param int $sourceId Public contact id
688 * @param int $targetId Public contact id
689 * @param array $condition Additional condition array on the contact table
693 public static function countCommonFollows(int $sourceId, int $targetId, array $condition = []): int
695 $condition = DBA::mergeConditions($condition,
696 ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)
697 AND `id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)',
698 $sourceId, $targetId]
701 return DI::dba()->count('contact', $condition);
705 * Returns a paginated list of contacts that are followed by both provided public contacts.
707 * @param int $sourceId Public contact id
708 * @param int $targetId Public contact id
709 * @param array $condition Additional condition array on the contact table
712 * @param bool $shuffle
713 * @return array|bool Array on success, false on failure
716 public static function listCommonFollows(int $sourceId, int $targetId, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
718 $condition = DBA::mergeConditions($condition,
719 ["`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)
720 AND `id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)",
721 $sourceId, $targetId]
724 return DI::dba()->selectToArray('contact', [], $condition,
725 ['limit' => [$offset, $count], 'order' => [$shuffle ? 'RAND()' : 'name']]
730 * Counts the number of contacts that follow both provided public contacts.
732 * @param int $sourceId Public contact id
733 * @param int $targetId Public contact id
734 * @param array $condition Additional condition on the contact table
738 public static function countCommonFollowers(int $sourceId, int $targetId, array $condition = []): int
740 $condition = DBA::mergeConditions($condition,
741 ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)
742 AND `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)",
743 $sourceId, $targetId]
746 return DI::dba()->count('contact', $condition);
750 * Returns a paginated list of contacts that follow both provided public contacts.
752 * @param int $sourceId Public contact id
753 * @param int $targetId Public contact id
754 * @param array $condition Additional condition on the contact table
757 * @param bool $shuffle
758 * @return array|bool Array on success, false on failure
761 public static function listCommonFollowers(int $sourceId, int $targetId, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
763 $condition = DBA::mergeConditions($condition,
764 ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)
765 AND `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)",
766 $sourceId, $targetId]
769 return DI::dba()->selectToArray('contact', [], $condition,
770 ['limit' => [$offset, $count], 'order' => [$shuffle ? 'RAND()' : 'name']]