]> git.mxchange.org Git - friendica.git/blob - src/Module/Notifications/Notification.php
Merge pull request #12026 from annando/no-boot-src-module-2
[friendica.git] / src / Module / Notifications / Notification.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Notifications;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Contact\Introduction\Repository\Introduction;
27 use Friendica\Core\L10n;
28 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
29 use Friendica\Core\Session;
30 use Friendica\Core\System;
31 use Friendica\DI;
32 use Friendica\Model\Contact;
33 use Friendica\Module\Response;
34 use Friendica\Module\Security\Login;
35 use Friendica\Navigation\Notifications\Factory;
36 use Friendica\Navigation\Notifications\Repository;
37 use Friendica\Network\HTTPException;
38 use Friendica\Util\Profiler;
39 use Psr\Log\LoggerInterface;
40
41 class Notification extends BaseModule
42 {
43         /** @var Introduction */
44         private $introductionRepo;
45         /** @var Repository\Notification */
46         private $notificationRepo;
47         /** @var Repository\Notify */
48         private $notifyRepo;
49         /** @var IManagePersonalConfigValues */
50         private $pconfig;
51         /** @var Factory\Notification */
52         private $notificationFactory;
53
54         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 = [])
55         {
56                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
57
58                 $this->introductionRepo    = $introductionRepo;
59                 $this->notificationRepo    = $notificationRepo;
60                 $this->notificationFactory = $notificationFactory;
61                 $this->notifyRepo          = $notifyRepo;
62                 $this->pconfig             = $pconfig;
63         }
64
65         /**
66          * {@inheritDoc}
67          *
68          * @throws HTTPException\InternalServerErrorException
69          * @throws HTTPException\NotFoundException
70          * @throws HTTPException\UnauthorizedException
71          * @throws \ImagickException
72          * @throws \Exception
73          */
74         protected function post(array $request = [])
75         {
76                 if (!Session::getLocalUser()) {
77                         throw new HTTPException\UnauthorizedException($this->l10n->t('Permission denied.'));
78                 }
79
80                 $request_id = $this->parameters['id'] ?? false;
81
82                 if ($request_id) {
83                         $intro = $this->introductionRepo->selectOneById($request_id, Session::getLocalUser());
84
85                         switch ($_POST['submit']) {
86                                 case $this->l10n->t('Discard'):
87                                         Contact\Introduction::discard($intro);
88                                         $this->introductionRepo->delete($intro);
89                                         break;
90                                 case $this->l10n->t('Ignore'):
91                                         $intro->ignore();
92                                         $this->introductionRepo->save($intro);
93                                         break;
94                         }
95
96                         $this->baseUrl->redirect('notifications/intros');
97                 }
98         }
99
100         /**
101          * {@inheritDoc}
102          *
103          * @throws HTTPException\UnauthorizedException
104          */
105         protected function rawContent(array $request = [])
106         {
107                 if (!Session::getLocalUser()) {
108                         throw new HTTPException\UnauthorizedException($this->l10n->t('Permission denied.'));
109                 }
110
111                 if ($this->args->get(1) === 'mark' && $this->args->get(2) === 'all') {
112                         try {
113                                 $this->notificationRepo->setAllSeenForUser(Session::getLocalUser());
114                                 $success = $this->notifyRepo->setAllSeenForUser(Session::getLocalUser());
115                         } catch (\Exception $e) {
116                                 $this->logger->warning('set all seen failed.', ['exception' => $e]);
117                                 $success = false;
118                         }
119
120                         System::jsonExit(['result' => (($success) ? 'success' : 'fail')]);
121                 }
122         }
123
124         /**
125          * {@inheritDoc}
126          *
127          * Redirect to the notifications main page or to the url for the chosen notifications
128          *
129          * @throws HTTPException\NotFoundException In case the notification is either not existing or is not for this user
130          * @throws HTTPException\InternalServerErrorException
131          * @throws \Exception
132          */
133         protected function content(array $request = []): string
134         {
135                 if (!Session::getLocalUser()) {
136                         DI::sysmsg()->addNotice($this->l10n->t('You must be logged in to show this page.'));
137                         return Login::form();
138                 }
139
140                 if (isset($this->parameters['notify_id'])) {
141                         $this->handleNotify($this->parameters['notify_id']);
142                 } elseif (isset($this->parameters['id'])) {
143                         $this->handleNotification($this->parameters['id']);
144                 }
145
146                 $this->baseUrl->redirect('notifications/system');
147
148                 return '';
149         }
150
151         private function handleNotify(int $notifyId)
152         {
153                 $Notify = $this->notifyRepo->selectOneById($notifyId);
154                 if ($Notify->uid !== Session::getLocalUser()) {
155                         throw new HTTPException\ForbiddenException();
156                 }
157
158                 if ($this->pconfig->get(Session::getLocalUser(), 'system', 'detailed_notif')) {
159                         $Notify->setSeen();
160                         $this->notifyRepo->save($Notify);
161                 } else {
162                         if ($Notify->uriId) {
163                                 $this->notificationRepo->setAllSeenForUser($Notify->uid, ['target-uri-id' => $Notify->uriId]);
164                         }
165
166                         $this->notifyRepo->setAllSeenForRelatedNotify($Notify);
167                 }
168
169                 if ((string)$Notify->link) {
170                         System::externalRedirect($Notify->link);
171                 }
172
173                 $this->baseUrl->redirect();
174         }
175
176         private function handleNotification(int $notificationId)
177         {
178                 $Notification = $this->notificationRepo->selectOneById($notificationId);
179                 if ($Notification->uid !== Session::getLocalUser()) {
180                         throw new HTTPException\ForbiddenException();
181                 }
182
183                 if ($this->pconfig->get(Session::getLocalUser(), 'system', 'detailed_notif')) {
184                         $Notification->setSeen();
185                         $this->notificationRepo->save($Notification);
186                 } else {
187                         if ($Notification->parentUriId) {
188                                 $this->notificationRepo->setAllSeenForUser($Notification->uid, ['parent-uri-id' => $Notification->parentUriId]);
189                         } else {
190                                 $Notification->setSeen();
191                                 $this->notificationRepo->save($Notification);
192                         }
193                 }
194
195                 $message = $this->notificationFactory->getMessageFromNotification($Notification);
196
197                 if ($message['link']) {
198                         System::externalRedirect($message['link']);
199                 }
200
201                 $this->baseUrl->redirect();
202         }
203 }