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\Session;
30 use Friendica\Core\System;
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;
41 class Notification extends BaseModule
43 /** @var Introduction */
44 private $introductionRepo;
45 /** @var Repository\Notification */
46 private $notificationRepo;
47 /** @var Repository\Notify */
49 /** @var IManagePersonalConfigValues */
51 /** @var Factory\Notification */
52 private $notificationFactory;
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 = [])
56 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
58 $this->introductionRepo = $introductionRepo;
59 $this->notificationRepo = $notificationRepo;
60 $this->notificationFactory = $notificationFactory;
61 $this->notifyRepo = $notifyRepo;
62 $this->pconfig = $pconfig;
68 * @throws HTTPException\InternalServerErrorException
69 * @throws HTTPException\NotFoundException
70 * @throws HTTPException\UnauthorizedException
71 * @throws \ImagickException
74 protected function post(array $request = [])
76 if (!Session::getLocalUser()) {
77 throw new HTTPException\UnauthorizedException($this->l10n->t('Permission denied.'));
80 $request_id = $this->parameters['id'] ?? false;
83 $intro = $this->introductionRepo->selectOneById($request_id, Session::getLocalUser());
85 switch ($_POST['submit']) {
86 case $this->l10n->t('Discard'):
87 Contact\Introduction::discard($intro);
88 $this->introductionRepo->delete($intro);
90 case $this->l10n->t('Ignore'):
92 $this->introductionRepo->save($intro);
96 $this->baseUrl->redirect('notifications/intros');
103 * @throws HTTPException\UnauthorizedException
105 protected function rawContent(array $request = [])
107 if (!Session::getLocalUser()) {
108 throw new HTTPException\UnauthorizedException($this->l10n->t('Permission denied.'));
111 if ($this->args->get(1) === 'mark' && $this->args->get(2) === 'all') {
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]);
120 System::jsonExit(['result' => (($success) ? 'success' : 'fail')]);
127 * Redirect to the notifications main page or to the url for the chosen notifications
129 * @throws HTTPException\NotFoundException In case the notification is either not existing or is not for this user
130 * @throws HTTPException\InternalServerErrorException
133 protected function content(array $request = []): string
135 if (!Session::getLocalUser()) {
136 DI::sysmsg()->addNotice($this->l10n->t('You must be logged in to show this page.'));
137 return Login::form();
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']);
146 $this->baseUrl->redirect('notifications/system');
151 private function handleNotify(int $notifyId)
153 $Notify = $this->notifyRepo->selectOneById($notifyId);
154 if ($Notify->uid !== Session::getLocalUser()) {
155 throw new HTTPException\ForbiddenException();
158 if ($this->pconfig->get(Session::getLocalUser(), 'system', 'detailed_notif')) {
160 $this->notifyRepo->save($Notify);
162 if ($Notify->uriId) {
163 $this->notificationRepo->setAllSeenForUser($Notify->uid, ['target-uri-id' => $Notify->uriId]);
166 $this->notifyRepo->setAllSeenForRelatedNotify($Notify);
169 if ((string)$Notify->link) {
170 System::externalRedirect($Notify->link);
173 $this->baseUrl->redirect();
176 private function handleNotification(int $notificationId)
178 $Notification = $this->notificationRepo->selectOneById($notificationId);
179 if ($Notification->uid !== Session::getLocalUser()) {
180 throw new HTTPException\ForbiddenException();
183 if ($this->pconfig->get(Session::getLocalUser(), 'system', 'detailed_notif')) {
184 $Notification->setSeen();
185 $this->notificationRepo->save($Notification);
187 if ($Notification->parentUriId) {
188 $this->notificationRepo->setAllSeenForUser($Notification->uid, ['parent-uri-id' => $Notification->parentUriId]);
190 $Notification->setSeen();
191 $this->notificationRepo->save($Notification);
195 $message = $this->notificationFactory->getMessageFromNotification($Notification);
197 if ($message['link']) {
198 System::externalRedirect($message['link']);
201 $this->baseUrl->redirect();