3 * @copyright Copyright (C) 2010-2022, 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\Module\BaseApi;
28 use Friendica\Util\DateTimeFormat;
31 * @see https://docs.joinmastodon.org/methods/timelines/markers/
33 class Markers extends BaseApi
35 protected function post(array $request = [])
37 self::checkAllowedScope(self::SCOPE_WRITE);
38 $uid = self::getCurrentUserID();
39 $application = self::getCurrentApplication();
43 foreach (['home', 'notifications'] as $name) {
44 if (!empty($request[$name])) {
46 $last_read_id = $request[$name]['last_read_id'] ?? '';
50 if (empty($timeline) || empty($last_read_id) || empty($application['id'])) {
51 DI::mstdnError()->UnprocessableEntity();
54 $condition = ['application-id' => $application['id'], 'uid' => $uid, 'timeline' => $timeline];
55 $marker = DBA::selectFirst('application-marker', [], $condition);
56 if (!empty($marker['version'])) {
57 $version = $marker['version'] + 1;
62 $fields = ['last_read_id' => $last_read_id, 'version' => $version, 'updated_at' => DateTimeFormat::utcNow()];
63 DBA::update('application-marker', $fields, $condition, true);
64 System::jsonExit($this->fetchTimelines($application['id'], $uid));
68 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
70 protected function rawContent(array $request = [])
72 self::checkAllowedScope(self::SCOPE_READ);
73 $uid = self::getCurrentUserID();
74 $application = self::getCurrentApplication();
76 System::jsonExit($this->fetchTimelines($application['id'], $uid));
79 private function fetchTimelines(int $application_id, int $uid)
82 $markers = DBA::select('application-marker', [], ['application-id' => $application_id, 'uid' => $uid]);
83 while ($marker = DBA::fetch($markers)) {
84 $values[$marker['timeline']] = [
85 'last_read_id' => $marker['last_read_id'],
86 'version' => $marker['version'],
87 'updated_at' => $marker['updated_at']