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