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