]> git.mxchange.org Git - friendica.git/blob - src/Module/Notifications/Notification.php
Use rawContent for Special Options to avoid a protected options() method
[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\BaseModule;
25 use Friendica\Core\System;
26 use Friendica\DI;
27 use Friendica\Model\Contact;
28 use Friendica\Module\Security\Login;
29 use Friendica\Network\HTTPException;
30
31 /**
32  * Interacting with the /notification command
33  */
34 class Notification extends BaseModule
35 {
36         /**
37          * {@inheritDoc}
38          *
39          * @throws HTTPException\InternalServerErrorException
40          * @throws HTTPException\NotFoundException
41          * @throws HTTPException\UnauthorizedException
42          * @throws \ImagickException
43          * @throws \Exception
44          */
45         protected function post(array $request = [])
46         {
47                 if (!local_user()) {
48                         throw new HTTPException\UnauthorizedException(DI::l10n()->t('Permission denied.'));
49                 }
50
51                 $request_id = $this->parameters['id'] ?? false;
52
53                 if ($request_id) {
54                         $intro = DI::intro()->selectOneById($request_id, local_user());
55
56                         switch ($_POST['submit']) {
57                                 case DI::l10n()->t('Discard'):
58                                         Contact\Introduction::discard($intro);
59                                         DI::intro()->delete($intro);
60                                         break;
61                                 case DI::l10n()->t('Ignore'):
62                                         $intro->ignore();
63                                         DI::intro()->save($intro);
64                                         break;
65                         }
66
67                         DI::baseUrl()->redirect('notifications/intros');
68                 }
69         }
70
71         /**
72          * {@inheritDoc}
73          *
74          * @throws HTTPException\UnauthorizedException
75          */
76         protected function rawContent(array $request = [])
77         {
78                 if (!local_user()) {
79                         throw new HTTPException\UnauthorizedException(DI::l10n()->t('Permission denied.'));
80                 }
81
82                 if (DI::args()->get(1) === 'mark' && DI::args()->get(2) === 'all') {
83                         try {
84                                 DI::notification()->setAllSeenForUser(local_user());
85                                 $success = DI::notify()->setAllSeenForUser(local_user());
86                         } catch (\Exception $e) {
87                                 DI::logger()->warning('set all seen failed.', ['exception' => $e]);
88                                 $success = false;
89                         }
90
91                         System::jsonExit(['result' => (($success) ? 'success' : 'fail')]);
92                 }
93         }
94
95         /**
96          * {@inheritDoc}
97          *
98          * Redirect to the notifications main page or to the url for the chosen notifications
99          *
100          * @throws HTTPException\NotFoundException In case the notification is either not existing or is not for this user
101          * @throws HTTPException\InternalServerErrorException
102          * @throws \Exception
103          */
104         protected function content(array $request = []): string
105         {
106                 if (!local_user()) {
107                         notice(DI::l10n()->t('You must be logged in to show this page.'));
108                         return Login::form();
109                 }
110
111                 $request_id = $this->parameters['id'] ?? false;
112
113                 if ($request_id) {
114                         $Notify = DI::notify()->selectOneById($request_id);
115                         if ($Notify->uid !== local_user()) {
116                                 throw new HTTPException\ForbiddenException();
117                         }
118
119                         if (DI::pConfig()->get(local_user(), 'system', 'detailed_notif')) {
120                                 $Notify->setSeen();
121                                 DI::notify()->save($Notify);
122                         } else {
123                                 if ($Notify->uriId) {
124                                         DI::notification()->setAllSeenForUser($Notify->uid, ['target-uri-id' => $Notify->uriId]);
125                                 }
126
127                                 DI::notify()->setAllSeenForRelatedNotify($Notify);
128                         }
129
130                         if ((string)$Notify->link) {
131                                 System::externalRedirect($Notify->link);
132                         }
133
134                         DI::baseUrl()->redirect();
135                 }
136
137                 DI::baseUrl()->redirect('notifications/system');
138
139                 return '';
140         }
141 }