3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Worker;
24 use Friendica\Core\Logger;
25 use Friendica\Database\DBA;
27 use Friendica\Model\Contact;
28 use Friendica\Model\GServer;
30 class UpdateServerDirectory
33 * Query the given server for their users
35 * @param array $gserver Server record
37 public static function execute(array $gserver)
39 $gserver = DBA::selectFirst('gserver', [], ['url' => $gserver['url']]);
40 if ($gserver['directory-type'] == GServer::DT_MASTODON) {
41 self::discoverMastodonDirectory($gserver);
42 } elseif (!empty($gserver['poco'])) {
43 self::discoverPoCo($gserver);
47 private static function discoverPoCo(array $gserver)
49 $result = DI::httpRequest()->fetch($gserver['poco'] . '?fields=urls');
51 Logger::info('Empty result', ['url' => $gserver['url']]);
55 $contacts = json_decode($result, true);
56 if (empty($contacts['entry'])) {
57 Logger::info('No contacts', ['url' => $gserver['url']]);
61 Logger::info('PoCo discovery started', ['poco' => $gserver['poco']]);
64 foreach ($contacts['entry'] as $entry) {
65 foreach ($entry['urls'] as $url_entry) {
66 if (empty($url_entry['type']) || empty($url_entry['value'])) {
69 if ($url_entry['type'] == 'profile') {
70 $urls[] = $url_entry['value'];
75 $result = Contact::addContactsByArray($urls);
77 Logger::info('PoCo discovery ended', ['count' => $result['count'], 'added' => $result['added'], 'updated' => $result['updated'], 'poco' => $gserver['poco']]);
80 private static function discoverMastodonDirectory(array $gserver)
82 $result = DI::httpRequest()->fetch($gserver['url'] . '/api/v1/directory?order=new&local=true&limit=200&offset=0');
84 Logger::info('Empty result', ['url' => $gserver['url']]);
88 $accounts = json_decode($result, true);
89 if (empty($accounts)) {
90 Logger::info('No contacts', ['url' => $gserver['url']]);
94 Logger::info('Account discovery started', ['url' => $gserver['url']]);
97 foreach ($accounts as $account) {
98 if (!empty($account['url'])) {
99 $urls[] = $account['url'];
103 $result = Contact::addContactsByArray($urls);
105 Logger::info('Account discovery ended', ['count' => $result['count'], 'added' => $result['added'], 'updated' => $result['updated'], 'url' => $gserver['url']]);