]> git.mxchange.org Git - friendica.git/blob - src/Model/ContactRelation.php
Remove obsolete mod/lockview.php file
[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\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Protocol\ActivityPub;
28 use Friendica\Util\DateTimeFormat;
29 use Friendica\Util\Strings;
30
31 class ContactRelation
32 {
33         /**
34          * No discovery of followers/followings
35          */
36         const DISCOVERY_NONE = 0;
37         /**
38          * Discover followers/followings of local contacts
39          */
40         const DISCOVERY_LOCAL = 1;
41         /**
42          * Discover followers/followings of local contacts and contacts that visibly interacted on the system
43          */
44         const DISCOVERY_INTERACTOR = 2;
45         /**
46          * Discover followers/followings of all contacts
47          */
48         const DISCOVERY_ALL = 3;
49
50         public static function store(int $target, int $actor, string $interaction_date)
51         {
52                 if ($actor == $target) {
53                         return;
54                 }
55
56                 DBA::update('contact-relation', ['last-interaction' => $interaction_date], ['cid' => $target, 'relation-cid' => $actor], true);
57         }
58
59         /**
60          * Fetches the followers of a given profile and adds them
61          *
62          * @param string $url URL of a profile
63          * @return void
64          */
65         public static function discoverByUrl(string $url)
66         {
67                 $contact_discovery = DI::config()->get('system', 'contact_discovery');
68
69                 if ($contact_discovery == self::DISCOVERY_NONE) {
70                         return;
71                 }
72
73                 $contact = Contact::getByURL($url);
74                 if (empty($contact)) {
75                         return;
76                 }
77
78                 if ($contact['last-discovery'] > DateTimeFormat::utc('now - 1 month')) {
79                         Logger::info('No discovery - Last was less than a month ago.', ['id' => $contact['id'], 'url' => $url, 'discovery' => $contact['last-discovery']]);
80                         return;
81                 }
82
83                 if ($contact_discovery != self::DISCOVERY_ALL) {
84                         $local = DBA::exists('contact', ["`nurl` = ? AND `uid` != ?", Strings::normaliseLink($url), 0]);
85                         if (($contact_discovery == self::DISCOVERY_LOCAL) && !$local) {
86                                 Logger::info('No discovery - This contact is not followed/following locally.', ['id' => $contact['id'], 'url' => $url]);
87                                 return;
88                         }
89
90                         if ($contact_discovery == self::DISCOVERY_INTERACTOR) {
91                                 $interactor = DBA::exists('contact-relation', ["`relation-cid` = ? AND `last-interaction` > ?", $contact['id'], DBA::NULL_DATETIME]);
92                                 if (!$local && !$interactor) {
93                                         Logger::info('No discovery - This contact is not interacting locally.', ['id' => $contact['id'], 'url' => $url]);
94                                         return;
95                                 }
96                         }
97                 } elseif ($contact['created'] > DateTimeFormat::utc('now - 1 day')) {
98                         // Newly created contacts are not discovered to avoid DDoS attacks
99                         Logger::info('No discovery - Contact record is less than a day old.', ['id' => $contact['id'], 'url' => $url, 'discovery' => $contact['created']]);
100                         return;
101                 }
102
103                 $apcontact = APContact::getByURL($url);
104
105                 if (!empty($apcontact['followers']) && is_string($apcontact['followers'])) {
106                         $followers = ActivityPub::fetchItems($apcontact['followers']);
107                 } else {
108                         $followers = [];
109                 }
110
111                 if (!empty($apcontact['following']) && is_string($apcontact['following'])) {
112                         $followings = ActivityPub::fetchItems($apcontact['following']);
113                 } else {
114                         $followings = [];
115                 }
116
117                 if (empty($followers) && empty($followings)) {
118                         return;
119                 }
120
121                 $target = $contact['id'];
122
123                 if (!empty($followers)) {
124                         // Clear the follower list, since it will be recreated in the next step
125                         DBA::update('contact-relation', ['follows' => false], ['cid' => $target]);
126                 }
127
128                 $contacts = [];
129                 foreach (array_merge($followers, $followings) as $contact) {
130                         if (is_string($contact)) {
131                                 $contacts[] = $contact;
132                         } elseif (!empty($contact['url']) && is_string($contact['url'])) {
133                                 $contacts[] = $contact['url'];
134                         }
135                 }
136                 $contacts = array_unique($contacts);
137
138                 Logger::info('Discover contacts', ['id' => $target, 'url' => $url, 'contacts' => count($contacts)]);
139                 foreach ($contacts as $contact) {
140                         $actor = Contact::getIdForURL($contact);
141                         if (!empty($actor)) {
142                                 $fields = [];
143                                 if (in_array($contact, $followers)) {
144                                         $fields = ['cid' => $target, 'relation-cid' => $actor];
145                                 } elseif (in_array($contact, $followings)) {
146                                         $fields = ['cid' => $actor, 'relation-cid' => $target];
147                                 } else {
148                                         continue;
149                                 }
150
151                                 DBA::update('contact-relation', ['follows' => true, 'follow-updated' => DateTimeFormat::utcNow()], $fields, true);
152                         }
153                 }
154
155                 if (!empty($followers)) {
156                         // Delete all followers that aren't followers anymore (and aren't interacting)
157                         DBA::delete('contact-relation', ['cid' => $target, 'follows' => false, 'last-interaction' => DBA::NULL_DATETIME]);
158                 }
159
160                 DBA::update('contact', ['last-discovery' => DateTimeFormat::utcNow()], ['id' => $target]);
161                 Logger::info('Contacts discovery finished, "last-discovery" set', ['id' => $target, 'url' => $url]);
162                 return;
163         }
164 }