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