]> git.mxchange.org Git - friendica.git/blob - src/Model/ContactRelation.php
Merge pull request #8939 from MrPetovan/task/8906-frio-viewas-redesign
[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_discovery = DI::config()->get('system', 'contact_discovery');
69
70                 if ($contact_discovery == self::DISCOVERY_NONE) {
71                         return;
72                 }
73
74                 $contact = Contact::getByURL($url);
75                 if (empty($contact)) {
76                         return;
77                 }
78
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']]);
81                         return;
82                 }
83
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]);
88                                 return;
89                         }
90
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]);
95                                         return;
96                                 }
97                         }
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']]);
101                         return;
102                 }
103
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);
107                 } else {
108                         // The contact isn't obviously speaking AP, so we don't allow updating
109                         $apcontact = APContact::getByURL($url, false);
110                 }
111
112                 if (!empty($apcontact['followers']) && is_string($apcontact['followers'])) {
113                         $followers = ActivityPub::fetchItems($apcontact['followers']);
114                 } else {
115                         $followers = [];
116                 }
117
118                 if (!empty($apcontact['following']) && is_string($apcontact['following'])) {
119                         $followings = ActivityPub::fetchItems($apcontact['following']);
120                 } else {
121                         $followings = [];
122                 }
123
124                 if (empty($followers) && empty($followings)) {
125                         return;
126                 }
127
128                 $target = $contact['id'];
129
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]);
133                 }
134
135                 $contacts = [];
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'];
141                         }
142                 }
143                 $contacts = array_unique($contacts);
144
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)) {
149                                 $fields = [];
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];
154                                 } else {
155                                         continue;
156                                 }
157
158                                 DBA::update('contact-relation', ['follows' => true, 'follow-updated' => DateTimeFormat::utcNow()], $fields, true);
159                         }
160                 }
161
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]);
165                 }
166
167                 DBA::update('contact', ['last-discovery' => DateTimeFormat::utcNow()], ['id' => $target]);
168                 Logger::info('Contacts discovery finished, "last-discovery" set', ['id' => $target, 'url' => $url]);
169                 return;
170         }
171 }