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