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