]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Notifications.php
Merge pull request #10275 from very-ape/authenticate-hook
[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\Notification;
29 use Friendica\Module\BaseApi;
30
31 /**
32  * @see https://docs.joinmastodon.org/methods/notifications/
33  */
34 class Notifications extends BaseApi
35 {
36         /**
37          * @param array $parameters
38          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
39          */
40         public static function rawContent(array $parameters = [])
41         {
42                 self::login(self::SCOPE_READ);
43                 $uid = self::getCurrentUserID();
44
45                 if (!empty($parameters['id'])) {
46                         $id = $parameters['id'];
47                         if (!DBA::exists('notify', ['id' => $id, 'uid' => $uid])) {
48                                 DI::mstdnError()->RecordNotFound();
49                         }
50                         System::jsonExit(DI::mstdnNotification()->createFromNotifyId($id));
51                 }
52
53                 $request = self::getRequest([
54                         'max_id'        => 0,     // Return results older than this ID
55                         'since_id'      => 0,     // Return results newer than this ID
56                         'min_id'        => 0,     // Return results immediately newer than this ID
57                         'limit'         => 20,    // Maximum number of results to return (default 20)
58                         'exclude_types' => [],    // Array of types to exclude (follow, favourite, reblog, mention, poll, follow_request)
59                         'account_id'    => 0,     // Return only notifications received from this account
60                         'with_muted'    => false, // Pleroma extension: return activities by muted (not by blocked!) users.
61                         'count'         => 0,     // Unknown parameter
62                 ]);
63
64                 $params = ['order' => ['id' => true], 'limit' => $request['limit']];
65
66                 $condition = ['uid' => $uid, 'seen' => false, 'type' => []];
67
68                 if (!empty($request['account_id'])) {
69                         $contact = Contact::getById($request['account_id'], ['url']);
70                         if (!empty($contact['url'])) {
71                                 $condition['url'] = $contact['url'];
72                         }
73                 }
74
75                 if (!in_array('follow_request', $request['exclude_types'])) {
76                         $condition['type'] = array_merge($condition['type'], [Notification\Type::INTRO]);
77                 }
78
79                 if (!in_array('mention', $request['exclude_types'])) {
80                         $condition['type'] = array_merge($condition['type'],
81                                 [Notification\Type::WALL, Notification\Type::COMMENT, Notification\Type::MAIL, Notification\Type::TAG_SELF, Notification\Type::POKE]);
82                 }
83
84                 if (!in_array('status', $request['exclude_types'])) {
85                         $condition['type'] = array_merge($condition['type'], [Notification\Type::SHARE]);
86                 }
87
88                 if (!empty($request['max_id'])) {
89                         $condition = DBA::mergeConditions($condition, ["`id` < ?", $request['max_id']]);
90                 }
91
92                 if (!empty($request['since_id'])) {
93                         $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['since_id']]);
94                 }
95
96                 if (!empty($request['min_id'])) {
97                         $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['min_id']]);
98
99                         $params['order'] = ['id'];
100                 }
101
102                 $notifications = [];
103
104                 $notify = DBA::select('notify', ['id'], $condition, $params);
105                 while ($notification = DBA::fetch($notify)) {
106                         $notifications[] = DI::mstdnNotification()->createFromNotifyId($notification['id']);
107                 }
108
109                 if (!empty($request['min_id'])) {
110                         array_reverse($notifications);
111                 }
112
113                 System::jsonExit($notifications);
114         }
115 }