]> git.mxchange.org Git - friendica.git/blob - src/Worker/UpdateServerDirectory.php
Continued:
[friendica.git] / src / Worker / UpdateServerDirectory.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, the Friendica project
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\Worker;
23
24 use Friendica\Core\Logger;
25 use Friendica\DI;
26 use Friendica\Model\Contact;
27 use Friendica\Model\GServer;
28 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
29
30 class UpdateServerDirectory
31 {
32         /**
33          * Query the given server for their users
34          *
35          * @param array $gserver Server record
36          */
37         public static function execute(array $gserver)
38         {
39                 if (!DI::config()->get('system', 'poco_discovery')) {
40                         return;
41                 }
42
43                 if ($gserver['directory-type'] == GServer::DT_MASTODON) {
44                         self::discoverMastodonDirectory($gserver);
45                 } elseif (!empty($gserver['poco'])) {
46                         self::discoverPoCo($gserver);
47                 }
48         }
49
50         private static function discoverPoCo(array $gserver)
51         {
52                 $result = DI::httpClient()->fetch($gserver['poco'] . '?fields=urls', HttpClientAccept::JSON);
53                 if (empty($result)) {
54                         Logger::info('Empty result', ['url' => $gserver['url']]);
55                         return;
56                 }
57
58                 $contacts = json_decode($result, true);
59                 if (empty($contacts['entry'])) {
60                         Logger::info('No contacts', ['url' => $gserver['url']]);
61                         return;
62                 }
63
64                 Logger::info('PoCo discovery started', ['poco' => $gserver['poco']]);
65
66                 $urls = [];
67                 foreach (array_column($contacts['entry'], 'urls') as $url_entries) {
68                         foreach ($url_entries as $url_entry) {
69                                 if (empty($url_entry['type']) || empty($url_entry['value'])) {
70                                         continue;
71                                 }
72                                 if ($url_entry['type'] == 'profile') {
73                                         $urls[] = $url_entry['value'];
74                                 }
75                         }
76                 }
77
78                 $result = Contact::addByUrls($urls);
79
80                 Logger::info('PoCo discovery ended', ['count' => $result['count'], 'added' => $result['added'], 'updated' => $result['updated'], 'unchanged' => $result['unchanged'], 'poco' => $gserver['poco']]);
81         }
82
83         private static function discoverMastodonDirectory(array $gserver)
84         {
85                 $result = DI::httpClient()->fetch($gserver['url'] . '/api/v1/directory?order=new&local=true&limit=200&offset=0', HttpClientAccept::JSON);
86                 if (empty($result)) {
87                         Logger::info('Empty result', ['url' => $gserver['url']]);
88                         return;
89                 }
90
91                 $accounts = json_decode($result, true);
92                 if (empty($accounts)) {
93                         Logger::info('No contacts', ['url' => $gserver['url']]);
94                         return;
95                 }
96
97                 Logger::info('Account discovery started', ['url' => $gserver['url']]);
98
99                 $urls = [];
100                 foreach ($accounts as $account) {
101                         if (!empty($account['url'])) {
102                                 $urls[] = $account['url'];
103                         }
104                 }
105
106                 $result = Contact::addByUrls($urls);
107
108                 Logger::info('Account discovery ended', ['count' => $result['count'], 'added' => $result['added'], 'updated' => $result['updated'], 'unchanged' => $result['unchanged'], 'url' => $gserver['url']]);
109         }
110 }