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