3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Api\Mastodon;
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Notification;
29 use Friendica\Module\BaseApi;
32 * @see https://docs.joinmastodon.org/methods/notifications/
34 class Notifications extends BaseApi
37 * @param array $parameters
38 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
40 public static function rawContent(array $parameters = [])
42 self::login(self::SCOPE_READ);
43 $uid = self::getCurrentUserID();
45 if (!empty($parameters['id'])) {
46 $id = $parameters['id'];
47 if (!DBA::exists('notify', ['id' => $id, 'uid' => $uid])) {
48 DI::mstdnError()->RecordNotFound();
50 System::jsonExit(DI::mstdnNotification()->createFromNotifyId($id));
53 $request = self::getRequest(['max_id' => 0, 'since_id' => 0, 'min_id' => 0, 'limit' => 20,
54 'exclude_types' => [], 'account_id' => 0, 'with_muted' => false]);
56 // Return results older than this ID
57 $max_id = $request['max_id'];
59 // Return results newer than this ID
60 $since_id = $request['since_id'];
62 // Return results immediately newer than this ID
63 $min_id = $request['min_id'];
65 // Maximum number of results to return (default 20)
66 $limit = $request['limit'];
68 // Array of types to exclude (follow, favourite, reblog, mention, poll, follow_request)
69 $exclude_types = $request['exclude_types'];
71 // Return only notifications received from this account
72 $account_id = $request['account_id'];
75 $with_muted = $request['with_muted'];
77 $params = ['order' => ['id' => true], 'limit' => $limit];
79 $condition = ['uid' => $uid, 'seen' => false, 'type' => []];
81 if (!empty($account_id)) {
82 $contact = Contact::getById($account_id, ['url']);
83 if (!empty($contact['url'])) {
84 $condition['url'] = $contact['url'];
88 if (!in_array('follow_request', $exclude_types)) {
89 $condition['type'] = array_merge($condition['type'], [Notification\Type::INTRO]);
92 if (!in_array('mention', $exclude_types)) {
93 $condition['type'] = array_merge($condition['type'],
94 [Notification\Type::WALL, Notification\Type::COMMENT, Notification\Type::MAIL, Notification\Type::TAG_SELF, Notification\Type::POKE]);
97 if (!in_array('status', $exclude_types)) {
98 $condition['type'] = array_merge($condition['type'], [Notification\Type::SHARE]);
101 if (!empty($max_id)) {
102 $condition = DBA::mergeConditions($condition, ["`id` < ?", $max_id]);
105 if (!empty($since_id)) {
106 $condition = DBA::mergeConditions($condition, ["`id` > ?", $since_id]);
109 if (!empty($min_id)) {
110 $condition = DBA::mergeConditions($condition, ["`id` > ?", $min_id]);
112 $params['order'] = ['id'];
117 $notify = DBA::select('notify', ['id'], $condition, $params);
118 while ($notification = DBA::fetch($notify)) {
119 $notifications[] = DI::mstdnNotification()->createFromNotifyId($notification['id']);
122 if (!empty($min_id)) {
123 array_reverse($notifications);
126 System::jsonExit($notifications);