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