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/accounts/mutes/
34 class Notifications extends BaseApi
37 * @param array $parameters
38 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
40 public static function rawContent(array $parameters = [])
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 // Return results older than this ID
54 $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id'];
56 // Return results newer than this ID
57 $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id'];
59 // Return results immediately newer than this ID
60 $min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id'];
62 // Maximum number of results to return (default 20)
63 $limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit'];
65 // Array of types to exclude (follow, favourite, reblog, mention, poll, follow_request)
66 $exclude_types = !isset($_REQUEST['exclude_types']) ? [] : $_REQUEST['exclude_types'];
68 // Return only notifications received from this account
69 $account_id = (int)!isset($_REQUEST['account_id']) ? 0 : $_REQUEST['account_id'];
71 $params = ['order' => ['id' => true], 'limit' => $limit];
73 $condition = ['uid' => $uid, 'seen' => false, 'type' => []];
75 if (!empty($account_id)) {
76 $contact = Contact::getById($account_id, ['url']);
77 if (!empty($contact['url'])) {
78 $condition['url'] = $contact['url'];
82 if (!in_array('follow_request', $exclude_types)) {
83 $condition['type'] = array_merge($condition['type'], [Notification\Type::INTRO]);
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]);
91 if (!in_array('status', $exclude_types)) {
92 $condition['type'] = array_merge($condition['type'], [Notification\Type::SHARE]);
95 if (!empty($max_id)) {
96 $condition = DBA::mergeConditions($condition, ["`id` < ?", $max_id]);
99 if (!empty($since_id)) {
100 $condition = DBA::mergeConditions($condition, ["`id` > ?", $since_id]);
103 if (!empty($min_id)) {
104 $condition = DBA::mergeConditions($condition, ["`id` > ?", $min_id]);
106 $params['order'] = ['id'];
111 $notify = DBA::select('notify', ['id'], $condition, $params);
112 while ($notification = DBA::fetch($notify)) {
113 $notifications[] = DI::mstdnNotification()->createFromNotifyId($notification['id']);
116 if (!empty($min_id)) {
117 array_reverse($notifications);
120 System::jsonExit($notifications);