]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Api/Mastodon/Markers.php
Merge pull request #13176 from MrPetovan/bug/warnings
[friendica.git] / src / Module / Api / Mastodon / Markers.php
index 2f74c2d1036c257295f55c4599daf30703f6233a..a00b0f83179235b532362c267b6c6a8489c4dd81 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
 
 namespace Friendica\Module\Api\Mastodon;
 
-use Friendica\App\Router;
 use Friendica\Core\System;
+use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Module\BaseApi;
+use Friendica\Util\DateTimeFormat;
 
 /**
  * @see https://docs.joinmastodon.org/methods/timelines/markers/
@@ -34,8 +35,33 @@ class Markers extends BaseApi
        protected function post(array $request = [])
        {
                self::checkAllowedScope(self::SCOPE_WRITE);
+               $uid         = self::getCurrentUserID();
+               $application = self::getCurrentApplication();
 
-               $this->response->unsupported(Router::POST, $request);
+               $timeline     = '';
+               $last_read_id = '';
+               foreach (['home', 'notifications'] as $name) {
+                       if (!empty($request[$name])) {
+                               $timeline     = $name;
+                               $last_read_id = $request[$name]['last_read_id'] ?? '';
+                       }
+               }
+
+               if (empty($timeline) || empty($last_read_id) || empty($application['id'])) {
+                       DI::mstdnError()->UnprocessableEntity();
+               }
+
+               $condition = ['application-id' => $application['id'], 'uid' => $uid, 'timeline' => $timeline];
+               $marker = DBA::selectFirst('application-marker', [], $condition);
+               if (!empty($marker['version'])) {
+                       $version = $marker['version'] + 1;
+               } else {
+                       $version = 1;
+               }
+
+               $fields = ['last_read_id' => $last_read_id, 'version' => $version, 'updated_at' => DateTimeFormat::utcNow()];
+               DBA::update('application-marker', $fields, $condition, true);
+               System::jsonExit($this->fetchTimelines($application['id'], $uid));
        }
 
        /**
@@ -44,7 +70,23 @@ class Markers extends BaseApi
        protected function rawContent(array $request = [])
        {
                self::checkAllowedScope(self::SCOPE_READ);
+               $uid         = self::getCurrentUserID();
+               $application = self::getCurrentApplication();
 
-               System::jsonExit([]);
+               System::jsonExit($this->fetchTimelines($application['id'], $uid));
+       }
+
+       private function fetchTimelines(int $application_id, int $uid)
+       {
+               $values = [];
+               $markers = DBA::select('application-marker', [], ['application-id' => $application_id, 'uid' => $uid]);
+               while ($marker = DBA::fetch($markers)) {
+                       $values[$marker['timeline']] = [
+                               'last_read_id' => $marker['last_read_id'],
+                               'version'      => $marker['version'],
+                               'updated_at'   => $marker['updated_at']
+                       ];
+               }
+               return $values;
        }
 }