]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Notifications.php
Merge pull request #10231 from MrPetovan/bug/warnings
[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/accounts/mutes/
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();
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                 // Return results older than this ID
54                 $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id'];
55
56                 // Return results newer than this ID
57                 $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id'];
58
59                 // Return results immediately newer than this ID
60                 $min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id'];
61
62                 // Maximum number of results to return (default 20)
63                 $limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit'];
64
65                 // Array of types to exclude (follow, favourite, reblog, mention, poll, follow_request)
66                 $exclude_types = $_REQUEST['exclude_types'] ?? [];
67
68                 // Return only notifications received from this account
69                 $account_id = (int)!isset($_REQUEST['account_id']) ? 0 : $_REQUEST['account_id'];
70
71                 $params = ['order' => ['id' => true], 'limit' => $limit];
72
73                 $condition = ['uid' => $uid, 'seen' => false, 'type' => []];
74
75                 if (!empty($account_id)) {
76                         $contact = Contact::getById($account_id, ['url']);
77                         if (!empty($contact['url'])) {
78                                 $condition['url'] = $contact['url'];
79                         }
80                 }
81
82                 if (!in_array('follow_request', $exclude_types)) {
83                         $condition['type'] = array_merge($condition['type'], [Notification\Type::INTRO]);
84                 }
85
86                 if (!in_array('mention', $exclude_types)) {
87                         $condition['type'] = array_merge($condition['type'],
88                                 [Notification\Type::WALL, Notification\Type::COMMENT, Notification\Type::MAIL, Notification\Type::TAG_SELF, Notification\Type::POKE]);
89                 }
90
91                 if (!in_array('status', $exclude_types)) {
92                         $condition['type'] = array_merge($condition['type'], [Notification\Type::SHARE]);
93                 }
94
95                 if (!empty($max_id)) {
96                         $condition = DBA::mergeConditions($condition, ["`id` < ?", $max_id]);
97                 }
98
99                 if (!empty($since_id)) {
100                         $condition = DBA::mergeConditions($condition, ["`id` > ?", $since_id]);
101                 }
102
103                 if (!empty($min_id)) {
104                         $condition = DBA::mergeConditions($condition, ["`id` > ?", $min_id]);
105
106                         $params['order'] = ['id'];
107                 }
108
109                 $notifications = [];
110
111                 $notify = DBA::select('notify', ['id'], $condition, $params);
112                 while ($notification = DBA::fetch($notify)) {
113                         $notifications[] = DI::mstdnNotification()->createFromNotifyId($notification['id']);
114                 }
115
116                 if (!empty($min_id)) {
117                         array_reverse($notifications);
118                 }
119
120                 System::jsonExit($notifications);
121         }
122 }