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([
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
64 $params = ['order' => ['id' => true], 'limit' => $request['limit']];
66 $condition = ['uid' => $uid, 'seen' => false, 'type' => []];
68 if (!empty($request['account_id'])) {
69 $contact = Contact::getById($request['account_id'], ['url']);
70 if (!empty($contact['url'])) {
71 $condition['url'] = $contact['url'];
75 if (!in_array('follow_request', $request['exclude_types'])) {
76 $condition['type'] = array_merge($condition['type'], [Notification\Type::INTRO]);
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]);
84 if (!in_array('status', $request['exclude_types'])) {
85 $condition['type'] = array_merge($condition['type'], [Notification\Type::SHARE]);
88 if (!empty($request['max_id'])) {
89 $condition = DBA::mergeConditions($condition, ["`id` < ?", $request['max_id']]);
92 if (!empty($request['since_id'])) {
93 $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['since_id']]);
96 if (!empty($request['min_id'])) {
97 $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['min_id']]);
99 $params['order'] = ['id'];
104 $notify = DBA::select('notify', ['id'], $condition, $params);
105 while ($notification = DBA::fetch($notify)) {
106 $notifications[] = DI::mstdnNotification()->createFromNotifyId($notification['id']);
109 if (!empty($request['min_id'])) {
110 array_reverse($notifications);
113 System::jsonExit($notifications);