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 * Fetches the followers of a given profile and adds them
74 * @param string $url URL of a profile
77 public static function discoverByUrl(string $url)
79 $contact = Contact::getByURL($url);
80 if (empty($contact)) {
81 Logger::info('Contact not found', ['url' => $url]);
85 if (!self::isDiscoverable($url, $contact)) {
86 Logger::info('Contact is not discoverable', ['url' => $url]);
90 $uid = User::getIdForURL($url);
92 Logger::info('Fetch the followers/followings locally', ['url' => $url]);
93 $followers = self::getContacts($uid, [Contact::FOLLOWER, Contact::FRIEND]);
94 $followings = self::getContacts($uid, [Contact::SHARING, Contact::FRIEND]);
95 } elseif (!Contact::isLocal($url)) {
96 Logger::info('Fetch the followers/followings by polling the endpoints', ['url' => $url]);
97 $apcontact = APContact::getByURL($url, false);
99 if (!empty($apcontact['followers']) && is_string($apcontact['followers'])) {
100 $followers = ActivityPub::fetchItems($apcontact['followers']);
105 if (!empty($apcontact['following']) && is_string($apcontact['following'])) {
106 $followings = ActivityPub::fetchItems($apcontact['following']);
111 Logger::warning('Contact seems to be local but could not be found here', ['url' => $url]);
116 if (empty($followers) && empty($followings)) {
117 Contact::update(['last-discovery' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
118 Logger::info('The contact does not offer discoverable data', ['id' => $contact['id'], 'url' => $url, 'network' => $contact['network']]);
122 $target = $contact['id'];
124 if (!empty($followers)) {
125 // Clear the follower list, since it will be recreated in the next step
126 DBA::update('contact-relation', ['follows' => false], ['cid' => $target]);
130 foreach (array_merge($followers, $followings) as $contact) {
131 if (is_string($contact)) {
132 $contacts[] = $contact;
133 } elseif (!empty($contact['url']) && is_string($contact['url'])) {
134 $contacts[] = $contact['url'];
137 $contacts = array_unique($contacts);
139 $follower_counter = 0;
140 $following_counter = 0;
142 Logger::info('Discover contacts', ['id' => $target, 'url' => $url, 'contacts' => count($contacts)]);
143 foreach ($contacts as $contact) {
144 $actor = Contact::getIdForURL($contact);
145 if (!empty($actor)) {
146 if (in_array($contact, $followers)) {
147 $fields = ['cid' => $target, 'relation-cid' => $actor, 'follows' => true, 'follow-updated' => DateTimeFormat::utcNow()];
148 DBA::insert('contact-relation', $fields, Database::INSERT_UPDATE);
152 if (in_array($contact, $followings)) {
153 $fields = ['cid' => $actor, 'relation-cid' => $target, 'follows' => true, 'follow-updated' => DateTimeFormat::utcNow()];
154 DBA::insert('contact-relation', $fields, Database::INSERT_UPDATE);
155 $following_counter++;
160 if (!empty($followers)) {
161 // Delete all followers that aren't followers anymore (and aren't interacting)
162 DBA::delete('contact-relation', ['cid' => $target, 'follows' => false, 'last-interaction' => DBA::NULL_DATETIME]);
165 Contact::update(['last-discovery' => DateTimeFormat::utcNow()], ['id' => $target]);
166 Logger::info('Contacts discovery finished', ['id' => $target, 'url' => $url, 'follower' => $follower_counter, 'following' => $following_counter]);
171 * Fetch contact url list from the given local user
173 * @param integer $uid
175 * @return array contact list
177 private static function getContacts(int $uid, array $rel): array
180 $profile = Profile::getByUID($uid);
181 if (!empty($profile['hide-friends'])) {
194 $condition = DBA::mergeConditions($condition, ["`url` IN (SELECT `url` FROM `apcontact`)"]);
195 $contacts = DBA::select('contact', ['url'], $condition);
196 while ($contact = DBA::fetch($contacts)) {
197 $list[] = $contact['url'];
199 DBA::close($contacts);
205 * Tests if a given contact url is discoverable
207 * @param string $url Contact url
208 * @param array $contact Contact array
209 * @return boolean True if contact is discoverable
211 public static function isDiscoverable(string $url, array $contact = []): bool
213 $contact_discovery = DI::config()->get('system', 'contact_discovery');
215 if ($contact_discovery == self::DISCOVERY_NONE) {
219 if (empty($contact)) {
220 $contact = Contact::getByURL($url, false);
223 if (empty($contact)) {
227 if ($contact['last-discovery'] > DateTimeFormat::utc('now - 1 month')) {
228 Logger::info('No discovery - Last was less than a month ago.', ['id' => $contact['id'], 'url' => $url, 'discovery' => $contact['last-discovery']]);
232 if ($contact_discovery != self::DISCOVERY_ALL) {
233 $local = DBA::exists('contact', ["`nurl` = ? AND `uid` != ?", Strings::normaliseLink($url), 0]);
234 if (($contact_discovery == self::DISCOVERY_LOCAL) && !$local) {
235 Logger::info('No discovery - This contact is not followed/following locally.', ['id' => $contact['id'], 'url' => $url]);
239 if ($contact_discovery == self::DISCOVERY_INTERACTOR) {
240 $interactor = DBA::exists('contact-relation', ["`relation-cid` = ? AND `last-interaction` > ?", $contact['id'], DBA::NULL_DATETIME]);
241 if (!$local && !$interactor) {
242 Logger::info('No discovery - This contact is not interacting locally.', ['id' => $contact['id'], 'url' => $url]);
246 } elseif ($contact['created'] > DateTimeFormat::utc('now - 1 day')) {
247 // Newly created contacts are not discovered to avoid DDoS attacks
248 Logger::info('No discovery - Contact record is less than a day old.', ['id' => $contact['id'], 'url' => $url, 'discovery' => $contact['created']]);
252 if (!in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS])) {
253 $apcontact = APContact::getByURL($url, false);
254 if (empty($apcontact)) {
255 Logger::info('No discovery - The contact does not seem to speak ActivityPub.', ['id' => $contact['id'], 'url' => $url, 'network' => $contact['network']]);
264 * Check if the cached suggestion is outdated
266 * @param integer $uid
269 static public function areSuggestionsOutdated(int $uid): bool
271 return DI::pConfig()->get($uid, 'suggestion', 'last_update') + 3600 < time();
275 * Update contact suggestions for a given user
277 * @param integer $uid
280 static public function updateCachedSuggestions(int $uid)
282 if (!self::areSuggestionsOutdated($uid)) {
286 DBA::delete('account-suggestion', ['uid' => $uid, 'ignore' => false]);
288 foreach (self::getSuggestions($uid) as $contact) {
289 DBA::insert('account-suggestion', ['uri-id' => $contact['uri-id'], 'uid' => $uid, 'level' => 1], Database::INSERT_IGNORE);
292 DI::pConfig()->set($uid, 'suggestion', 'last_update', time());
296 * Returns a cached array of suggested contacts for given user id
298 * @param int $uid User id
299 * @param int $start optional, default 0
300 * @param int $limit optional, default 80
303 static public function getCachedSuggestions(int $uid, int $start = 0, int $limit = 80): array
305 $condition = ["`uid` = ? AND `uri-id` IN (SELECT `uri-id` FROM `account-suggestion` WHERE NOT `ignore` AND `uid` = ?)", 0, $uid];
306 $params = ['limit' => [$start, $limit]];
307 $cached = DBA::selectToArray('contact', [], $condition, $params);
309 if (!empty($cached)) {
312 return self::getSuggestions($uid, $start, $limit);
317 * Returns an array of suggested contacts for given user id
319 * @param int $uid User id
320 * @param int $start optional, default 0
321 * @param int $limit optional, default 80
324 static public function getSuggestions(int $uid, int $start = 0, int $limit = 80): array
330 $cid = Contact::getPublicIdByUserId($uid);
331 $totallimit = $start + $limit;
334 Logger::info('Collecting suggestions', ['uid' => $uid, 'cid' => $cid, 'start' => $start, 'limit' => $limit]);
336 $diaspora = DI::config()->get('system', 'diaspora_enabled') ? Protocol::DIASPORA : Protocol::ACTIVITYPUB;
337 $ostatus = !DI::config()->get('system', 'ostatus_disabled') ? Protocol::OSTATUS : Protocol::ACTIVITYPUB;
339 // The query returns contacts where contacts interacted with whom the given user follows.
340 // Contacts who already are in the user's contact table are ignored.
341 $results = DBA::select('contact', [], ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` IN
342 (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ?)
343 AND NOT `cid` IN (SELECT `id` FROM `contact` WHERE `uid` = ? AND `nurl` IN
344 (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))) AND `id` = `cid`)
345 AND NOT `hidden` AND `network` IN (?, ?, ?, ?)
346 AND NOT `uri-id` IN (SELECT `uri-id` FROM `account-suggestion` WHERE `uri-id` = `contact`.`uri-id` AND `uid` = ?)",
349 $uid, Contact::FRIEND, Contact::SHARING,
350 Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus, $uid
352 'order' => ['last-item' => true],
353 'limit' => $totallimit,
357 while ($contact = DBA::fetch($results)) {
358 $contacts[$contact['id']] = $contact;
361 DBA::close($results);
363 Logger::info('Contacts of contacts who are followed by the given user', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
365 if (count($contacts) >= $totallimit) {
366 return array_slice($contacts, $start, $limit);
369 // The query returns contacts where contacts interacted with whom also interacted with the given user.
370 // Contacts who already are in the user's contact table are ignored.
371 $results = DBA::select('contact', [],
372 ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` IN
373 (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?)
374 AND NOT `cid` IN (SELECT `id` FROM `contact` WHERE `uid` = ? AND `nurl` IN
375 (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))) AND `id` = `cid`)
376 AND NOT `hidden` AND `network` IN (?, ?, ?, ?)
377 AND NOT `uri-id` IN (SELECT `uri-id` FROM `account-suggestion` WHERE `uri-id` = `contact`.`uri-id` AND `uid` = ?)",
378 $cid, 0, $uid, Contact::FRIEND, Contact::SHARING,
379 Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus, $uid],
380 ['order' => ['last-item' => true], 'limit' => $totallimit]
383 while ($contact = DBA::fetch($results)) {
384 $contacts[$contact['id']] = $contact;
386 DBA::close($results);
388 Logger::info('Contacts of contacts who are following the given user', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
390 if (count($contacts) >= $totallimit) {
391 return array_slice($contacts, $start, $limit);
394 // The query returns contacts that follow the given user but aren't followed by that user.
395 $results = DBA::select('contact', [],
396 ["`nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` = ?)
397 AND NOT `hidden` AND `uid` = ? AND `network` IN (?, ?, ?, ?)
398 AND NOT `uri-id` IN (SELECT `uri-id` FROM `account-suggestion` WHERE `uri-id` = `contact`.`uri-id` AND `uid` = ?)",
399 $uid, Contact::FOLLOWER, 0,
400 Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus, $uid],
401 ['order' => ['last-item' => true], 'limit' => $totallimit]
404 while ($contact = DBA::fetch($results)) {
405 $contacts[$contact['id']] = $contact;
407 DBA::close($results);
409 Logger::info('Followers that are not followed by the given user', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
411 if (count($contacts) >= $totallimit) {
412 return array_slice($contacts, $start, $limit);
415 // The query returns any contact that isn't followed by that user.
416 $results = DBA::select('contact', [],
417 ["NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?) AND `nurl` = `nurl`)
418 AND NOT `hidden` AND `uid` = ? AND `network` IN (?, ?, ?, ?)
419 AND NOT `uri-id` IN (SELECT `uri-id` FROM `account-suggestion` WHERE `uri-id` = `contact`.`uri-id` AND `uid` = ?)",
420 $uid, Contact::FRIEND, Contact::SHARING, 0,
421 Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus, $uid],
422 ['order' => ['last-item' => true], 'limit' => $totallimit]
425 while ($contact = DBA::fetch($results)) {
426 $contacts[$contact['id']] = $contact;
428 DBA::close($results);
430 Logger::info('Any contact', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
432 return array_slice($contacts, $start, $limit);
436 * Counts all the known follows of the provided public contact
438 * @param int $cid Public contact id
439 * @param array $condition Additional condition on the contact table
443 public static function countFollows(int $cid, array $condition = []): int
445 $condition = DBA::mergeConditions($condition, [
446 '`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)',
450 return DI::dba()->count('contact', $condition);
454 * Returns a paginated list of contacts that are followed the provided public contact.
456 * @param int $cid Public contact id
457 * @param array $condition Additional condition on the contact table
460 * @param bool $shuffle
464 public static function listFollows(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
466 $condition = DBA::mergeConditions($condition,
467 ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)',
471 return DI::dba()->selectToArray('contact', [], $condition,
472 ['limit' => [$offset, $count], 'order' => [$shuffle ? 'RAND()' : 'name']]
477 * Counts all the known followers of the provided public contact
479 * @param int $cid Public contact id
480 * @param array $condition Additional condition on the contact table
484 public static function countFollowers(int $cid, array $condition = [])
486 $condition = DBA::mergeConditions($condition,
487 ['`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)',
491 return DI::dba()->count('contact', $condition);
495 * Returns a paginated list of contacts that follow the provided public contact.
497 * @param int $cid Public contact id
498 * @param array $condition Additional condition on the contact table
501 * @param bool $shuffle
505 public static function listFollowers(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
507 $condition = DBA::mergeConditions($condition,
508 ['`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)', $cid]
511 return DI::dba()->selectToArray('contact', [], $condition,
512 ['limit' => [$offset, $count], 'order' => [$shuffle ? 'RAND()' : 'name']]
517 * Counts the number of contacts that are known mutuals with the provided public contact.
519 * @param int $cid Public contact id
520 * @param array $condition Additional condition array on the contact table
524 public static function countMutuals(int $cid, array $condition = [])
526 $condition = DBA::mergeConditions($condition,
527 ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)
528 AND `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)',
532 return DI::dba()->count('contact', $condition);
536 * Returns a paginated list of contacts that are known mutuals with the provided public contact.
538 * @param int $cid Public contact id
539 * @param array $condition Additional condition on the contact table
542 * @param bool $shuffle
546 public static function listMutuals(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
548 $condition = DBA::mergeConditions($condition,
549 ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)
550 AND `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)',
554 return DI::dba()->selectToArray('contact', [], $condition,
555 ['limit' => [$offset, $count], 'order' => [$shuffle ? 'RAND()' : 'name']]
561 * Counts the number of contacts with any relationship with the provided public contact.
563 * @param int $cid Public contact id
564 * @param array $condition Additional condition array on the contact table
568 public static function countAll(int $cid, array $condition = [])
570 $condition = DBA::mergeConditions($condition,
571 ['(`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)
572 OR `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`))',
576 return DI::dba()->count('contact', $condition);
580 * Returns a paginated list of contacts with any relationship with the provided public contact.
582 * @param int $cid Public contact id
583 * @param array $condition Additional condition on the contact table
586 * @param bool $shuffle
590 public static function listAll(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
592 $condition = DBA::mergeConditions($condition,
593 ['(`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)
594 OR `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`))',
598 return DI::dba()->selectToArray('contact', [], $condition,
599 ['limit' => [$offset, $count], 'order' => [$shuffle ? 'RAND()' : 'name']]
604 * Counts the number of contacts that both provided public contacts have interacted with at least once.
605 * Interactions include follows and likes and comments on public posts.
607 * @param int $sourceId Public contact id
608 * @param int $targetId Public contact id
609 * @param array $condition Additional condition array on the contact table
613 public static function countCommon(int $sourceId, int $targetId, array $condition = [])
615 $condition = DBA::mergeConditions($condition,
616 ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?)
617 AND `id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?)',
618 $sourceId, $targetId]
621 return DI::dba()->count('contact', $condition);
625 * Returns a paginated list of contacts that both provided public contacts have interacted with at least once.
626 * Interactions include follows and likes and comments on public posts.
628 * @param int $sourceId Public contact id
629 * @param int $targetId Public contact id
630 * @param array $condition Additional condition on the contact table
633 * @param bool $shuffle
634 * @return array|bool Array on success, false on failure
637 public static function listCommon(int $sourceId, int $targetId, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
639 $condition = DBA::mergeConditions($condition,
640 ["`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?)
641 AND `id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?)",
642 $sourceId, $targetId]
645 return DI::dba()->selectToArray('contact', [], $condition,
646 ['limit' => [$offset, $count], 'order' => [$shuffle ? 'RAND()' : 'name']]
651 * Counts the number of contacts that are followed by both provided public contacts.
653 * @param int $sourceId Public contact id
654 * @param int $targetId Public contact id
655 * @param array $condition Additional condition array on the contact table
659 public static function countCommonFollows(int $sourceId, int $targetId, array $condition = []): int
661 $condition = DBA::mergeConditions($condition,
662 ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)
663 AND `id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)',
664 $sourceId, $targetId]
667 return DI::dba()->count('contact', $condition);
671 * Returns a paginated list of contacts that are followed by both provided public contacts.
673 * @param int $sourceId Public contact id
674 * @param int $targetId Public contact id
675 * @param array $condition Additional condition array on the contact table
678 * @param bool $shuffle
679 * @return array|bool Array on success, false on failure
682 public static function listCommonFollows(int $sourceId, int $targetId, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
684 $condition = DBA::mergeConditions($condition,
685 ["`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)
686 AND `id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)",
687 $sourceId, $targetId]
690 return DI::dba()->selectToArray('contact', [], $condition,
691 ['limit' => [$offset, $count], 'order' => [$shuffle ? 'RAND()' : 'name']]
696 * Counts the number of contacts that follow both provided public contacts.
698 * @param int $sourceId Public contact id
699 * @param int $targetId Public contact id
700 * @param array $condition Additional condition on the contact table
704 public static function countCommonFollowers(int $sourceId, int $targetId, array $condition = []): int
706 $condition = DBA::mergeConditions($condition,
707 ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)
708 AND `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)",
709 $sourceId, $targetId]
712 return DI::dba()->count('contact', $condition);
716 * Returns a paginated list of contacts that follow both provided public contacts.
718 * @param int $sourceId Public contact id
719 * @param int $targetId Public contact id
720 * @param array $condition Additional condition on the contact table
723 * @param bool $shuffle
724 * @return array|bool Array on success, false on failure
727 public static function listCommonFollowers(int $sourceId, int $targetId, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
729 $condition = DBA::mergeConditions($condition,
730 ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)
731 AND `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)",
732 $sourceId, $targetId]
735 return DI::dba()->selectToArray('contact', [], $condition,
736 ['limit' => [$offset, $count], 'order' => [$shuffle ? 'RAND()' : 'name']]