session = $session; $this->systemMessages = $systemMessages; $this->httpClient = $httpClient; $this->pConfig = $pConfig; $this->page = $page; } protected function content(array $request = []): string { if (!$this->session->getLocalUserId()) { $this->systemMessages->addNotice($this->t('Permission denied.')); $this->baseUrl->redirect('login'); } $o = '

' . $this->t('Subscribing to contacts') . '

'; $uid = $this->session->getLocalUserId(); $counter = intval($request['counter'] ?? 0); if ($this->pConfig->get($uid, 'ostatus', 'legacy_friends') == '') { if (empty($request['url'])) { $this->pConfig->delete($uid, 'ostatus', 'legacy_contact'); return $o . $this->t('No contact provided.'); } $contact = Contact::getByURL($request['url']); if (!$contact) { $this->pConfig->delete($uid, 'ostatus', 'legacy_contact'); return $o . $this->t('Couldn\'t fetch information for contact.'); } if ($contact['network'] == Protocol::OSTATUS) { $api = $contact['baseurl'] . '/api/'; // Fetching friends $curlResult = $this->httpClient->get($api . 'statuses/friends.json?screen_name=' . $contact['nick'], HttpClientAccept::JSON, [HttpClientOptions::REQUEST => HttpClientRequest::OSTATUS]); if (!$curlResult->isSuccess()) { $this->pConfig->delete($uid, 'ostatus', 'legacy_contact'); return $o . $this->t('Couldn\'t fetch friends for contact.'); } $friends = $curlResult->getBodyString(); if (empty($friends)) { $this->pConfig->delete($uid, 'ostatus', 'legacy_contact'); return $o . $this->t('Couldn\'t fetch following contacts.'); } $this->pConfig->set($uid, 'ostatus', 'legacy_friends', $friends); } elseif ($apcontact = APContact::getByURL($contact['url'])) { if (empty($apcontact['following'])) { $this->pConfig->delete($uid, 'ostatus', 'legacy_contact'); return $o . $this->t('Couldn\'t fetch remote profile.'); } $followings = ActivityPub::fetchItems($apcontact['following']); if (empty($followings)) { $this->pConfig->delete($uid, 'ostatus', 'legacy_contact'); return $o . $this->t('Couldn\'t fetch following contacts.'); } $this->pConfig->set($uid, 'ostatus', 'legacy_friends', json_encode($followings)); } else { $this->pConfig->delete($uid, 'ostatus', 'legacy_contact'); return $o . $this->t('Unsupported network'); } } $friends = json_decode($this->pConfig->get($uid, 'ostatus', 'legacy_friends')); if (empty($friends)) { $friends = []; } $total = sizeof($friends); if ($counter >= $total) { $this->page['htmlhead'] = ''; $this->pConfig->delete($uid, 'ostatus', 'legacy_friends'); $this->pConfig->delete($uid, 'ostatus', 'legacy_contact'); $o .= $this->t('Done'); return $o; } $friend = $friends[$counter++]; $url = $friend->statusnet_profile_url ?? $friend; $o .= '

' . $counter . '/' . $total . ': ' . $url; $probed = Contact::getByURL($url); if (!empty($probed['network']) && in_array($probed['network'], Protocol::FEDERATED)) { $result = Contact::createFromProbeForUser($this->session->getLocalUserId(), $probed['url']); if ($result['success']) { $o .= ' - ' . $this->t('success'); } else { $o .= ' - ' . $this->t('failed'); } } else { $o .= ' - ' . $this->t('ignored'); } $o .= '

'; $o .= '

' . $this->t('Keep this window open until done.') . '

'; $this->page['htmlhead'] = ''; return $o; } }