]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Markers.php
Merge pull request #13176 from MrPetovan/bug/warnings
[friendica.git] / src / Module / Api / Mastodon / Markers.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Api\Mastodon;
23
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Module\BaseApi;
28 use Friendica\Util\DateTimeFormat;
29
30 /**
31  * @see https://docs.joinmastodon.org/methods/timelines/markers/
32  */
33 class Markers extends BaseApi
34 {
35         protected function post(array $request = [])
36         {
37                 self::checkAllowedScope(self::SCOPE_WRITE);
38                 $uid         = self::getCurrentUserID();
39                 $application = self::getCurrentApplication();
40
41                 $timeline     = '';
42                 $last_read_id = '';
43                 foreach (['home', 'notifications'] as $name) {
44                         if (!empty($request[$name])) {
45                                 $timeline     = $name;
46                                 $last_read_id = $request[$name]['last_read_id'] ?? '';
47                         }
48                 }
49
50                 if (empty($timeline) || empty($last_read_id) || empty($application['id'])) {
51                         DI::mstdnError()->UnprocessableEntity();
52                 }
53
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;
58                 } else {
59                         $version = 1;
60                 }
61
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));
65         }
66
67         /**
68          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
69          */
70         protected function rawContent(array $request = [])
71         {
72                 self::checkAllowedScope(self::SCOPE_READ);
73                 $uid         = self::getCurrentUserID();
74                 $application = self::getCurrentApplication();
75
76                 System::jsonExit($this->fetchTimelines($application['id'], $uid));
77         }
78
79         private function fetchTimelines(int $application_id, int $uid)
80         {
81                 $values = [];
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']
88                         ];
89                 }
90                 return $values;
91         }
92 }