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_discovery = DI::config()->get('system', 'contact_discovery');
70 if ($contact_discovery == self::DISCOVERY_NONE) {
74 $contact = Contact::getByURL($url);
75 if (empty($contact)) {
79 if ($contact['last-discovery'] > DateTimeFormat::utc('now - 1 month')) {
80 Logger::info('No discovery - Last was less than a month ago.', ['id' => $contact['id'], 'url' => $url, 'discovery' => $contact['last-discovery']]);
84 if ($contact_discovery != self::DISCOVERY_ALL) {
85 $local = DBA::exists('contact', ["`nurl` = ? AND `uid` != ?", Strings::normaliseLink($url), 0]);
86 if (($contact_discovery == self::DISCOVERY_LOCAL) && !$local) {
87 Logger::info('No discovery - This contact is not followed/following locally.', ['id' => $contact['id'], 'url' => $url]);
91 if ($contact_discovery == self::DISCOVERY_INTERACTOR) {
92 $interactor = DBA::exists('contact-relation', ["`relation-cid` = ? AND `last-interaction` > ?", $contact['id'], DBA::NULL_DATETIME]);
93 if (!$local && !$interactor) {
94 Logger::info('No discovery - This contact is not interacting locally.', ['id' => $contact['id'], 'url' => $url]);
98 } elseif ($contact['created'] > DateTimeFormat::utc('now - 1 day')) {
99 // Newly created contacts are not discovered to avoid DDoS attacks
100 Logger::info('No discovery - Contact record is less than a day old.', ['id' => $contact['id'], 'url' => $url, 'discovery' => $contact['created']]);
104 if (in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS])) {
105 // The contact is (most likely) speaking AP, so updating is allowed
106 $apcontact = APContact::getByURL($url);
108 // The contact isn't obviously speaking AP, so we don't allow updating
109 $apcontact = APContact::getByURL($url, false);
112 if (!empty($apcontact['followers']) && is_string($apcontact['followers'])) {
113 $followers = ActivityPub::fetchItems($apcontact['followers']);
118 if (!empty($apcontact['following']) && is_string($apcontact['following'])) {
119 $followings = ActivityPub::fetchItems($apcontact['following']);
124 if (empty($followers) && empty($followings)) {
128 $target = $contact['id'];
130 if (!empty($followers)) {
131 // Clear the follower list, since it will be recreated in the next step
132 DBA::update('contact-relation', ['follows' => false], ['cid' => $target]);
136 foreach (array_merge($followers, $followings) as $contact) {
137 if (is_string($contact)) {
138 $contacts[] = $contact;
139 } elseif (!empty($contact['url']) && is_string($contact['url'])) {
140 $contacts[] = $contact['url'];
143 $contacts = array_unique($contacts);
145 Logger::info('Discover contacts', ['id' => $target, 'url' => $url, 'contacts' => count($contacts)]);
146 foreach ($contacts as $contact) {
147 $actor = Contact::getIdForURL($contact);
148 if (!empty($actor)) {
150 if (in_array($contact, $followers)) {
151 $fields = ['cid' => $target, 'relation-cid' => $actor];
152 } elseif (in_array($contact, $followings)) {
153 $fields = ['cid' => $actor, 'relation-cid' => $target];
158 DBA::update('contact-relation', ['follows' => true, 'follow-updated' => DateTimeFormat::utcNow()], $fields, true);
162 if (!empty($followers)) {
163 // Delete all followers that aren't followers anymore (and aren't interacting)
164 DBA::delete('contact-relation', ['cid' => $target, 'follows' => false, 'last-interaction' => DBA::NULL_DATETIME]);
167 DBA::update('contact', ['last-discovery' => DateTimeFormat::utcNow()], ['id' => $target]);
168 Logger::info('Contacts discovery finished, "last-discovery" set', ['id' => $target, 'url' => $url]);