]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Notifications.php
Merge pull request #12837 from HankG/fix-blocks-ignores-in-full-context-status-request
[friendica.git] / src / Module / Api / Mastodon / Notifications.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Api\Mastodon;
23
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Post;
29 use Friendica\Model\Verb;
30 use Friendica\Module\BaseApi;
31 use Friendica\Navigation\Notifications\Entity;
32 use Friendica\Object\Api\Mastodon\Notification;
33 use Friendica\Protocol\Activity;
34
35 /**
36  * @see https://docs.joinmastodon.org/methods/notifications/
37  */
38 class Notifications extends BaseApi
39 {
40         /**
41          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
42          */
43         protected function rawContent(array $request = [])
44         {
45                 self::checkAllowedScope(self::SCOPE_READ);
46                 $uid = self::getCurrentUserID();
47
48                 if (!empty($this->parameters['id'])) {
49                         $id = $this->parameters['id'];
50                         try {
51                                 $notification = DI::notification()->selectOneForUser($uid, ['id' => $id]);
52                                 System::jsonExit(DI::mstdnNotification()->createFromNotification($notification, self::appSupportsQuotes()));
53                         } catch (\Exception $e) {
54                                 DI::mstdnError()->RecordNotFound();
55                         }
56                 }
57
58                 $request = $this->getRequest([
59                         'max_id'        => 0,     // Return results older than this ID
60                         'since_id'      => 0,     // Return results newer than this ID
61                         'min_id'        => 0,     // Return results immediately newer than this ID
62                         'limit'         => 20,    // Maximum number of results to return (default 20)
63                         'exclude_types' => [],    // Array of types to exclude (follow, favourite, reblog, mention, poll, follow_request)
64                         'account_id'    => 0,     // Return only notifications received from this account
65                         'with_muted'    => false, // Pleroma extension: return activities by muted (not by blocked!) users.
66                         'include_all'   => false,  // Include dismissed and undismissed
67                         'summary'       => false,
68                 ], $request);
69
70                 $params = ['order' => ['id' => true]];
71
72                 $condition = ["`uid` = ? AND (NOT `type` IN (?, ?))", $uid,
73                         Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION,
74                         Post\UserNotification::TYPE_COMMENT_PARTICIPATION];
75
76                 if (!$request['include_all']) {
77                         $condition = DBA::mergeConditions($condition, ['dismissed' => false]);
78                 }
79
80                 if (!empty($request['account_id'])) {
81                         $contact = Contact::getById($request['account_id'], ['url']);
82                         if (!empty($contact['url'])) {
83                                 $condition['url'] = $contact['url'];
84                         }
85                 }
86
87                 if (in_array(Notification::TYPE_INTRODUCTION, $request['exclude_types'])) {
88                         $condition = DBA::mergeConditions(
89                                 $condition,
90                                 ["(`vid` != ? OR `type` != ? OR NOT `actor-id` IN (SELECT `id` FROM `contact` WHERE `pending`))",
91                                         Verb::getID(Activity::FOLLOW),
92                                         Post\UserNotification::TYPE_NONE]
93                         );
94                 }
95
96                 if (in_array(Notification::TYPE_FOLLOW, $request['exclude_types'])) {
97                         $condition = DBA::mergeConditions(
98                                 $condition,
99                                 ["(`vid` != ? OR `type` != ? OR NOT `actor-id` IN (SELECT `id` FROM `contact` WHERE NOT `pending`))",
100                                         Verb::getID(Activity::FOLLOW),
101                                         Post\UserNotification::TYPE_NONE]
102                         );
103                 }
104
105                 if (in_array(Notification::TYPE_LIKE, $request['exclude_types'])) {
106                         $condition = DBA::mergeConditions($condition, [
107                                 "(NOT `vid` IN (?, ?) OR NOT `type` IN (?, ?))",
108                                 Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE),
109                                 Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_THREAD_COMMENT
110                         ]);
111                 }
112
113                 if (in_array(Notification::TYPE_RESHARE, $request['exclude_types'])) {
114                         $condition = DBA::mergeConditions($condition, [
115                                 "(NOT `vid` IN (?) OR NOT `type` IN (?, ?))",
116                                 Verb::getID(Activity::ANNOUNCE),
117                                 Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_THREAD_COMMENT
118                         ]);
119                 }
120
121                 if (in_array(Notification::TYPE_MENTION, $request['exclude_types'])) {
122                         $condition = DBA::mergeConditions($condition, [
123                                 "(NOT `vid` IN (?) OR NOT `type` IN (?, ?, ?, ?, ?))",
124                                 Verb::getID(Activity::POST), Post\UserNotification::TYPE_EXPLICIT_TAGGED,
125                                 Post\UserNotification::TYPE_IMPLICIT_TAGGED, Post\UserNotification::TYPE_DIRECT_COMMENT,
126                                 Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT, Post\UserNotification::TYPE_THREAD_COMMENT]);
127                 }
128
129                 if (in_array(Notification::TYPE_POST, $request['exclude_types'])) {
130                         $condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?) OR NOT `type` IN (?))",
131                                 Verb::getID(Activity::POST), Post\UserNotification::TYPE_SHARED]);
132                 }
133
134                 if ($request['summary']) {
135                         $count = DI::notification()->countForUser($uid, $condition);
136                         System::jsonExit(['count' => $count]);
137                 } else {
138                         $mstdnNotifications = [];
139
140                         $Notifications = DI::notification()->selectByBoundaries(
141                                 $condition,
142                                 $params,
143                                 $request['min_id'] ?: $request['since_id'],
144                                 $request['max_id'],
145                                 $request['limit']
146                         );
147
148                         foreach ($Notifications as $Notification) {
149                                 try {
150                                         $mstdnNotifications[] = DI::mstdnNotification()->createFromNotification($Notification, self::appSupportsQuotes());
151                                         self::setBoundaries($Notification->id);
152                                 } catch (\Exception $e) {
153                                         // Skip this notification
154                                 }
155                         }
156
157                         self::setLinkHeader();
158                         System::jsonExit($mstdnNotifications);
159                 }
160         }
161 }