]> git.mxchange.org Git - friendica.git/blob - src/Model/Contact/Relation.php
Merge pull request #9046 from annando/local-followers
[friendica.git] / src / Model / Contact / Relation.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Model\Contact;
23
24 use Exception;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Protocol;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\APContact;
30 use Friendica\Model\Contact;
31 use Friendica\Model\Profile;
32 use Friendica\Model\User;
33 use Friendica\Protocol\ActivityPub;
34 use Friendica\Util\DateTimeFormat;
35 use Friendica\Util\Strings;
36
37 /**
38  * This class provides relationship information based on the `contact-relation` table.
39  * This table is directional (cid = source, relation-cid = target), references public contacts (with uid=0) and records both
40  * follows and the last interaction (likes/comments) on public posts.
41  */
42 class Relation
43 {
44         /**
45          * No discovery of followers/followings
46          */
47         const DISCOVERY_NONE = 0;
48         /**
49          * Discover followers/followings of local contacts
50          */
51         const DISCOVERY_LOCAL = 1;
52         /**
53          * Discover followers/followings of local contacts and contacts that visibly interacted on the system
54          */
55         const DISCOVERY_INTERACTOR = 2;
56         /**
57          * Discover followers/followings of all contacts
58          */
59         const DISCOVERY_ALL = 3;
60
61         public static function store(int $target, int $actor, string $interaction_date)
62         {
63                 if ($actor == $target) {
64                         return;
65                 }
66
67                 DBA::update('contact-relation', ['last-interaction' => $interaction_date], ['cid' => $target, 'relation-cid' => $actor], true);
68         }
69
70         /**
71          * Fetches the followers of a given profile and adds them
72          *
73          * @param string $url URL of a profile
74          * @return void
75          */
76         public static function discoverByUrl(string $url)
77         {
78                 $contact = Contact::getByURL($url);
79                 if (empty($contact)) {
80                         return;
81                 }
82
83                 if (!self::isDiscoverable($url, $contact)) {
84                         return;
85                 }
86
87                 $uid = User::getIdForURL($url);
88                 if (!empty($uid)) {
89                         // Fetch the followers/followings locally
90                         $followers = self::getContacts($uid, [Contact::FOLLOWER, Contact::FRIEND]);
91                         $followings = self::getContacts($uid, [Contact::SHARING, Contact::FRIEND]);
92                 } else {
93                         $apcontact = APContact::getByURL($url, false);
94
95                         if (!empty($apcontact['followers']) && is_string($apcontact['followers'])) {
96                                 $followers = ActivityPub::fetchItems($apcontact['followers']);
97                         } else {
98                                 $followers = [];
99                         }
100
101                         if (!empty($apcontact['following']) && is_string($apcontact['following'])) {
102                                 $followings = ActivityPub::fetchItems($apcontact['following']);
103                         } else {
104                                 $followings = [];
105                         }
106                 }
107
108                 if (empty($followers) && empty($followings)) {
109                         DBA::update('contact', ['last-discovery' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
110                         Logger::info('The contact does not offer discoverable data', ['id' => $contact['id'], 'url' => $url, 'network' => $contact['network']]);
111                         return;
112                 }
113
114                 $target = $contact['id'];
115
116                 if (!empty($followers)) {
117                         // Clear the follower list, since it will be recreated in the next step
118                         DBA::update('contact-relation', ['follows' => false], ['cid' => $target]);
119                 }
120
121                 $contacts = [];
122                 foreach (array_merge($followers, $followings) as $contact) {
123                         if (is_string($contact)) {
124                                 $contacts[] = $contact;
125                         } elseif (!empty($contact['url']) && is_string($contact['url'])) {
126                                 $contacts[] = $contact['url'];
127                         }
128                 }
129                 $contacts = array_unique($contacts);
130
131                 $follower_counter = 0;
132                 $following_counter = 0;
133
134                 Logger::info('Discover contacts', ['id' => $target, 'url' => $url, 'contacts' => count($contacts)]);
135                 foreach ($contacts as $contact) {
136                         $actor = Contact::getIdForURL($contact);
137                         if (!empty($actor)) {
138                                 if (in_array($contact, $followers)) {
139                                         $fields = ['cid' => $target, 'relation-cid' => $actor];
140                                         DBA::update('contact-relation', ['follows' => true, 'follow-updated' => DateTimeFormat::utcNow()], $fields, true);
141                                         $follower_counter++;
142                                 }
143
144                                 if (in_array($contact, $followings)) {
145                                         $fields = ['cid' => $actor, 'relation-cid' => $target];
146                                         DBA::update('contact-relation', ['follows' => true, 'follow-updated' => DateTimeFormat::utcNow()], $fields, true);
147                                         $following_counter++;
148                                 }
149                         }
150                 }
151
152                 if (!empty($followers)) {
153                         // Delete all followers that aren't followers anymore (and aren't interacting)
154                         DBA::delete('contact-relation', ['cid' => $target, 'follows' => false, 'last-interaction' => DBA::NULL_DATETIME]);
155                 }
156
157                 DBA::update('contact', ['last-discovery' => DateTimeFormat::utcNow()], ['id' => $target]);
158                 Logger::info('Contacts discovery finished', ['id' => $target, 'url' => $url, 'follower' => $follower_counter, 'following' => $following_counter]);
159                 return;
160         }
161
162         /**
163          * Fetch contact list from the given local user
164          *
165          * @param integer $uid
166          * @param array $rel
167          * @return void
168          */
169         private static function getContacts(int $uid, array $rel)
170         {
171                 $list = [];
172                 $profile = Profile::getByUID($uid);
173                 if (!empty($profile['hide-friends'])) {
174                         return $list;
175                 }
176
177                 $condition = ['rel' => $rel, 'uid' => $uid, 'self' => false, 'deleted' => false,
178                         'hidden' => false, 'archive' => false, 'pending' => false];
179                 $condition = DBA::mergeConditions($condition, ["`url` IN (SELECT `url` FROM `apcontact`)"]);
180                 $contacts = DBA::select('contact', ['url'], $condition);
181                 while ($contact = DBA::fetch($contacts)) {
182                         $list[] = $contact['url'];
183                 }
184                 DBA::close($contacts);
185
186                 return $list;
187         }
188
189         /**
190          * Tests if a given contact url is discoverable
191          *
192          * @param string $url     Contact url
193          * @param array  $contact Contact array
194          * @return boolean True if contact is discoverable
195          */
196         public static function isDiscoverable(string $url, array $contact = [])
197         {
198                 $contact_discovery = DI::config()->get('system', 'contact_discovery');
199
200                 if ($contact_discovery == self::DISCOVERY_NONE) {
201                         return false;
202                 }
203
204                 if (empty($contact)) {
205                         $contact = Contact::getByURL($url, false);
206                 }
207
208                 if (empty($contact)) {
209                         return false;
210                 }
211
212                 if ($contact['last-discovery'] > DateTimeFormat::utc('now - 1 month')) {
213                         Logger::info('No discovery - Last was less than a month ago.', ['id' => $contact['id'], 'url' => $url, 'discovery' => $contact['last-discovery']]);
214                         return false;
215                 }
216
217                 if ($contact_discovery != self::DISCOVERY_ALL) {
218                         $local = DBA::exists('contact', ["`nurl` = ? AND `uid` != ?", Strings::normaliseLink($url), 0]);
219                         if (($contact_discovery == self::DISCOVERY_LOCAL) && !$local) {
220                                 Logger::info('No discovery - This contact is not followed/following locally.', ['id' => $contact['id'], 'url' => $url]);
221                                 return false;
222                         }
223
224                         if ($contact_discovery == self::DISCOVERY_INTERACTOR) {
225                                 $interactor = DBA::exists('contact-relation', ["`relation-cid` = ? AND `last-interaction` > ?", $contact['id'], DBA::NULL_DATETIME]);
226                                 if (!$local && !$interactor) {
227                                         Logger::info('No discovery - This contact is not interacting locally.', ['id' => $contact['id'], 'url' => $url]);
228                                         return false;
229                                 }
230                         }
231                 } elseif ($contact['created'] > DateTimeFormat::utc('now - 1 day')) {
232                         // Newly created contacts are not discovered to avoid DDoS attacks
233                         Logger::info('No discovery - Contact record is less than a day old.', ['id' => $contact['id'], 'url' => $url, 'discovery' => $contact['created']]);
234                         return false;
235                 }
236
237                 if (!in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS])) {
238                         $apcontact = APContact::getByURL($url, false);
239                         if (empty($apcontact)) {
240                                 Logger::info('No discovery - The contact does not seem to speak ActivityPub.', ['id' => $contact['id'], 'url' => $url, 'network' => $contact['network']]);
241                                 return false;
242                         }
243                 }
244
245                 return true;
246         }
247
248         /**
249          * @param int $uid   user
250          * @param int $start optional, default 0
251          * @param int $limit optional, default 80
252          * @return array
253          */
254         static public function getSuggestions(int $uid, int $start = 0, int $limit = 80)
255         {
256                 $cid = Contact::getPublicIdByUserId($uid);
257                 $totallimit = $start + $limit;
258                 $contacts = [];
259
260                 Logger::info('Collecting suggestions', ['uid' => $uid, 'cid' => $cid, 'start' => $start, 'limit' => $limit]);
261
262                 $diaspora = DI::config()->get('system', 'diaspora_enabled') ? Protocol::DIASPORA : Protocol::ACTIVITYPUB;
263                 $ostatus = !DI::config()->get('system', 'ostatus_disabled') ? Protocol::OSTATUS : Protocol::ACTIVITYPUB;
264
265                 // The query returns contacts where contacts interacted with whom the given user follows.
266                 // Contacts who already are in the user's contact table are ignored.
267                 $results = DBA::select('contact', [],
268                         ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` IN
269                                 (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ?)
270                                         AND NOT `cid` IN (SELECT `id` FROM `contact` WHERE `uid` = ? AND `nurl` IN
271                                                 (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))))
272                         AND NOT `hidden` AND `network` IN (?, ?, ?, ?)",
273                         $cid, 0, $uid, Contact::FRIEND, Contact::SHARING,
274                         Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus],
275                         ['order' => ['last-item' => true], 'limit' => $totallimit]
276                 );
277
278                 while ($contact = DBA::fetch($results)) {
279                         $contacts[$contact['id']] = $contact;
280                 }
281                 DBA::close($results);
282
283                 Logger::info('Contacts of contacts who are followed by the given user', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
284
285                 if (count($contacts) >= $totallimit) {
286                         return array_slice($contacts, $start, $limit);
287                 }
288
289                 // The query returns contacts where contacts interacted with whom also interacted with the given user.
290                 // Contacts who already are in the user's contact table are ignored.
291                 $results = DBA::select('contact', [],
292                         ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` IN
293                                 (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?)
294                                         AND NOT `cid` IN (SELECT `id` FROM `contact` WHERE `uid` = ? AND `nurl` IN
295                                                 (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))))
296                         AND NOT `hidden` AND `network` IN (?, ?, ?, ?)",
297                         $cid, 0, $uid, Contact::FRIEND, Contact::SHARING,
298                         Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus],
299                         ['order' => ['last-item' => true], 'limit' => $totallimit]
300                 );
301
302                 while ($contact = DBA::fetch($results)) {
303                         $contacts[$contact['id']] = $contact;
304                 }
305                 DBA::close($results);
306
307                 Logger::info('Contacts of contacts who are following the given user', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
308
309                 if (count($contacts) >= $totallimit) {
310                         return array_slice($contacts, $start, $limit);
311                 }
312
313                 // The query returns contacts that follow the given user but aren't followed by that user.
314                 $results = DBA::select('contact', [],
315                         ["`nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` = ?)
316                         AND NOT `hidden` AND `uid` = ? AND `network` IN (?, ?, ?, ?)",
317                         $uid, Contact::FOLLOWER, 0, 
318                         Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus],
319                         ['order' => ['last-item' => true], 'limit' => $totallimit]
320                 );
321
322                 while ($contact = DBA::fetch($results)) {
323                         $contacts[$contact['id']] = $contact;
324                 }
325                 DBA::close($results);
326
327                 Logger::info('Followers that are not followed by the given user', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
328
329                 if (count($contacts) >= $totallimit) {
330                         return array_slice($contacts, $start, $limit);
331                 }
332
333                 // The query returns any contact that isn't followed by that user.
334                 $results = DBA::select('contact', [],
335                         ["NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))
336                         AND NOT `hidden` AND `uid` = ? AND `network` IN (?, ?, ?, ?)",
337                         $uid, Contact::FRIEND, Contact::SHARING, 0, 
338                         Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus],
339                         ['order' => ['last-item' => true], 'limit' => $totallimit]
340                 );
341
342                 while ($contact = DBA::fetch($results)) {
343                         $contacts[$contact['id']] = $contact;
344                 }
345                 DBA::close($results);
346
347                 Logger::info('Any contact', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
348
349                 return array_slice($contacts, $start, $limit);
350         }
351
352         /**
353          * Counts all the known follows of the provided public contact
354          *
355          * @param int   $cid       Public contact id
356          * @param array $condition Additional condition on the contact table
357          * @return int
358          * @throws Exception
359          */
360         public static function countFollows(int $cid, array $condition = [])
361         {
362                 $condition = DBA::mergeConditions($condition,
363                         ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)', 
364                         $cid]
365                 );
366
367                 return DI::dba()->count('contact', $condition);
368         }
369
370         /**
371          * Returns a paginated list of contacts that are followed the provided public contact.
372          *
373          * @param int   $cid       Public contact id
374          * @param array $condition Additional condition on the contact table
375          * @param int   $count
376          * @param int   $offset
377          * @param bool  $shuffle
378          * @return array
379          * @throws Exception
380          */
381         public static function listFollows(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
382         {
383                 $condition = DBA::mergeConditions($condition,
384                         ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)', 
385                         $cid]
386                 );
387
388                 return DI::dba()->selectToArray('contact', [], $condition,
389                         ['limit' => [$offset, $count], 'order' => [$shuffle ? 'RAND()' : 'name']]
390                 );
391         }
392
393         /**
394          * Counts all the known followers of the provided public contact
395          *
396          * @param int   $cid       Public contact id
397          * @param array $condition Additional condition on the contact table
398          * @return int
399          * @throws Exception
400          */
401         public static function countFollowers(int $cid, array $condition = [])
402         {
403                 $condition = DBA::mergeConditions($condition,
404                         ['`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)',
405                         $cid]
406                 );
407
408                 return DI::dba()->count('contact', $condition);
409         }
410
411         /**
412          * Returns a paginated list of contacts that follow the provided public contact.
413          *
414          * @param int   $cid       Public contact id
415          * @param array $condition Additional condition on the contact table
416          * @param int   $count
417          * @param int   $offset
418          * @param bool  $shuffle
419          * @return array
420          * @throws Exception
421          */
422         public static function listFollowers(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
423         {
424                 $condition = DBA::mergeConditions($condition,
425                         ['`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)', $cid]
426                 );
427
428                 return DI::dba()->selectToArray('contact', [], $condition,
429                         ['limit' => [$offset, $count], 'order' => [$shuffle ? 'RAND()' : 'name']]
430                 );
431         }
432
433         /**
434          * Counts the number of contacts that are known mutuals with the provided public contact.
435          *
436          * @param int   $cid       Public contact id
437          * @param array $condition Additional condition array on the contact table
438          * @return int
439          * @throws Exception
440          */
441         public static function countMutuals(int $cid, array $condition = [])
442         {
443                 $condition = DBA::mergeConditions($condition,
444                         ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`) 
445                         AND `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)',
446                         $cid, $cid]
447                 );
448
449                 return DI::dba()->count('contact', $condition);
450         }
451
452         /**
453          * Returns a paginated list of contacts that are known mutuals with the provided public contact.
454          *
455          * @param int   $cid       Public contact id
456          * @param array $condition Additional condition on the contact table
457          * @param int   $count
458          * @param int   $offset
459          * @param bool  $shuffle
460          * @return array
461          * @throws Exception
462          */
463         public static function listMutuals(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
464         {
465                 $condition = DBA::mergeConditions($condition,
466                         ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`) 
467                         AND `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)',
468                         $cid, $cid]
469                 );
470
471                 return DI::dba()->selectToArray('contact', [], $condition,
472                         ['limit' => [$offset, $count], 'order' => [$shuffle ? 'name' : 'RAND()']]
473                 );
474         }
475
476
477         /**
478          * Counts the number of contacts with any relationship with the provided public contact.
479          *
480          * @param int   $cid       Public contact id
481          * @param array $condition Additional condition array on the contact table
482          * @return int
483          * @throws Exception
484          */
485         public static function countAll(int $cid, array $condition = [])
486         {
487                 $condition = DBA::mergeConditions($condition,
488                         ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`) 
489                         OR `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)',
490                                 $cid, $cid]
491                 );
492
493                 return DI::dba()->count('contact', $condition);
494         }
495
496         /**
497          * Returns a paginated list of contacts with any relationship with the provided public contact.
498          *
499          * @param int   $cid       Public contact id
500          * @param array $condition Additional condition on the contact table
501          * @param int   $count
502          * @param int   $offset
503          * @param bool  $shuffle
504          * @return array
505          * @throws Exception
506          */
507         public static function listAll(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
508         {
509                 $condition = DBA::mergeConditions($condition,
510                         ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`) 
511                         OR `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)',
512                                 $cid, $cid]
513                 );
514
515                 return DI::dba()->selectToArray('contact', [], $condition,
516                         ['limit' => [$offset, $count], 'order' => [$shuffle ? 'name' : 'RAND()']]
517                 );
518         }
519
520         /**
521          * Counts the number of contacts that both provided public contacts have interacted with at least once.
522          * Interactions include follows and likes and comments on public posts.
523          *
524          * @param int   $sourceId  Public contact id
525          * @param int   $targetId  Public contact id
526          * @param array $condition Additional condition array on the contact table
527          * @return int
528          * @throws Exception
529          */
530         public static function countCommon(int $sourceId, int $targetId, array $condition = [])
531         {
532                 $condition = DBA::mergeConditions($condition,
533                         ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?) 
534                         AND `id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?)',
535                         $sourceId, $targetId]
536                 );
537
538                 return DI::dba()->count('contact', $condition);
539         }
540
541         /**
542          * Returns a paginated list of contacts that both provided public contacts have interacted with at least once.
543          * Interactions include follows and likes and comments on public posts.
544          *
545          * @param int   $sourceId  Public contact id
546          * @param int   $targetId  Public contact id
547          * @param array $condition Additional condition on the contact table
548          * @param int   $count
549          * @param int   $offset
550          * @param bool  $shuffle
551          * @return array
552          * @throws Exception
553          */
554         public static function listCommon(int $sourceId, int $targetId, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
555         {
556                 $condition = DBA::mergeConditions($condition,
557                         ["`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?) 
558                         AND `id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?)",
559                         $sourceId, $targetId]
560                 );
561
562                 return DI::dba()->selectToArray('contact', [], $condition,
563                         ['limit' => [$offset, $count], 'order' => [$shuffle ? 'name' : 'RAND()']]
564                 );
565         }
566
567         /**
568          * Counts the number of contacts that are followed by both provided public contacts.
569          *
570          * @param int   $sourceId  Public contact id
571          * @param int   $targetId  Public contact id
572          * @param array $condition Additional condition array on the contact table
573          * @return int
574          * @throws Exception
575          */
576         public static function countCommonFollows(int $sourceId, int $targetId, array $condition = [])
577         {
578                 $condition = DBA::mergeConditions($condition,
579                         ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`) 
580                         AND `id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)',
581                         $sourceId, $targetId]
582                 );
583
584                 return DI::dba()->count('contact', $condition);
585         }
586
587         /**
588          * Returns a paginated list of contacts that are followed by both provided public contacts.
589          *
590          * @param int   $sourceId  Public contact id
591          * @param int   $targetId  Public contact id
592          * @param array $condition Additional condition array on the contact table
593          * @param int   $count
594          * @param int   $offset
595          * @param bool  $shuffle
596          * @return array
597          * @throws Exception
598          */
599         public static function listCommonFollows(int $sourceId, int $targetId, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
600         {
601                 $condition = DBA::mergeConditions($condition,
602                         ["`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`) 
603                         AND `id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)",
604                         $sourceId, $targetId]
605                 );
606
607                 return DI::dba()->selectToArray('contact', [], $condition,
608                         ['limit' => [$offset, $count], 'order' => [$shuffle ? 'name' : 'RAND()']]
609                 );
610         }
611
612         /**
613          * Counts the number of contacts that follow both provided public contacts.
614          *
615          * @param int   $sourceId  Public contact id
616          * @param int   $targetId  Public contact id
617          * @param array $condition Additional condition on the contact table
618          * @return int
619          * @throws Exception
620          */
621         public static function countCommonFollowers(int $sourceId, int $targetId, array $condition = [])
622         {
623                 $condition = DBA::mergeConditions($condition,
624                         ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`) 
625                         AND `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)",
626                         $sourceId, $targetId]
627                 );
628
629                 return DI::dba()->count('contact', $condition);
630         }
631
632         /**
633          * Returns a paginated list of contacts that follow both provided public contacts.
634          *
635          * @param int   $sourceId  Public contact id
636          * @param int   $targetId  Public contact id
637          * @param array $condition Additional condition on the contact table
638          * @param int   $count
639          * @param int   $offset
640          * @param bool  $shuffle
641          * @return array
642          * @throws Exception
643          */
644         public static function listCommonFollowers(int $sourceId, int $targetId, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
645         {
646                 $condition = DBA::mergeConditions($condition,
647                         ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`) 
648                         AND `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)",
649                         $sourceId, $targetId]
650                 );
651
652                 return DI::dba()->selectToArray('contact', [], $condition,
653                         ['limit' => [$offset, $count],  'order' => [$shuffle ? 'name' : 'RAND()']]
654                 );
655         }
656 }