From c7dbe0b610387a37cc0dc04dc7fcc0f710afc479 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Mon, 7 Nov 2022 21:44:18 -0500 Subject: [PATCH] Create new OStatus\Repair module class - Create corresponding template file --- mod/settings.php | 2 +- src/Module/OStatus/Repair.php | 93 +++++++++++++++++++++++++++++++ static/routes.config.php | 2 + view/templates/ostatus/repair.tpl | 18 ++++++ 4 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 src/Module/OStatus/Repair.php create mode 100644 view/templates/ostatus/repair.tpl diff --git a/mod/settings.php b/mod/settings.php index 45f5f96c54..259981541b 100644 --- a/mod/settings.php +++ b/mod/settings.php @@ -331,7 +331,7 @@ function settings_content(App $a) '$attach_link_title' => ['attach_link_title', DI::l10n()->t('Attach the link title'), $attach_link_title, DI::l10n()->t('When activated, the title of the attached link will be added as a title on posts to Diaspora. This is mostly helpful with "remote-self" contacts that share feed content.')], '$legacy_contact' => ['legacy_contact', DI::l10n()->t('Your legacy ActivityPub/GNU Social account'), $legacy_contact, DI::l10n()->t("If you enter your old account name from an ActivityPub based system or your GNU Social/Statusnet account name here (in the format user@domain.tld), your contacts will be added automatically. The field will be emptied when done.")], - '$repair_ostatus_url' => DI::baseUrl() . '/repair_ostatus', + '$repair_ostatus_url' => 'ostatus/repair', '$repair_ostatus_text' => DI::l10n()->t('Repair OStatus subscriptions'), '$connector_settings_forms' => $connector_settings_forms, diff --git a/src/Module/OStatus/Repair.php b/src/Module/OStatus/Repair.php new file mode 100644 index 0000000000..b7920c2abc --- /dev/null +++ b/src/Module/OStatus/Repair.php @@ -0,0 +1,93 @@ +. + * + */ + +namespace Friendica\Module\OStatus; + +use Friendica\App; +use Friendica\Core\L10n; +use Friendica\Core\Protocol; +use Friendica\Core\Renderer; +use Friendica\Core\Session\Capability\IHandleUserSessions; +use Friendica\Database\Database; +use Friendica\Model\Contact; +use Friendica\Module\Response; +use Friendica\Navigation\SystemMessages; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; + +class Repair extends \Friendica\BaseModule +{ + /** @var IHandleUserSessions */ + private $session; + /** @var SystemMessages */ + private $systemMessages; + /** @var Database */ + private $database; + /** @var App\Page */ + private $page; + + public function __construct(App\Page $page, Database $database, SystemMessages $systemMessages, IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) + { + parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->session = $session; + $this->systemMessages = $systemMessages; + $this->database = $database; + $this->page = $page; + } + + protected function content(array $request = []): string + { + if (!$this->session->getLocalUserId()) { + $this->systemMessages->addNotice($this->t('Permission denied.')); + $this->baseUrl->redirect('login'); + } + + $uid = $this->session->getLocalUserId(); + + $counter = intval($request['counter'] ?? 0); + + $condition = ['uid' => $uid, 'network' => Protocol::OSTATUS, 'rel' => [Contact::FRIEND, Contact::SHARING]]; + $total = $this->database->count('contact', $condition); + if ($total) { + $contacts = Contact::selectToArray(['url'], $condition, ['order' => ['url'], 'limit' => [$counter++, 1]]); + if ($contacts) { + Contact::createFromProbeForUser($this->session->getLocalUserId(), $contacts[0]['url']); + + $this->page['htmlhead'] .= ''; + } + } + + $tpl = Renderer::getMarkupTemplate('ostatus/repair.tpl'); + + return Renderer::replaceMacros($tpl, [ + '$l10n' => [ + 'title' => $this->t('Resubscribing to OStatus contacts'), + 'keep' => $this->t('Keep this window open until done.'), + 'done' => $this->t('✔ Done'), + 'nocontacts' => $this->t('No OStatus contacts to resubscribe to.'), + ], + '$total' => $total, + '$counter' => $counter, + '$contact' => $contacts[0] ?? null, + ]); + } +} diff --git a/static/routes.config.php b/static/routes.config.php index 773b8d2531..e64b138307 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -522,6 +522,8 @@ return [ '/openid' => [Module\Security\OpenID::class, [R::GET]], '/opensearch' => [Module\OpenSearch::class, [R::GET]], + '/ostatus/repair' => [Module\OStatus\Repair::class, [R::GET]], + '/parseurl' => [Module\ParseUrl::class, [R::GET]], '/permission/tooltip/{type}/{id:\d+}' => [Module\PermissionTooltip::class, [R::GET]], diff --git a/view/templates/ostatus/repair.tpl b/view/templates/ostatus/repair.tpl new file mode 100644 index 0000000000..06298adcfe --- /dev/null +++ b/view/templates/ostatus/repair.tpl @@ -0,0 +1,18 @@ +
+

{{$l10n.title}}

+{{if $total}} + {{if $contact}} +
+ {{$counter}} / {{$total}} : {{$contact.url}} +
+ {{else}} +
+ {{$l10n.done}} +
+ {{/if}} +{{else}} +
+ {{$l10n.nocontacts}} +
+{{/if}} +
-- 2.39.5