3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Notifications;
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;
39 class Notification extends BaseModule
41 /** @var Introduction */
42 private $introductionRepo;
43 /** @var Repository\Notification */
44 private $notificationRepo;
45 /** @var Repository\Notify */
47 /** @var IManagePersonalConfigValues */
49 /** @var Factory\Notification */
50 private $notificationFactory;
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 = [])
54 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
56 $this->introductionRepo = $introductionRepo;
57 $this->notificationRepo = $notificationRepo;
58 $this->notificationFactory = $notificationFactory;
59 $this->notifyRepo = $notifyRepo;
60 $this->pconfig = $pconfig;
66 * @throws HTTPException\InternalServerErrorException
67 * @throws HTTPException\NotFoundException
68 * @throws HTTPException\UnauthorizedException
69 * @throws \ImagickException
72 protected function post(array $request = [])
75 throw new HTTPException\UnauthorizedException($this->l10n->t('Permission denied.'));
78 $request_id = $this->parameters['id'] ?? false;
81 $intro = $this->introductionRepo->selectOneById($request_id, local_user());
83 switch ($_POST['submit']) {
84 case $this->l10n->t('Discard'):
85 Contact\Introduction::discard($intro);
86 $this->introductionRepo->delete($intro);
88 case $this->l10n->t('Ignore'):
90 $this->introductionRepo->save($intro);
94 $this->baseUrl->redirect('notifications/intros');
101 * @throws HTTPException\UnauthorizedException
103 protected function rawContent(array $request = [])
106 throw new HTTPException\UnauthorizedException($this->l10n->t('Permission denied.'));
109 if ($this->args->get(1) === 'mark' && $this->args->get(2) === 'all') {
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]);
118 System::jsonExit(['result' => (($success) ? 'success' : 'fail')]);
125 * Redirect to the notifications main page or to the url for the chosen notifications
127 * @throws HTTPException\NotFoundException In case the notification is either not existing or is not for this user
128 * @throws HTTPException\InternalServerErrorException
131 protected function content(array $request = []): string
134 notice($this->l10n->t('You must be logged in to show this page.'));
135 return Login::form();
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']);
144 $this->baseUrl->redirect('notifications/system');
149 private function handleNotify(int $notifyId)
151 $Notify = $this->notifyRepo->selectOneById($notifyId);
152 if ($Notify->uid !== local_user()) {
153 throw new HTTPException\ForbiddenException();
156 if ($this->pconfig->get(local_user(), 'system', 'detailed_notif')) {
158 $this->notifyRepo->save($Notify);
160 if ($Notify->uriId) {
161 $this->notificationRepo->setAllSeenForUser($Notify->uid, ['target-uri-id' => $Notify->uriId]);
164 $this->notifyRepo->setAllSeenForRelatedNotify($Notify);
167 if ((string)$Notify->link) {
168 System::externalRedirect($Notify->link);
171 $this->baseUrl->redirect();
174 private function handleNotification(int $notificationId)
176 $Notification = $this->notificationRepo->selectOneById($notificationId);
177 if ($Notification->uid !== local_user()) {
178 throw new HTTPException\ForbiddenException();
181 if ($this->pconfig->get(local_user(), 'system', 'detailed_notif')) {
182 $Notification->setSeen();
183 $this->notificationRepo->save($Notification);
185 if ($Notification->parentUriId) {
186 $this->notificationRepo->setAllSeenForUser($Notification->uid, ['parent-uri-id' => $Notification->parentUriId]);
188 $Notification->setSeen();
189 $this->notificationRepo->save($Notification);
193 $message = $this->notificationFactory->getMessageFromNotification($Notification);
195 if ($message['link']) {
196 System::externalRedirect($message['link']);
199 $this->baseUrl->redirect();