]> git.mxchange.org Git - friendica.git/commitdiff
API: Support for markers added
authorMichael <heluecht@pirati.ca>
Mon, 6 Jun 2022 21:38:59 +0000 (21:38 +0000)
committerMichael <heluecht@pirati.ca>
Mon, 6 Jun 2022 21:38:59 +0000 (21:38 +0000)
database.sql
doc/API-Mastodon.md
doc/database.md
doc/database/db_application-marker.md [new file with mode: 0644]
src/Module/Api/Mastodon/Markers.php
static/dbstructure.config.php
static/routes.config.php

index c0c5f78358a7c0b5ff4f1020bbe7a2b84401548b..ce9d74cc680bcb4362bb5922fac07dafbf65929a 100644 (file)
@@ -1,6 +1,6 @@
 -- ------------------------------------------
 -- Friendica 2022.05-rc (Siberian Iris)
--- DB_UPDATE_VERSION 1467
+-- DB_UPDATE_VERSION 1468
 -- ------------------------------------------
 
 
@@ -386,6 +386,22 @@ CREATE TABLE IF NOT EXISTS `application` (
         UNIQUE INDEX `client_id` (`client_id`)
 ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='OAuth application';
 
+--
+-- TABLE application-marker
+--
+CREATE TABLE IF NOT EXISTS `application-marker` (
+       `application-id` int unsigned NOT NULL COMMENT '',
+       `uid` mediumint unsigned NOT NULL COMMENT 'Owner User id',
+       `timeline` varchar(64) NOT NULL COMMENT 'Marker (home, notifications)',
+       `last_read_id` varchar(255) COMMENT 'Marker id for the timeline',
+       `version` smallint unsigned COMMENT 'Version number',
+       `updated_at` datetime COMMENT 'creation time',
+        PRIMARY KEY(`application-id`,`uid`,`timeline`),
+        INDEX `uid_id` (`uid`),
+       FOREIGN KEY (`application-id`) REFERENCES `application` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE,
+       FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE
+) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Timeline marker';
+
 --
 -- TABLE application-token
 --
index a89649921deadc935f8ec9ae72f0480beb03a526..b09ecca8afe8c2e7d67dce549335f5a3774d9ea3 100644 (file)
@@ -84,6 +84,8 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en
 - [`GET /api/v1/lists/:id/accounts`](https://docs.joinmastodon.org/methods/timelines/lists/)
 - [`POST /api/v1/lists/:id/accounts`](https://docs.joinmastodon.org/methods/timelines/lists/)
 - [`DELETE /api/v1/lists/:id/accounts`](https://docs.joinmastodon.org/methods/timelines/lists/)
+- [`POST /api/v1/markers`](https://docs.joinmastodon.org/methods/timelines/markers/)
+- [`GET /api/v1/markers`](https://docs.joinmastodon.org/methods/timelines/markers/)
 - [`POST /api/v1/media`](https://docs.joinmastodon.org/methods/statuses/media/)
 - [`GET /api/v1/media/:id`](https://docs.joinmastodon.org/methods/statuses/media/)
 - [`PUT /api/v1/media/:id`](https://docs.joinmastodon.org/methods/statuses/media/)
@@ -156,7 +158,6 @@ They refer to features that don't exist in Friendica yet.
 - [`GET /api/v1/announcements`](https://docs.joinmastodon.org/methods/announcements/)
 - [`GET /api/v1/endorsements`](https://docs.joinmastodon.org/methods/accounts/endorsements/)
 - [`GET /api/v1/filters`](https://docs.joinmastodon.org/methods/accounts/filters/)
-- [`GET /api/v1/markers`](https://docs.joinmastodon.org/methods/timelines/markers/)
 
 ## Non supportable endpoints
 
index baa92053549f7e5bfeb13f8c2d0675c81f61e2ce..fcff590e1381b0f92f9adcb123c1534ce8bed28f 100644 (file)
@@ -11,6 +11,7 @@ Database Tables
 | [addon](help/database/db_addon) | registered addons |
 | [apcontact](help/database/db_apcontact) | ActivityPub compatible contacts - used in the ActivityPub implementation |
 | [application](help/database/db_application) | OAuth application |
+| [application-marker](help/database/db_application-marker) | Timeline marker |
 | [application-token](help/database/db_application-token) | OAuth user token |
 | [attach](help/database/db_attach) | file attachments |
 | [cache](help/database/db_cache) | Stores temporary data |
diff --git a/doc/database/db_application-marker.md b/doc/database/db_application-marker.md
new file mode 100644 (file)
index 0000000..fa83bf5
--- /dev/null
@@ -0,0 +1,34 @@
+Table application-marker
+===========
+
+Timeline marker
+
+Fields
+------
+
+| Field          | Description                  | Type               | Null | Key | Default | Extra |
+| -------------- | ---------------------------- | ------------------ | ---- | --- | ------- | ----- |
+| application-id |                              | int unsigned       | NO   | PRI | NULL    |       |
+| uid            | Owner User id                | mediumint unsigned | NO   | PRI | NULL    |       |
+| timeline       | Marker (home, notifications) | varchar(64)        | NO   | PRI | NULL    |       |
+| last_read_id   | Marker id for the timeline   | varchar(255)       | YES  |     | NULL    |       |
+| version        | Version number               | smallint unsigned  | YES  |     | NULL    |       |
+| updated_at     | creation time                | datetime           | YES  |     | NULL    |       |
+
+Indexes
+------------
+
+| Name    | Fields                        |
+| ------- | ----------------------------- |
+| PRIMARY | application-id, uid, timeline |
+| uid_id  | uid                           |
+
+Foreign Keys
+------------
+
+| Field | Target Table | Target Field |
+|-------|--------------|--------------|
+| application-id | [application](help/database/db_application) | id |
+| uid | [user](help/database/db_user) | uid |
+
+Return to [database documentation](help/database)
index 43949f74c7f94b9928abd5c5a4b7872c11909a18..4b2a4f46ce191f4ff85783d2d78e1f06d096a6a4 100644 (file)
 
 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->fethTimelines($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->fethTimelines($application['id'], $uid));
+       }
+
+       private function fethTimelines(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;
        }
 }
index 642e1667bdd07cdfcbe331b5098fe2d1e5a0f7df..011a298a57f90814b1a917ec2c91f91ac08f670e 100644 (file)
@@ -55,7 +55,7 @@
 use Friendica\Database\DBA;
 
 if (!defined('DB_UPDATE_VERSION')) {
-       define('DB_UPDATE_VERSION', 1467);
+       define('DB_UPDATE_VERSION', 1468);
 }
 
 return [
@@ -449,6 +449,21 @@ return [
                        "client_id" => ["UNIQUE", "client_id"]
                ]
        ],
+       "application-marker" => [
+               "comment" => "Timeline marker",
+               "fields" => [
+                       "application-id" => ["type" => "int unsigned", "not null" => "1", "primary" => "1", "foreign" => ["application" => "id"], "comment" => ""],
+                       "uid" => ["type" => "mediumint unsigned", "not null" => "1", "primary" => "1", "foreign" => ["user" => "uid"], "comment" => "Owner User id"],
+                       "timeline" => ["type" => "varchar(64)", "not null" => "1", "primary" => "1", "comment" => "Marker (home, notifications)"],
+                       "last_read_id" => ["type" => "varchar(255)", "comment" => "Marker id for the timeline"],
+                       "version" => ["type" => "smallint unsigned", "comment" => "Version number"],
+                       "updated_at" => ["type" => "datetime", "comment" => "creation time"],
+               ],
+               "indexes" => [
+                       "PRIMARY" => ["application-id", "uid", "timeline"],
+                       "uid_id" => ["uid"],
+               ]
+       ],
        "application-token" => [
                "comment" => "OAuth user token",
                "fields" => [
index f2a64154827dadd2d8a334a0cd36de3867346a0b..4c6f5b6bb453ee0b433527f36d972b5524a75155 100644 (file)
@@ -237,7 +237,7 @@ return [
                        '/lists'                             => [Module\Api\Mastodon\Lists::class,                    [R::GET, R::POST]],
                        '/lists/{id:\d+}'                    => [Module\Api\Mastodon\Lists::class,                    [R::GET, R::PUT, R::DELETE]],
                        '/lists/{id:\d+}/accounts'           => [Module\Api\Mastodon\Lists\Accounts::class,           [R::GET, R::POST, R::DELETE]],
-                       '/markers'                           => [Module\Api\Mastodon\Markers::class,                  [R::GET, R::POST]], // Dummy, not supported
+                       '/markers'                           => [Module\Api\Mastodon\Markers::class,                  [R::GET, R::POST]],
                        '/media/{id:\d+}'                    => [Module\Api\Mastodon\Media::class,                    [R::GET, R::PUT ]],
                        '/mutes'                             => [Module\Api\Mastodon\Mutes::class,                    [R::GET         ]],
                        '/notifications'                     => [Module\Api\Mastodon\Notifications::class,            [R::GET         ]],