]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Notifications.php
Renamed function
[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::login(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, ["NOT `vid` IN (?)", Verb::getID(Activity::FOLLOW)]);
79                 }
80
81                 if (in_array('favourite', $request['exclude_types'])) {
82                         $condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?, ?) OR NOT `type` IN (?, ?))",
83                                 Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE),
84                                 Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT]);
85                 }
86
87                 if (in_array('reblog', $request['exclude_types'])) {
88                         $condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?) OR NOT `type` IN (?, ?))",
89                                 Verb::getID(Activity::ANNOUNCE),
90                                 Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT]);
91                 }
92
93                 if (in_array('mention', $request['exclude_types'])) {
94                         $condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?) OR NOT `type` IN (?, ?, ?, ?, ?))",
95                                 Verb::getID(Activity::POST), Post\UserNotification::NOTIF_EXPLICIT_TAGGED,
96                                 Post\UserNotification::NOTIF_IMPLICIT_TAGGED, Post\UserNotification::NOTIF_DIRECT_COMMENT,
97                                 Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT]);
98                 }
99
100                 if (in_array('status', $request['exclude_types'])) {
101                         $condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?) OR NOT `type` IN (?))",
102                                 Verb::getID(Activity::POST), Post\UserNotification::NOTIF_SHARED]);
103                 }
104
105                 if (!empty($request['max_id'])) {
106                         $condition = DBA::mergeConditions($condition, ["`id` < ?", $request['max_id']]);
107                 }
108
109                 if (!empty($request['since_id'])) {
110                         $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['since_id']]);
111                 }
112
113                 if (!empty($request['min_id'])) {
114                         $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['min_id']]);
115
116                         $params['order'] = ['id'];
117                 }
118
119                 $notifications = [];
120
121                 $notify = DBA::select('notification', ['id'], $condition, $params);
122                 while ($notification = DBA::fetch($notify)) {
123                         $entry = DI::mstdnNotification()->createFromNotificationId($notification['id']);
124                         if (!empty($entry)) {
125                                 $notifications[] = $entry;
126                         }
127                 }
128
129                 if (!empty($request['min_id'])) {
130                         array_reverse($notifications);
131                 }
132
133                 System::jsonExit($notifications);
134         }
135 }