]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Notifications.php
Move API Response methods into an own class to make them mockable
[friendica.git] / src / Module / Api / Mastodon / Notifications.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\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          * @param array $parameters
42          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
43          */
44         public static function rawContent(array $parameters = [])
45         {
46                 self::checkAllowedScope(self::SCOPE_READ);
47                 $uid = self::getCurrentUserID();
48
49                 if (!empty($parameters['id'])) {
50                         $id = $parameters['id'];
51                         try {
52                                 $notification = DI::notification()->selectOneForUser($uid, ['id' => $id]);
53                                 System::jsonExit(DI::mstdnNotification()->createFromNotification($notification));
54                         } catch (\Exception $e) {
55                                 DI::mstdnError()->RecordNotFound();
56                         }
57                 }
58
59                 $request = self::getRequest([
60                         'max_id'        => 0,     // Return results older than this ID
61                         'since_id'      => 0,     // Return results newer than this ID
62                         'min_id'        => 0,     // Return results immediately newer than this ID
63                         'limit'         => 20,    // Maximum number of results to return (default 20)
64                         'exclude_types' => [],    // Array of types to exclude (follow, favourite, reblog, mention, poll, follow_request)
65                         'account_id'    => 0,     // Return only notifications received from this account
66                         'with_muted'    => false, // Pleroma extension: return activities by muted (not by blocked!) users.
67                         'count'         => 0,     // Unknown parameter
68                 ]);
69
70                 $params = ['order' => ['id' => true]];
71
72                 $condition = ['uid' => $uid, 'seen' => false];
73
74                 if (!empty($request['account_id'])) {
75                         $contact = Contact::getById($request['account_id'], ['url']);
76                         if (!empty($contact['url'])) {
77                                 $condition['url'] = $contact['url'];
78                         }
79                 }
80
81                 if (in_array(Notification::TYPE_INTRODUCTION, $request['exclude_types'])) {
82                         $condition = DBA::mergeConditions($condition,
83                                 ["(`vid` != ? OR `type` != ? OR NOT EXISTS (SELECT `id` FROM `contact` WHERE `id` = `actor-id` AND `pending`))",
84                                 Verb::getID(Activity::FOLLOW), Post\UserNotification::TYPE_NONE]);
85                 }
86
87                 if (in_array(Notification::TYPE_FOLLOW, $request['exclude_types'])) {
88                         $condition = DBA::mergeConditions($condition,
89                                 ["(`vid` != ? OR `type` != ? OR NOT EXISTS (SELECT `id` FROM `contact` WHERE `id` = `actor-id` AND NOT `pending`))",
90                                 Verb::getID(Activity::FOLLOW), Post\UserNotification::TYPE_NONE]);
91                 }
92
93                 if (in_array(Notification::TYPE_LIKE, $request['exclude_types'])) {
94                         $condition = DBA::mergeConditions($condition, [
95                                 "(NOT `vid` IN (?, ?) OR NOT `type` IN (?, ?))",
96                                 Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE),
97                                 Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_THREAD_COMMENT
98                         ]);
99                 }
100
101                 if (in_array(Notification::TYPE_RESHARE, $request['exclude_types'])) {
102                         $condition = DBA::mergeConditions($condition, [
103                                 "(NOT `vid` IN (?) OR NOT `type` IN (?, ?))",
104                                 Verb::getID(Activity::ANNOUNCE),
105                                 Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_THREAD_COMMENT
106                         ]);
107                 }
108
109                 if (in_array(Notification::TYPE_MENTION, $request['exclude_types'])) {
110                         $condition = DBA::mergeConditions($condition, [
111                                 "(NOT `vid` IN (?) OR NOT `type` IN (?, ?, ?, ?, ?))",
112                                 Verb::getID(Activity::POST), Post\UserNotification::TYPE_EXPLICIT_TAGGED,
113                                 Post\UserNotification::TYPE_IMPLICIT_TAGGED, Post\UserNotification::TYPE_DIRECT_COMMENT,
114                                 Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT, Post\UserNotification::TYPE_THREAD_COMMENT]);
115                 }
116
117                 if (in_array(Notification::TYPE_POST, $request['exclude_types'])) {
118                         $condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?) OR NOT `type` IN (?))",
119                                 Verb::getID(Activity::POST), Post\UserNotification::TYPE_SHARED]);
120                 }
121
122                 $mstdnNotifications = [];
123
124                 $Notifications = DI::notification()->selectByBoundaries(
125                         $condition,
126                         $params,
127                         $request['min_id'] ?: $request['since_id'],
128                         $request['max_id'],
129                         $request['limit']
130                 );
131
132                 foreach($Notifications as $Notification) {
133                         try {
134                                 $mstdnNotifications[] = DI::mstdnNotification()->createFromNotification($Notification);
135                                 self::setBoundaries($Notification->id);
136                         } catch (\Exception $e) {
137                                 // Skip this notification
138                         }
139                 }
140
141                 self::setLinkHeader();
142                 System::jsonExit($mstdnNotifications);
143         }
144 }