]> git.mxchange.org Git - friendica.git/commitdiff
Add legacy notification endpoint for deprecated Notify entity
authorHypolite Petovan <hypolite@mrpetovan.com>
Thu, 3 Mar 2022 14:07:37 +0000 (09:07 -0500)
committerHypolite Petovan <hypolite@mrpetovan.com>
Mon, 14 Mar 2022 11:56:21 +0000 (07:56 -0400)
- Add dependencies to Notifications\Notification module class

mod/ping.php
src/Module/Notifications/Notification.php
src/Navigation/Notifications/Factory/FormattedNotify.php
src/Navigation/Notifications/Repository/Notify.php
static/routes.config.php
tests/datasets/api.fixture.php
tests/src/Module/Api/Friendica/NotificationTest.php

index 1afab5f726794adc2bffc1a23133492ce631c179..d5b43d6ea44516aedd0d9dd74e9d5bf8f5afc6f9 100644 (file)
@@ -442,7 +442,7 @@ function ping_get_notifications($uid)
                                DBA::update('notify', ['name_cache' => $notification["name"], 'msg_cache' => $notification["message"]], ['id' => $notification["id"]]);
                        }
 
-                       $notification["href"] = DI::baseUrl() . "/notification/" . $notification["id"];
+                       $notification["href"] = DI::baseUrl() . "/notify/" . $notification["id"];
 
                        if ($notification["visible"]
                                && !$notification["deleted"]
index d72e3c2502709ddad804d67d11d6e2da8dff49af..a28b5d6f65c102505c435bf1863eeaf1f3661c4c 100644 (file)
 
 namespace Friendica\Module\Notifications;
 
+use Friendica\App;
 use Friendica\BaseModule;
+use Friendica\Contact\Introduction\Repository\Introduction;
+use Friendica\Core\L10n;
+use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
 use Friendica\Core\System;
-use Friendica\DI;
 use Friendica\Model\Contact;
+use Friendica\Module\Response;
 use Friendica\Module\Security\Login;
+use Friendica\Navigation\Notifications\Factory;
+use Friendica\Navigation\Notifications\Repository;
 use Friendica\Network\HTTPException;
+use Friendica\Util\Profiler;
+use Psr\Log\LoggerInterface;
 
-/**
- * Interacting with the /notification command
- */
 class Notification extends BaseModule
 {
+       /** @var Introduction */
+       private $introductionRepo;
+       /** @var Repository\Notification */
+       private $notificationRepo;
+       /** @var Repository\Notify */
+       private $notifyRepo;
+       /** @var IManagePersonalConfigValues */
+       private $pconfig;
+       /** @var Factory\Notification */
+       private $notificationFactory;
+
+       public function __construct(Introduction $introductionRepo, Repository\Notification $notificationRepo, Factory\Notification $notificationFactory, Repository\Notify $notifyRepo, IManagePersonalConfigValues $pconfig, 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->introductionRepo    = $introductionRepo;
+               $this->notificationRepo    = $notificationRepo;
+               $this->notificationFactory = $notificationFactory;
+               $this->notifyRepo          = $notifyRepo;
+               $this->pconfig             = $pconfig;
+       }
+
        /**
         * {@inheritDoc}
         *
@@ -45,26 +72,26 @@ class Notification extends BaseModule
        protected function post(array $request = [])
        {
                if (!local_user()) {
-                       throw new HTTPException\UnauthorizedException(DI::l10n()->t('Permission denied.'));
+                       throw new HTTPException\UnauthorizedException($this->l10n->t('Permission denied.'));
                }
 
                $request_id = $this->parameters['id'] ?? false;
 
                if ($request_id) {
-                       $intro = DI::intro()->selectOneById($request_id, local_user());
+                       $intro = $this->introductionRepo->selectOneById($request_id, local_user());
 
                        switch ($_POST['submit']) {
-                               case DI::l10n()->t('Discard'):
+                               case $this->l10n->t('Discard'):
                                        Contact\Introduction::discard($intro);
-                                       DI::intro()->delete($intro);
+                                       $this->introductionRepo->delete($intro);
                                        break;
-                               case DI::l10n()->t('Ignore'):
+                               case $this->l10n->t('Ignore'):
                                        $intro->ignore();
-                                       DI::intro()->save($intro);
+                                       $this->introductionRepo->save($intro);
                                        break;
                        }
 
-                       DI::baseUrl()->redirect('notifications/intros');
+                       $this->baseUrl->redirect('notifications/intros');
                }
        }
 
@@ -76,15 +103,15 @@ class Notification extends BaseModule
        protected function rawContent(array $request = [])
        {
                if (!local_user()) {
-                       throw new HTTPException\UnauthorizedException(DI::l10n()->t('Permission denied.'));
+                       throw new HTTPException\UnauthorizedException($this->l10n->t('Permission denied.'));
                }
 
-               if (DI::args()->get(1) === 'mark' && DI::args()->get(2) === 'all') {
+               if ($this->args->get(1) === 'mark' && $this->args->get(2) === 'all') {
                        try {
-                               DI::notification()->setAllSeenForUser(local_user());
-                               $success = DI::notify()->setAllSeenForUser(local_user());
+                               $this->notificationRepo->setAllSeenForUser(local_user());
+                               $success = $this->notifyRepo->setAllSeenForUser(local_user());
                        } catch (\Exception $e) {
-                               DI::logger()->warning('set all seen failed.', ['exception' => $e]);
+                               $this->logger->warning('set all seen failed.', ['exception' => $e]);
                                $success = false;
                        }
 
@@ -104,38 +131,71 @@ class Notification extends BaseModule
        protected function content(array $request = []): string
        {
                if (!local_user()) {
-                       notice(DI::l10n()->t('You must be logged in to show this page.'));
+                       notice($this->l10n->t('You must be logged in to show this page.'));
                        return Login::form();
                }
 
-               $request_id = $this->parameters['id'] ?? false;
+               if (isset($this->parameters['notify_id'])) {
+                       $this->handleNotify($this->parameters['notify_id']);
+               } elseif (isset($this->parameters['id'])) {
+                       $this->handleNotification($this->parameters['id']);
+               }
 
-               if ($request_id) {
-                       $Notify = DI::notify()->selectOneById($request_id);
-                       if ($Notify->uid !== local_user()) {
-                               throw new HTTPException\ForbiddenException();
-                       }
+               $this->baseUrl->redirect('notifications/system');
 
-                       if (DI::pConfig()->get(local_user(), 'system', 'detailed_notif')) {
-                               $Notify->setSeen();
-                               DI::notify()->save($Notify);
-                       } else {
-                               if ($Notify->uriId) {
-                                       DI::notification()->setAllSeenForUser($Notify->uid, ['target-uri-id' => $Notify->uriId]);
-                               }
+               return '';
+       }
 
-                               DI::notify()->setAllSeenForRelatedNotify($Notify);
-                       }
+       private function handleNotify(int $notifyId)
+       {
+               $Notify = $this->notifyRepo->selectOneById($notifyId);
+               if ($Notify->uid !== local_user()) {
+                       throw new HTTPException\ForbiddenException();
+               }
 
-                       if ((string)$Notify->link) {
-                               System::externalRedirect($Notify->link);
+               if ($this->pconfig->get(local_user(), 'system', 'detailed_notif')) {
+                       $Notify->setSeen();
+                       $this->notifyRepo->save($Notify);
+               } else {
+                       if ($Notify->uriId) {
+                               $this->notificationRepo->setAllSeenForUser($Notify->uid, ['target-uri-id' => $Notify->uriId]);
                        }
 
-                       DI::baseUrl()->redirect();
+                       $this->notifyRepo->setAllSeenForRelatedNotify($Notify);
                }
 
-               DI::baseUrl()->redirect('notifications/system');
+               if ((string)$Notify->link) {
+                       System::externalRedirect($Notify->link);
+               }
 
-               return '';
+               $this->baseUrl->redirect();
+       }
+
+       private function handleNotification(int $notificationId)
+       {
+               $Notification = $this->notificationRepo->selectOneById($notificationId);
+               if ($Notification->uid !== local_user()) {
+                       throw new HTTPException\ForbiddenException();
+               }
+
+               if ($this->pconfig->get(local_user(), 'system', 'detailed_notif')) {
+                       $Notification->setSeen();
+                       $this->notificationRepo->save($Notification);
+               } else {
+                       if ($Notification->parentUriId) {
+                               $this->notificationRepo->setAllSeenForUser($Notification->uid, ['parent-uri-id' => $Notification->parentUriId]);
+                       } else {
+                               $Notification->setSeen();
+                               $this->notificationRepo->save($Notification);
+                       }
+               }
+
+               $message = $this->notificationFactory->getMessageFromNotification($Notification);
+
+               if ($message['link']) {
+                       System::externalRedirect($message['link']);
+               }
+
+               $this->baseUrl->redirect();
        }
 }
index 2551de1a142d9c5d731dbc999480c01b02aadfe0..d4aba639c564468a5d9159a56d73ffd6280b1128 100644 (file)
@@ -214,7 +214,7 @@ class FormattedNotify extends BaseFactory
                        foreach ($Notifies as $Notify) {
                                $formattedNotifications[] = new ValueObject\FormattedNotify(
                                        'notification',
-                                       $this->baseUrl->get(true) . '/notification/' . $Notify->id,
+                                       $this->baseUrl->get(true) . '/notify/' . $Notify->id,
                                        Contact::getAvatarUrlForUrl($Notify->url, $Notify->uid, Proxy::SIZE_MICRO),
                                        $Notify->url,
                                        strip_tags(BBCode::toPlaintext($Notify->msg)),
index eb65ed4a41b8d3a25a5ecec01f76aa9d203eb80d..9773b6446aed29d2275e9e2f78ed10cc481f8685 100644 (file)
@@ -570,7 +570,7 @@ class Notify extends BaseRepository
                        $Notify->updateMsgFromPreamble($epreamble);
                        $Notify = $this->save($Notify);
 
-                       $itemlink  = $this->baseUrl->get() . '/notification/' . $Notify->id;
+                       $itemlink  = $this->baseUrl->get() . '/notify/' . $Notify->id;
                        $notify_id = $Notify->id;
                }
 
index e10ec2aedff3684c3f0de795960a1c64a7171c15..bc3f15e4e1c76015bb8c049652af69483f1e9cad 100644 (file)
@@ -449,6 +449,8 @@ return [
                '/{id:\d+}' => [Module\Notifications\Notification::class, [R::GET, R::POST]],
        ],
 
+       '/notify/{notify_id:\d+}' => [Module\Notifications\Notification::class, [R::GET]],
+
        '/oauth' => [
                '/acknowledge' => [Module\OAuth\Acknowledge::class, [R::GET, R::POST]],
                '/authorize'   => [Module\OAuth\Authorize::class,   [R::GET]],
index 1b52baa3546977c57fe80ec0b519b205d36552fb..ace928b090b43ca1d07487f569c9bf3c61d5b0ce 100644 (file)
@@ -903,18 +903,18 @@ return [
                [
                        'id'         => 1,
                        'type'       => 8,
-                       'name'       => 'Reply to',
-                       'url'        => 'http://localhost/display/1',
+                       'name'       => 'Friend contact',
+                       'url'        => 'http://localhost/profile/friendcontact',
                        'photo'      => 'http://localhost/',
                        'date'       => '2020-01-01 12:12:02',
                        'msg'        => 'A test reply from an item',
                        'uid'        => 42,
-                       'link'       => 'http://localhost/notification/1',
+                       'link'       => 'http://localhost/display/1',
                        'iid'        => 4,
                        'seen'       => 0,
                        'verb'       => \Friendica\Protocol\Activity::POST,
                        'otype'      => Notification\ObjectType::ITEM,
-                       'name_cache' => 'Reply to',
+                       'name_cache' => 'Friend contact',
                        'msg_cache'  => 'A test reply from an item',
                ],
        ],
index 4e8509b4ebcaa2ee16e3ec17d5ceb909abcf34b7..3c17471b034fd573ce18c8ed7f5515a7f4468860 100644 (file)
@@ -62,7 +62,7 @@ class NotificationTest extends ApiTest
                $assertXml = <<<XML
 <?xml version="1.0"?>
 <notes>
-  <note date="$date" date_rel="$dateRel" id="1" iid="4" link="http://localhost/notification/1" msg="A test reply from an item" msg_cache="A test reply from an item" msg_html="A test reply from an item" msg_plain="A test reply from an item" name="Reply to" name_cache="Reply to" otype="item" parent="" photo="http://localhost/" seen="false" timestamp="1577880722" type="8" uid="42" url="http://localhost/display/1" verb="http://activitystrea.ms/schema/1.0/post"/>
+  <note date="$date" date_rel="$dateRel" id="1" iid="4" link="http://localhost/display/1" msg="A test reply from an item" msg_cache="A test reply from an item" msg_html="A test reply from an item" msg_plain="A test reply from an item" name="Friend contact" name_cache="Friend contact" otype="item" parent="" photo="http://localhost/" seen="false" timestamp="1577880722" type="8" uid="42" url="http://localhost/profile/friendcontact" verb="http://activitystrea.ms/schema/1.0/post"/>
 </notes>
 XML;