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\Post;
29 use Friendica\Model\Verb;
30 use Friendica\Module\BaseApi;
31 use Friendica\Protocol\Activity;
34 * @see https://docs.joinmastodon.org/methods/notifications/
36 class Notifications extends BaseApi
39 * @param array $parameters
40 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
42 public static function rawContent(array $parameters = [])
44 self::checkAllowedScope(self::SCOPE_READ);
45 $uid = self::getCurrentUserID();
47 if (!empty($parameters['id'])) {
48 $id = $parameters['id'];
49 if (!DBA::exists('notification', ['id' => $id, 'uid' => $uid])) {
50 DI::mstdnError()->RecordNotFound();
52 System::jsonExit(DI::mstdnNotification()->createFromNotificationId($id));
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
66 $params = ['order' => ['id' => true], 'limit' => $request['limit']];
68 $condition = ['uid' => $uid, 'seen' => false];
70 if (!empty($request['account_id'])) {
71 $contact = Contact::getById($request['account_id'], ['url']);
72 if (!empty($contact['url'])) {
73 $condition['url'] = $contact['url'];
77 if (in_array('follow_request', $request['exclude_types'])) {
78 $condition = DBA::mergeConditions($condition,
79 ["(`vid` != ? OR `type` != ? OR NOT EXISTS (SELECT `id` FROM `contact` WHERE `id` = `actor-id` AND `pending`))",
80 Verb::getID(Activity::FOLLOW), Post\UserNotification::NOTIF_NONE]);
83 if (in_array('follow', $request['exclude_types'])) {
84 $condition = DBA::mergeConditions($condition,
85 ["(`vid` != ? OR `type` != ? OR NOT EXISTS (SELECT `id` FROM `contact` WHERE `id` = `actor-id` AND NOT `pending`))",
86 Verb::getID(Activity::FOLLOW), Post\UserNotification::NOTIF_NONE]);
89 if (in_array('favourite', $request['exclude_types'])) {
90 $condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?, ?) OR NOT `type` IN (?, ?))",
91 Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE),
92 Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT]);
95 if (in_array('reblog', $request['exclude_types'])) {
96 $condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?) OR NOT `type` IN (?, ?))",
97 Verb::getID(Activity::ANNOUNCE),
98 Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT]);
101 if (in_array('mention', $request['exclude_types'])) {
102 $condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?) OR NOT `type` IN (?, ?, ?, ?, ?))",
103 Verb::getID(Activity::POST), Post\UserNotification::NOTIF_EXPLICIT_TAGGED,
104 Post\UserNotification::NOTIF_IMPLICIT_TAGGED, Post\UserNotification::NOTIF_DIRECT_COMMENT,
105 Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT]);
108 if (in_array('status', $request['exclude_types'])) {
109 $condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?) OR NOT `type` IN (?))",
110 Verb::getID(Activity::POST), Post\UserNotification::NOTIF_SHARED]);
113 if (!empty($request['max_id'])) {
114 $condition = DBA::mergeConditions($condition, ["`id` < ?", $request['max_id']]);
117 if (!empty($request['since_id'])) {
118 $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['since_id']]);
121 if (!empty($request['min_id'])) {
122 $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['min_id']]);
124 $params['order'] = ['id'];
129 $notify = DBA::select('notification', ['id'], $condition, $params);
130 while ($notification = DBA::fetch($notify)) {
131 $entry = DI::mstdnNotification()->createFromNotificationId($notification['id']);
132 if (!empty($entry)) {
133 $notifications[] = $entry;
137 if (!empty($request['min_id'])) {
138 array_reverse($notifications);
141 System::jsonExit($notifications);