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;
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;
40 class Notification extends BaseModule
42 /** @var Introduction */
43 private $introductionRepo;
44 /** @var Repository\Notification */
45 private $notificationRepo;
46 /** @var Repository\Notify */
48 /** @var IManagePersonalConfigValues */
50 /** @var Factory\Notification */
51 private $notificationFactory;
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 = [])
55 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
57 $this->introductionRepo = $introductionRepo;
58 $this->notificationRepo = $notificationRepo;
59 $this->notificationFactory = $notificationFactory;
60 $this->notifyRepo = $notifyRepo;
61 $this->pconfig = $pconfig;
67 * @throws HTTPException\InternalServerErrorException
68 * @throws HTTPException\NotFoundException
69 * @throws HTTPException\UnauthorizedException
70 * @throws \ImagickException
73 protected function post(array $request = [])
75 if (!DI::userSession()->getLocalUserId()) {
76 throw new HTTPException\UnauthorizedException($this->l10n->t('Permission denied.'));
79 $request_id = $this->parameters['id'] ?? false;
82 $intro = $this->introductionRepo->selectOneById($request_id, DI::userSession()->getLocalUserId());
84 switch ($_POST['submit']) {
85 case $this->l10n->t('Discard'):
86 Contact\Introduction::discard($intro);
87 $this->introductionRepo->delete($intro);
89 case $this->l10n->t('Ignore'):
91 $this->introductionRepo->save($intro);
95 $this->baseUrl->redirect('notifications/intros');
102 * @throws HTTPException\UnauthorizedException
104 protected function rawContent(array $request = [])
106 if (!DI::userSession()->getLocalUserId()) {
107 throw new HTTPException\UnauthorizedException($this->l10n->t('Permission denied.'));
110 if ($this->args->get(1) === 'mark' && $this->args->get(2) === 'all') {
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]);
119 System::jsonExit(['result' => (($success) ? 'success' : 'fail')]);
126 * Redirect to the notifications main page or to the url for the chosen notifications
128 * @throws HTTPException\NotFoundException In case the notification is either not existing or is not for this user
129 * @throws HTTPException\InternalServerErrorException
132 protected function content(array $request = []): string
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();
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']);
145 $this->baseUrl->redirect('notifications/system');
150 private function handleNotify(int $notifyId)
152 $Notify = $this->notifyRepo->selectOneById($notifyId);
153 if ($Notify->uid !== DI::userSession()->getLocalUserId()) {
154 throw new HTTPException\ForbiddenException();
157 if ($this->pconfig->get(DI::userSession()->getLocalUserId(), 'system', 'detailed_notif')) {
159 $this->notifyRepo->save($Notify);
161 if ($Notify->uriId) {
162 $this->notificationRepo->setAllSeenForUser($Notify->uid, ['target-uri-id' => $Notify->uriId]);
165 $this->notifyRepo->setAllSeenForRelatedNotify($Notify);
168 if ((string)$Notify->link) {
169 System::externalRedirect($Notify->link);
172 $this->baseUrl->redirect();
175 private function handleNotification(int $notificationId)
177 $Notification = $this->notificationRepo->selectOneById($notificationId);
178 if ($Notification->uid !== DI::userSession()->getLocalUserId()) {
179 throw new HTTPException\ForbiddenException();
182 if ($this->pconfig->get(DI::userSession()->getLocalUserId(), 'system', 'detailed_notif')) {
183 $Notification->setSeen();
184 $this->notificationRepo->save($Notification);
186 if ($Notification->parentUriId) {
187 $this->notificationRepo->setAllSeenForUser($Notification->uid, ['parent-uri-id' => $Notification->parentUriId]);
189 $Notification->setSeen();
190 $this->notificationRepo->save($Notification);
194 $message = $this->notificationFactory->getMessageFromNotification($Notification);
196 if ($message['link']) {
197 System::externalRedirect($message['link']);
200 $this->baseUrl->redirect();