]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Notifications.php
Merge branch 'stable' into develop
[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\Protocol\Activity;
32
33 /**
34  * @see https://docs.joinmastodon.org/methods/notifications/
35  */
36 class Notifications extends BaseApi
37 {
38         /**
39          * @param array $parameters
40          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
41          */
42         public static function rawContent(array $parameters = [])
43         {
44                 self::checkAllowedScope(self::SCOPE_READ);
45                 $uid = self::getCurrentUserID();
46
47                 if (!empty($parameters['id'])) {
48                         $id = $parameters['id'];
49                         if (!DBA::exists('notification', ['id' => $id, 'uid' => $uid])) {
50                                 DI::mstdnError()->RecordNotFound();
51                         }
52                         System::jsonExit(DI::mstdnNotification()->createFromNotificationId($id));
53                 }
54
55                 $request = self::getRequest([
56                         'max_id'        => 0,     // Return results older than this ID
57                         'since_id'      => 0,     // Return results newer than this ID
58                         'min_id'        => 0,     // Return results immediately newer than this ID
59                         'limit'         => 20,    // Maximum number of results to return (default 20)
60                         'exclude_types' => [],    // Array of types to exclude (follow, favourite, reblog, mention, poll, follow_request)
61                         'account_id'    => 0,     // Return only notifications received from this account
62                         'with_muted'    => false, // Pleroma extension: return activities by muted (not by blocked!) users.
63                         'count'         => 0,     // Unknown parameter
64                 ]);
65
66                 $params = ['order' => ['id' => true], 'limit' => $request['limit']];
67
68                 $condition = ['uid' => $uid, 'seen' => false];
69
70                 if (!empty($request['account_id'])) {
71                         $contact = Contact::getById($request['account_id'], ['url']);
72                         if (!empty($contact['url'])) {
73                                 $condition['url'] = $contact['url'];
74                         }
75                 }
76
77                 if (in_array('follow_request', $request['exclude_types'])) {
78                         $condition = DBA::mergeConditions($condition,
79                                 ["(`vid` != ? OR `type` != ? OR NOT EXISTS (SELECT `id` FROM `contact` WHERE `id` = `actor-id` AND `pending`))",
80                                 Verb::getID(Activity::FOLLOW), Post\UserNotification::NOTIF_NONE]);
81                 }
82
83                 if (in_array('follow', $request['exclude_types'])) {
84                         $condition = DBA::mergeConditions($condition,
85                                 ["(`vid` != ? OR `type` != ? OR NOT EXISTS (SELECT `id` FROM `contact` WHERE `id` = `actor-id` AND NOT `pending`))",
86                                 Verb::getID(Activity::FOLLOW), Post\UserNotification::NOTIF_NONE]);
87                 }
88
89                 if (in_array('favourite', $request['exclude_types'])) {
90                         $condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?, ?) OR NOT `type` IN (?, ?))",
91                                 Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE),
92                                 Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT]);
93                 }
94
95                 if (in_array('reblog', $request['exclude_types'])) {
96                         $condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?) OR NOT `type` IN (?, ?))",
97                                 Verb::getID(Activity::ANNOUNCE),
98                                 Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT]);
99                 }
100
101                 if (in_array('mention', $request['exclude_types'])) {
102                         $condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?) OR NOT `type` IN (?, ?, ?, ?, ?))",
103                                 Verb::getID(Activity::POST), Post\UserNotification::NOTIF_EXPLICIT_TAGGED,
104                                 Post\UserNotification::NOTIF_IMPLICIT_TAGGED, Post\UserNotification::NOTIF_DIRECT_COMMENT,
105                                 Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT]);
106                 }
107
108                 if (in_array('status', $request['exclude_types'])) {
109                         $condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?) OR NOT `type` IN (?))",
110                                 Verb::getID(Activity::POST), Post\UserNotification::NOTIF_SHARED]);
111                 }
112
113                 if (!empty($request['max_id'])) {
114                         $condition = DBA::mergeConditions($condition, ["`id` < ?", $request['max_id']]);
115                 }
116
117                 if (!empty($request['since_id'])) {
118                         $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['since_id']]);
119                 }
120
121                 if (!empty($request['min_id'])) {
122                         $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['min_id']]);
123
124                         $params['order'] = ['id'];
125                 }
126
127                 $notifications = [];
128
129                 $notify = DBA::select('notification', ['id'], $condition, $params);
130                 while ($notification = DBA::fetch($notify)) {
131                         self::setBoundaries($notification['id']);
132                         $entry = DI::mstdnNotification()->createFromNotificationId($notification['id']);
133                         if (!empty($entry)) {
134                                 $notifications[] = $entry;
135                         }
136                 }
137
138                 if (!empty($request['min_id'])) {
139                         array_reverse($notifications);
140                 }
141
142                 self::setLinkHeader();
143                 System::jsonExit($notifications);
144         }
145 }