]> git.mxchange.org Git - friendica.git/blob - src/Module/OStatus/Subscribe.php
Issue 13052: The limit parameter now behaves like the Mastodon counterpart
[friendica.git] / src / Module / OStatus / Subscribe.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Module\OStatus;
23
24 use Friendica\App;
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;
38
39 class Subscribe extends \Friendica\BaseModule
40 {
41         /** @var IHandleUserSessions */
42         private $session;
43         /** @var SystemMessages */
44         private $systemMessages;
45         /** @var ICanSendHttpRequests */
46         private $httpClient;
47         /** @var IManagePersonalConfigValues */
48         private $pConfig;
49         /** @var App\Page */
50         private $page;
51
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 = [])
53         {
54                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
55
56                 $this->session        = $session;
57                 $this->systemMessages = $systemMessages;
58                 $this->httpClient     = $httpClient;
59                 $this->pConfig        = $pConfig;
60                 $this->page           = $page;
61         }
62
63         protected function content(array $request = []): string
64         {
65                 if (!$this->session->getLocalUserId()) {
66                         $this->systemMessages->addNotice($this->t('Permission denied.'));
67                         $this->baseUrl->redirect('login');
68                 }
69
70                 $o = '<h2>' . $this->t('Subscribing to contacts') . '</h2>';
71
72                 $uid = $this->session->getLocalUserId();
73
74                 $counter = intval($request['counter'] ?? 0);
75
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.');
80                         }
81
82                         $contact = Contact::getByURL($request['url']);
83                         if (!$contact) {
84                                 $this->pConfig->delete($uid, 'ostatus', 'legacy_contact');
85                                 return $o . $this->t('Couldn\'t fetch information for contact.');
86                         }
87
88                         if ($contact['network'] == Protocol::OSTATUS) {
89                                 $api = $contact['baseurl'] . '/api/';
90
91                                 // Fetching friends
92                                 $curlResult = $this->httpClient->get($api . 'statuses/friends.json?screen_name=' . $contact['nick'], HttpClientAccept::JSON);
93
94                                 if (!$curlResult->isSuccess()) {
95                                         $this->pConfig->delete($uid, 'ostatus', 'legacy_contact');
96                                         return $o . $this->t('Couldn\'t fetch friends for contact.');
97                                 }
98
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.');
103                                 }
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.');
109                                 }
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.');
114                                 }
115                                 $this->pConfig->set($uid, 'ostatus', 'legacy_friends', json_encode($followings));
116                         } else {
117                                 $this->pConfig->delete($uid, 'ostatus', 'legacy_contact');
118                                 return $o . $this->t('Unsupported network');
119                         }
120                 }
121
122                 $friends = json_decode($this->pConfig->get($uid, 'ostatus', 'legacy_friends'));
123
124                 if (empty($friends)) {
125                         $friends = [];
126                 }
127
128                 $total = sizeof($friends);
129
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');
135                         return $o;
136                 }
137
138                 $friend = $friends[$counter++];
139
140                 $url = $friend->statusnet_profile_url ?? $friend;
141
142                 $o .= '<p>' . $counter . '/' . $total . ': ' . $url;
143
144                 $probed = Contact::getByURL($url);
145                 if (!empty($probed['network']) && in_array($probed['network'], Protocol::FEDERATED)) {
146                         $result = Contact::createFromProbeForUser($this->session->getLocalUserId(), $probed['url']);
147                         if ($result['success']) {
148                                 $o .= ' - ' . $this->t('success');
149                         } else {
150                                 $o .= ' - ' . $this->t('failed');
151                         }
152                 } else {
153                         $o .= ' - ' . $this->t('ignored');
154                 }
155
156                 $o .= '</p>';
157
158                 $o .= '<p>' . $this->t('Keep this window open until done.') . '</p>';
159
160                 $this->page['htmlhead'] = '<meta http-equiv="refresh" content="0; URL=' . $this->baseUrl . '/ostatus/subscribe?counter=' . $counter . '">';
161
162                 return $o;
163         }
164 }