3 * @copyright Copyright (C) 2010-2022, the Friendica project
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\Module\OStatus;
25 use Friendica\Core\L10n;
26 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
27 use Friendica\Core\Protocol;
28 use Friendica\Core\Session\Capability\IHandleUserSessions;
29 use Friendica\Model\APContact;
30 use Friendica\Model\Contact;
31 use Friendica\Module\Response;
32 use Friendica\Navigation\SystemMessages;
33 use Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests;
34 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
35 use Friendica\Protocol\ActivityPub;
36 use Friendica\Util\Profiler;
37 use Psr\Log\LoggerInterface;
39 class Subscribe extends \Friendica\BaseModule
41 /** @var IHandleUserSessions */
43 /** @var SystemMessages */
44 private $systemMessages;
45 /** @var ICanSendHttpRequests */
47 /** @var IManagePersonalConfigValues */
52 public function __construct(App\Page $page, IManagePersonalConfigValues $pConfig, ICanSendHttpRequests $httpClient, SystemMessages $systemMessages, IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
54 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
56 $this->session = $session;
57 $this->systemMessages = $systemMessages;
58 $this->httpClient = $httpClient;
59 $this->pConfig = $pConfig;
63 protected function content(array $request = []): string
65 if (!$this->session->getLocalUserId()) {
66 $this->systemMessages->addNotice($this->t('Permission denied.'));
67 $this->baseUrl->redirect('login');
70 $o = '<h2>' . $this->t('Subscribing to contacts') . '</h2>';
72 $uid = $this->session->getLocalUserId();
74 $counter = intval($request['counter'] ?? 0);
76 if ($this->pConfig->get($uid, 'ostatus', 'legacy_friends') == '') {
77 if (empty($request['url'])) {
78 $this->pConfig->delete($uid, 'ostatus', 'legacy_contact');
79 return $o . $this->t('No contact provided.');
82 $contact = Contact::getByURL($request['url']);
84 $this->pConfig->delete($uid, 'ostatus', 'legacy_contact');
85 return $o . $this->t('Couldn\'t fetch information for contact.');
88 if ($contact['network'] == Protocol::OSTATUS) {
89 $api = $contact['baseurl'] . '/api/';
92 $curlResult = $this->httpClient->get($api . 'statuses/friends.json?screen_name=' . $contact['nick'], HttpClientAccept::JSON);
94 if (!$curlResult->isSuccess()) {
95 $this->pConfig->delete($uid, 'ostatus', 'legacy_contact');
96 return $o . $this->t('Couldn\'t fetch friends for contact.');
99 $friends = $curlResult->getBody();
100 if (empty($friends)) {
101 $this->pConfig->delete($uid, 'ostatus', 'legacy_contact');
102 return $o . $this->t('Couldn\'t fetch following contacts.');
104 $this->pConfig->set($uid, 'ostatus', 'legacy_friends', $friends);
105 } elseif ($apcontact = APContact::getByURL($contact['url'])) {
106 if (empty($apcontact['following'])) {
107 $this->pConfig->delete($uid, 'ostatus', 'legacy_contact');
108 return $o . $this->t('Couldn\'t fetch remote profile.');
110 $followings = ActivityPub::fetchItems($apcontact['following']);
111 if (empty($followings)) {
112 $this->pConfig->delete($uid, 'ostatus', 'legacy_contact');
113 return $o . $this->t('Couldn\'t fetch following contacts.');
115 $this->pConfig->set($uid, 'ostatus', 'legacy_friends', json_encode($followings));
117 $this->pConfig->delete($uid, 'ostatus', 'legacy_contact');
118 return $o . $this->t('Unsupported network');
122 $friends = json_decode($this->pConfig->get($uid, 'ostatus', 'legacy_friends'));
124 if (empty($friends)) {
128 $total = sizeof($friends);
130 if ($counter >= $total) {
131 $this->page['htmlhead'] = '<meta http-equiv="refresh" content="0; URL=' . $this->baseUrl . '/settings/connectors">';
132 $this->pConfig->delete($uid, 'ostatus', 'legacy_friends');
133 $this->pConfig->delete($uid, 'ostatus', 'legacy_contact');
134 $o .= $this->t('Done');
138 $friend = $friends[$counter++];
140 $url = $friend->statusnet_profile_url ?? $friend;
142 $o .= '<p>' . $counter . '/' . $total . ': ' . $url;
144 $probed = Contact::getByURL($url);
145 if (in_array($probed['network'], Protocol::FEDERATED)) {
146 $result = Contact::createFromProbeForUser($this->session->getLocalUserId(), $probed['url']);
147 if ($result['success']) {
148 $o .= ' - ' . $this->t('success');
150 $o .= ' - ' . $this->t('failed');
153 $o .= ' - ' . $this->t('ignored');
158 $o .= '<p>' . $this->t('Keep this window open until done.') . '</p>';
160 $this->page['htmlhead'] = '<meta http-equiv="refresh" content="0; URL=' . $this->baseUrl . '/ostatus/subscribe?counter=' . $counter . '">';