]> git.mxchange.org Git - friendica.git/blob - src/Module/Notifications/Notification.php
Merge pull request #9509 from MrPetovan/task/5616-clear-notifications-display
[friendica.git] / src / Module / Notifications / Notification.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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                                 $success = DI::notify()->setSeen();
82                         } catch (\Exception $e) {
83                                 DI::logger()->warning('set all seen failed.', ['exception' => $e]);
84                                 $success = false;
85                         }
86
87                         System::jsonExit(['result' => (($success) ? 'success' : 'fail')]);
88                 }
89         }
90
91         /**
92          * {@inheritDoc}
93          *
94          * Redirect to the notifications main page or to the url for the chosen notifications
95          *
96          * @throws HTTPException\NotFoundException In case the notification is either not existing or is not for this user
97          * @throws HTTPException\InternalServerErrorException
98          * @throws \Exception
99          */
100         public static function content(array $parameters = [])
101         {
102                 if (!local_user()) {
103                         notice(DI::l10n()->t('You must be logged in to show this page.'));
104                         return Login::form();
105                 }
106
107                 $request_id = $parameters['id'] ?? false;
108
109                 if ($request_id) {
110                         $notify = DI::notify()->getByID($request_id, local_user());
111
112                         if (DI::pConfig()->get(local_user(), 'system', 'detailed_notif')) {
113                                 $notify->seen = true;
114                                 DI::notify()->update($notify);
115                         } else {
116                                 DI::notify()->setSeen(true, $notify);
117                         }
118
119                         if (!empty($notify->link)) {
120                                 System::externalRedirect($notify->link);
121                         }
122
123                         DI::baseUrl()->redirect();
124                 }
125
126                 DI::baseUrl()->redirect('notifications/system');
127
128                 throw new HTTPException\InternalServerErrorException('Invalid situation.');
129         }
130 }