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;
24 use Friendica\Core\Logger;
25 use Friendica\Core\Protocol;
26 use Friendica\Database\DBA;
28 use Friendica\Protocol\ActivityPub;
29 use Friendica\Util\DateTimeFormat;
30 use Friendica\Util\Strings;
35 * No discovery of followers/followings
37 const DISCOVERY_NONE = 0;
39 * Discover followers/followings of local contacts
41 const DISCOVERY_LOCAL = 1;
43 * Discover followers/followings of local contacts and contacts that visibly interacted on the system
45 const DISCOVERY_INTERACTOR = 2;
47 * Discover followers/followings of all contacts
49 const DISCOVERY_ALL = 3;
51 public static function store(int $target, int $actor, string $interaction_date)
53 if ($actor == $target) {
57 DBA::update('contact-relation', ['last-interaction' => $interaction_date], ['cid' => $target, 'relation-cid' => $actor], true);
61 * Fetches the followers of a given profile and adds them
63 * @param string $url URL of a profile
66 public static function discoverByUrl(string $url)
68 $contact = Contact::getByURL($url);
69 if (empty($contact)) {
73 if (!self::isDiscoverable($url, $contact)) {
77 $apcontact = APContact::getByURL($url, false);
79 if (!empty($apcontact['followers']) && is_string($apcontact['followers'])) {
80 $followers = ActivityPub::fetchItems($apcontact['followers']);
85 if (!empty($apcontact['following']) && is_string($apcontact['following'])) {
86 $followings = ActivityPub::fetchItems($apcontact['following']);
91 if (empty($followers) && empty($followings)) {
92 DBA::update('contact', ['last-discovery' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
93 Logger::info('The contact does not offer discoverable data', ['id' => $contact['id'], 'url' => $url, 'network' => $contact['network']]);
97 $target = $contact['id'];
99 if (!empty($followers)) {
100 // Clear the follower list, since it will be recreated in the next step
101 DBA::update('contact-relation', ['follows' => false], ['cid' => $target]);
105 foreach (array_merge($followers, $followings) as $contact) {
106 if (is_string($contact)) {
107 $contacts[] = $contact;
108 } elseif (!empty($contact['url']) && is_string($contact['url'])) {
109 $contacts[] = $contact['url'];
112 $contacts = array_unique($contacts);
114 $follower_counter = 0;
115 $following_counter = 0;
117 Logger::info('Discover contacts', ['id' => $target, 'url' => $url, 'contacts' => count($contacts)]);
118 foreach ($contacts as $contact) {
119 $actor = Contact::getIdForURL($contact);
120 if (!empty($actor)) {
121 if (in_array($contact, $followers)) {
122 $fields = ['cid' => $target, 'relation-cid' => $actor];
123 DBA::update('contact-relation', ['follows' => true, 'follow-updated' => DateTimeFormat::utcNow()], $fields, true);
127 if (in_array($contact, $followings)) {
128 $fields = ['cid' => $actor, 'relation-cid' => $target];
129 DBA::update('contact-relation', ['follows' => true, 'follow-updated' => DateTimeFormat::utcNow()], $fields, true);
130 $following_counter++;
135 if (!empty($followers)) {
136 // Delete all followers that aren't followers anymore (and aren't interacting)
137 DBA::delete('contact-relation', ['cid' => $target, 'follows' => false, 'last-interaction' => DBA::NULL_DATETIME]);
140 DBA::update('contact', ['last-discovery' => DateTimeFormat::utcNow()], ['id' => $target]);
141 Logger::info('Contacts discovery finished', ['id' => $target, 'url' => $url, 'follower' => $follower_counter, 'following' => $following_counter]);
146 * Tests if a given contact url is discoverable
148 * @param string $url Contact url
149 * @param array $contact Contact array
150 * @return boolean True if contact is discoverable
152 public static function isDiscoverable(string $url, array $contact = [])
154 $contact_discovery = DI::config()->get('system', 'contact_discovery');
156 if ($contact_discovery == self::DISCOVERY_NONE) {
160 if (empty($contact)) {
161 $contact = Contact::getByURL($url);
164 if (empty($contact)) {
168 if ($contact['last-discovery'] > DateTimeFormat::utc('now - 1 month')) {
169 Logger::info('No discovery - Last was less than a month ago.', ['id' => $contact['id'], 'url' => $url, 'discovery' => $contact['last-discovery']]);
173 if ($contact_discovery != self::DISCOVERY_ALL) {
174 $local = DBA::exists('contact', ["`nurl` = ? AND `uid` != ?", Strings::normaliseLink($url), 0]);
175 if (($contact_discovery == self::DISCOVERY_LOCAL) && !$local) {
176 Logger::info('No discovery - This contact is not followed/following locally.', ['id' => $contact['id'], 'url' => $url]);
180 if ($contact_discovery == self::DISCOVERY_INTERACTOR) {
181 $interactor = DBA::exists('contact-relation', ["`relation-cid` = ? AND `last-interaction` > ?", $contact['id'], DBA::NULL_DATETIME]);
182 if (!$local && !$interactor) {
183 Logger::info('No discovery - This contact is not interacting locally.', ['id' => $contact['id'], 'url' => $url]);
187 } elseif ($contact['created'] > DateTimeFormat::utc('now - 1 day')) {
188 // Newly created contacts are not discovered to avoid DDoS attacks
189 Logger::info('No discovery - Contact record is less than a day old.', ['id' => $contact['id'], 'url' => $url, 'discovery' => $contact['created']]);
193 if (!in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS])) {
194 $apcontact = APContact::getByURL($url, false);
195 if (empty($apcontact)) {
196 Logger::info('No discovery - The contact does not seem to speak ActivityPub.', ['id' => $contact['id'], 'url' => $url, 'network' => $contact['network']]);