]> git.mxchange.org Git - friendica.git/blob - src/Model/ContactRelation.php
Asynchronous contact relation check
[friendica.git] / src / Model / ContactRelation.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;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\Protocol;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Protocol\ActivityPub;
29 use Friendica\Util\DateTimeFormat;
30 use Friendica\Util\Strings;
31
32 class ContactRelation
33 {
34         /**
35          * No discovery of followers/followings
36          */
37         const DISCOVERY_NONE = 0;
38         /**
39          * Discover followers/followings of local contacts
40          */
41         const DISCOVERY_LOCAL = 1;
42         /**
43          * Discover followers/followings of local contacts and contacts that visibly interacted on the system
44          */
45         const DISCOVERY_INTERACTOR = 2;
46         /**
47          * Discover followers/followings of all contacts
48          */
49         const DISCOVERY_ALL = 3;
50
51         public static function store(int $target, int $actor, string $interaction_date)
52         {
53                 if ($actor == $target) {
54                         return;
55                 }
56
57                 DBA::update('contact-relation', ['last-interaction' => $interaction_date], ['cid' => $target, 'relation-cid' => $actor], true);
58         }
59
60         /**
61          * Fetches the followers of a given profile and adds them
62          *
63          * @param string $url URL of a profile
64          * @return void
65          */
66         public static function discoverByUrl(string $url)
67         {
68                 $contact = Contact::getByURL($url);
69                 if (empty($contact)) {
70                         return;
71                 }
72
73                 if (!self::isDiscoverable($url, $contact)) {
74                         return;
75                 }
76
77                 $apcontact = APContact::getByURL($url, false);
78
79                 if (!empty($apcontact['followers']) && is_string($apcontact['followers'])) {
80                         $followers = ActivityPub::fetchItems($apcontact['followers']);
81                 } else {
82                         $followers = [];
83                 }
84
85                 if (!empty($apcontact['following']) && is_string($apcontact['following'])) {
86                         $followings = ActivityPub::fetchItems($apcontact['following']);
87                 } else {
88                         $followings = [];
89                 }
90
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']]);
94                         return;
95                 }
96
97                 $target = $contact['id'];
98
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]);
102                 }
103
104                 $contacts = [];
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'];
110                         }
111                 }
112                 $contacts = array_unique($contacts);
113
114                 $follower_counter = 0;
115                 $following_counter = 0;
116
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);
124                                         $follower_counter++;
125                                 }
126
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++;
131                                 }
132                         }
133                 }
134
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]);
138                 }
139
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]);
142                 return;
143         }
144
145         /**
146          * Tests if a given contact url is discoverable
147          *
148          * @param string $url     Contact url
149          * @param array  $contact Contact array
150          * @return boolean True if contact is discoverable
151          */
152         public static function isDiscoverable(string $url, array $contact = [])
153         {
154                 $contact_discovery = DI::config()->get('system', 'contact_discovery');
155
156                 if ($contact_discovery == self::DISCOVERY_NONE) {
157                         return false;
158                 }
159
160                 if (empty($contact)) {
161                         $contact = Contact::getByURL($url);
162                 }
163
164                 if (empty($contact)) {
165                         return false;
166                 }
167
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']]);
170                         return false;
171                 }
172
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]);
177                                 return false;
178                         }
179
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]);
184                                         return false;
185                                 }
186                         }
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']]);
190                         return false;
191                 }
192
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']]);
197                                 return false;
198                         }
199                 }
200
201                 return true;
202         }
203 }