3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Model\Contact;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Protocol;
27 use Friendica\Database\DBA;
29 use Friendica\Protocol\ActivityPub;
30 use Friendica\Util\DateTimeFormat;
31 use Friendica\Util\Strings;
34 * This class provides relationship information based on the `contact-relation` table.
35 * This table is directional (cid = source, relation-cid = target), references public contacts (with uid=0) and records both
36 * follows and the last interaction (likes/comments) on public posts.
41 * No discovery of followers/followings
43 const DISCOVERY_NONE = 0;
45 * Discover followers/followings of local contacts
47 const DISCOVERY_LOCAL = 1;
49 * Discover followers/followings of local contacts and contacts that visibly interacted on the system
51 const DISCOVERY_INTERACTOR = 2;
53 * Discover followers/followings of all contacts
55 const DISCOVERY_ALL = 3;
57 public static function store(int $target, int $actor, string $interaction_date)
59 if ($actor == $target) {
63 DBA::update('contact-relation', ['last-interaction' => $interaction_date], ['cid' => $target, 'relation-cid' => $actor], true);
67 * Fetches the followers of a given profile and adds them
69 * @param string $url URL of a profile
72 public static function discoverByUrl(string $url)
74 $contact = Contact::getByURL($url);
75 if (empty($contact)) {
79 if (!self::isDiscoverable($url, $contact)) {
83 $apcontact = APContact::getByURL($url, false);
85 if (!empty($apcontact['followers']) && is_string($apcontact['followers'])) {
86 $followers = ActivityPub::fetchItems($apcontact['followers']);
91 if (!empty($apcontact['following']) && is_string($apcontact['following'])) {
92 $followings = ActivityPub::fetchItems($apcontact['following']);
97 if (empty($followers) && empty($followings)) {
98 DBA::update('contact', ['last-discovery' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
99 Logger::info('The contact does not offer discoverable data', ['id' => $contact['id'], 'url' => $url, 'network' => $contact['network']]);
103 $target = $contact['id'];
105 if (!empty($followers)) {
106 // Clear the follower list, since it will be recreated in the next step
107 DBA::update('contact-relation', ['follows' => false], ['cid' => $target]);
111 foreach (array_merge($followers, $followings) as $contact) {
112 if (is_string($contact)) {
113 $contacts[] = $contact;
114 } elseif (!empty($contact['url']) && is_string($contact['url'])) {
115 $contacts[] = $contact['url'];
118 $contacts = array_unique($contacts);
120 $follower_counter = 0;
121 $following_counter = 0;
123 Logger::info('Discover contacts', ['id' => $target, 'url' => $url, 'contacts' => count($contacts)]);
124 foreach ($contacts as $contact) {
125 $actor = Contact::getIdForURL($contact);
126 if (!empty($actor)) {
127 if (in_array($contact, $followers)) {
128 $fields = ['cid' => $target, 'relation-cid' => $actor];
129 DBA::update('contact-relation', ['follows' => true, 'follow-updated' => DateTimeFormat::utcNow()], $fields, true);
133 if (in_array($contact, $followings)) {
134 $fields = ['cid' => $actor, 'relation-cid' => $target];
135 DBA::update('contact-relation', ['follows' => true, 'follow-updated' => DateTimeFormat::utcNow()], $fields, true);
136 $following_counter++;
141 if (!empty($followers)) {
142 // Delete all followers that aren't followers anymore (and aren't interacting)
143 DBA::delete('contact-relation', ['cid' => $target, 'follows' => false, 'last-interaction' => DBA::NULL_DATETIME]);
146 DBA::update('contact', ['last-discovery' => DateTimeFormat::utcNow()], ['id' => $target]);
147 Logger::info('Contacts discovery finished', ['id' => $target, 'url' => $url, 'follower' => $follower_counter, 'following' => $following_counter]);
152 * Tests if a given contact url is discoverable
154 * @param string $url Contact url
155 * @param array $contact Contact array
156 * @return boolean True if contact is discoverable
158 public static function isDiscoverable(string $url, array $contact = [])
160 $contact_discovery = DI::config()->get('system', 'contact_discovery');
162 if ($contact_discovery == self::DISCOVERY_NONE) {
166 if (empty($contact)) {
167 $contact = Contact::getByURL($url);
170 if (empty($contact)) {
174 if ($contact['last-discovery'] > DateTimeFormat::utc('now - 1 month')) {
175 Logger::info('No discovery - Last was less than a month ago.', ['id' => $contact['id'], 'url' => $url, 'discovery' => $contact['last-discovery']]);
179 if ($contact_discovery != self::DISCOVERY_ALL) {
180 $local = DBA::exists('contact', ["`nurl` = ? AND `uid` != ?", Strings::normaliseLink($url), 0]);
181 if (($contact_discovery == self::DISCOVERY_LOCAL) && !$local) {
182 Logger::info('No discovery - This contact is not followed/following locally.', ['id' => $contact['id'], 'url' => $url]);
186 if ($contact_discovery == self::DISCOVERY_INTERACTOR) {
187 $interactor = DBA::exists('contact-relation', ["`relation-cid` = ? AND `last-interaction` > ?", $contact['id'], DBA::NULL_DATETIME]);
188 if (!$local && !$interactor) {
189 Logger::info('No discovery - This contact is not interacting locally.', ['id' => $contact['id'], 'url' => $url]);
193 } elseif ($contact['created'] > DateTimeFormat::utc('now - 1 day')) {
194 // Newly created contacts are not discovered to avoid DDoS attacks
195 Logger::info('No discovery - Contact record is less than a day old.', ['id' => $contact['id'], 'url' => $url, 'discovery' => $contact['created']]);
199 if (!in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS])) {
200 $apcontact = APContact::getByURL($url, false);
201 if (empty($apcontact)) {
202 Logger::info('No discovery - The contact does not seem to speak ActivityPub.', ['id' => $contact['id'], 'url' => $url, 'network' => $contact['network']]);
211 * Counts all the known follows of the provided public contact
213 * @param int $cid Public contact id
214 * @param array $condition Additional condition on the contact table
218 public static function countFollows(int $cid, array $condition = [])
220 $condition = DBA::mergeConditions($condition,
222 SELECT `relation-cid`
223 FROM `contact-relation`
229 return DI::dba()->count('contact', $condition);
233 * Returns a paginated list of contacts that are followed the provided public contact.
235 * @param int $cid Public contact id
236 * @param array $condition Additional condition on the contact table
239 * @param bool $shuffle
243 public static function listFollows(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
245 $condition = DBA::mergeConditions($condition,
247 SELECT `relation-cid`
248 FROM `contact-relation`
254 $follows = DI::dba()->selectToArray(
258 'limit' => [$offset, $count],
259 'order' => [$shuffle ? 'RAND()' : 'name']
267 * Counts all the known followers of the provided public contact
269 * @param int $cid Public contact id
270 * @param array $condition Additional condition on the contact table
274 public static function countFollowers(int $cid, array $condition = [])
276 $condition = DBA::mergeConditions($condition,
279 FROM `contact-relation`
280 WHERE `relation-cid` = ?
285 return DI::dba()->count('contact', $condition);
289 * Returns a paginated list of contacts that follow the provided public contact.
291 * @param int $cid Public contact id
292 * @param array $condition Additional condition on the contact table
295 * @param bool $shuffle
299 public static function listFollowers(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
301 $condition = DBA::mergeConditions($condition,
304 FROM `contact-relation`
305 WHERE `relation-cid` = ?
310 $followers = DI::dba()->selectToArray(
314 'limit' => [$offset, $count],
315 'order' => [$shuffle ? 'RAND()' : 'name']
323 * Counts the number of contacts that both provided public contacts have interacted with at least once.
324 * Interactions include follows and likes and comments on public posts.
326 * @param int $sourceId Public contact id
327 * @param int $targetId Public contact id
328 * @param array $condition Additional condition array on the contact table
332 public static function countCommon(int $sourceId, int $targetId, array $condition = [])
334 $condition = DBA::mergeConditions($condition,
336 SELECT `relation-cid`
337 FROM `contact-relation`
341 SELECT `relation-cid`
342 FROM `contact-relation`
344 )', $sourceId, $targetId]
347 $total = DI::dba()->count('contact', $condition);
353 * Returns a paginated list of contacts that both provided public contacts have interacted with at least once.
354 * Interactions include follows and likes and comments on public posts.
356 * @param int $sourceId Public contact id
357 * @param int $targetId Public contact id
358 * @param array $condition Additional condition on the contact table
361 * @param bool $shuffle
365 public static function listCommon(int $sourceId, int $targetId, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
367 $condition = DBA::mergeConditions($condition,
369 SELECT `relation-cid`
370 FROM `contact-relation`
375 SELECT `relation-cid`
376 FROM `contact-relation`
379 )", $sourceId, $targetId]
382 $contacts = DI::dba()->selectToArray(
386 'limit' => [$offset, $count],
387 'order' => [$shuffle ? 'name' : 'RAND()'],
396 * Counts the number of contacts that are followed by both provided public contacts.
398 * @param int $sourceId Public contact id
399 * @param int $targetId Public contact id
400 * @param array $condition Additional condition array on the contact table
404 public static function countCommonFollows(int $sourceId, int $targetId, array $condition = [])
406 $condition = DBA::mergeConditions($condition,
408 SELECT `relation-cid`
409 FROM `contact-relation`
414 SELECT `relation-cid`
415 FROM `contact-relation`
418 )', $sourceId, $targetId]
421 $total = DI::dba()->count('contact', $condition);
427 * Returns a paginated list of contacts that are followed by both provided public contacts.
429 * @param int $sourceId Public contact id
430 * @param int $targetId Public contact id
431 * @param array $condition Additional condition array on the contact table
434 * @param bool $shuffle
438 public static function listCommonFollows(int $sourceId, int $targetId, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
440 $condition = DBA::mergeConditions($condition,
442 SELECT `relation-cid`
443 FROM `contact-relation`
448 SELECT `relation-cid`
449 FROM `contact-relation`
452 )", $sourceId, $targetId]
455 $contacts = DI::dba()->selectToArray(
459 'limit' => [$offset, $count],
460 'order' => [$shuffle ? 'name' : 'RAND()'],
468 * Counts the number of contacts that follow both provided public contacts.
470 * @param int $sourceId Public contact id
471 * @param int $targetId Public contact id
472 * @param array $condition Additional condition on the contact table
476 public static function countCommonFollowers(int $sourceId, int $targetId, array $condition = [])
478 $condition = DBA::mergeConditions($condition,
481 FROM `contact-relation`
482 WHERE `relation-cid` = ?
487 FROM `contact-relation`
488 WHERE `relation-cid` = ?
490 )', $sourceId, $targetId]
493 $total = DI::dba()->count('contact', $condition);
499 * Returns a paginated list of contacts that follow both provided public contacts.
501 * @param int $sourceId Public contact id
502 * @param int $targetId Public contact id
503 * @param array $condition Additional condition on the contact table
506 * @param bool $shuffle
510 public static function listCommonFollowers(int $sourceId, int $targetId, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
512 $condition = DBA::mergeConditions($condition,
515 FROM `contact-relation`
516 WHERE `relation-cid` = ?
521 FROM `contact-relation`
522 WHERE `relation-cid` = ?
524 )", $sourceId, $targetId]
527 $contacts = DI::dba()->selectToArray(
531 'limit' => [$offset, $count],
532 'order' => [$shuffle ? 'name' : 'RAND()'],