]> git.mxchange.org Git - friendica.git/commitdiff
API: Tredning statuses added
authorMichael <heluecht@pirati.ca>
Mon, 28 Nov 2022 09:30:41 +0000 (09:30 +0000)
committerMichael <heluecht@pirati.ca>
Mon, 28 Nov 2022 09:30:41 +0000 (09:30 +0000)
database.sql
doc/API-Mastodon.md
src/Model/Post.php
src/Module/Api/Mastodon/Trends.php [deleted file]
src/Module/Api/Mastodon/Trends/Statuses.php [new file with mode: 0644]
src/Module/Api/Mastodon/Trends/Tags.php [new file with mode: 0644]
static/dbstructure.config.php
static/dbview.config.php
static/routes.config.php

index 8fa20be52c464b295f517d337ad9d7805a8459cc..cccb233d37e7642a62dcea2e596d6f6fe3be3166 100644 (file)
@@ -1,6 +1,6 @@
 -- ------------------------------------------
 -- Friendica 2022.12-dev (Giant Rhubarb)
--- DB_UPDATE_VERSION 1491
+-- DB_UPDATE_VERSION 1492
 -- ------------------------------------------
 
 
@@ -2437,6 +2437,7 @@ CREATE VIEW `post-thread-view` AS SELECT
        `post-question`.`end-time` AS `question-end-time`,
        0 AS `has-categories`,
        EXISTS(SELECT `id` FROM `post-media` WHERE `post-media`.`uri-id` = `post-thread`.`uri-id`) AS `has-media`,
+       (SELECT COUNT(*) FROM `post` WHERE `parent-uri-id` = `post-thread`.`uri-id` AND `gravity` = 6) AS `total-comments`,
        `diaspora-interaction`.`interaction` AS `signed_text`,
        `parent-item-uri`.`guid` AS `parent-guid`,
        `parent-post`.`network` AS `parent-network`,
index d77218f67008d0bc271ec71a95a57bb2ee992552..2850f0ebf0915e21b8979bc9223b48630fe0a39e 100644 (file)
@@ -126,15 +126,16 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en
 - [`GET /api/v1/statuses/:id/source`](https://docs.joinmastodon.org/methods/statuses/#source)
 - [`GET /api/v1/statuses/:id/card`](https://docs.joinmastodon.org/methods/statuses/#card)
 - [`GET /api/v1/suggestions`](https://docs.joinmastodon.org/methods/accounts/suggestions/)
-- [`GET /api/v1/tags/:id'](https://docs.joinmastodon.org/methods/tags/#get)
-- [`GET /api/v1/tags/:id/follow'](https://docs.joinmastodon.org/methods/tags/#follow)
-- [`GET /api/v1/tags/:id/unfollow'](https://docs.joinmastodon.org/methods/tags/#unfollow)
+- [`GET /api/v1/tags/:id`](https://docs.joinmastodon.org/methods/tags/#get)
+- [`GET /api/v1/tags/:id/follow`](https://docs.joinmastodon.org/methods/tags/#follow)
+- [`GET /api/v1/tags/:id/unfollow`](https://docs.joinmastodon.org/methods/tags/#unfollow)
 - [`GET /api/v1/timelines/direct`](https://docs.joinmastodon.org/methods/timelines/)
 - [`GET /api/v1/timelines/home`](https://docs.joinmastodon.org/methods/timelines/)
 - [`GET /api/v1/timelines/list/:id`](https://docs.joinmastodon.org/methods/timelines/)
 - [`GET /api/v1/timelines/public`](https://docs.joinmastodon.org/methods/timelines/)
 - [`GET /api/v1/timelines/tag/:hashtag`](https://docs.joinmastodon.org/methods/timelines/)
 - [`GET /api/v1/trends`](https://docs.joinmastodon.org/methods/instance/trends/)
+- [`GET /api/v1/trends/statuses`](https://docs.joinmastodon.org/methods/trends/#statuses)
 - [`GET /api/v1/trends/tags`](https://docs.joinmastodon.org/methods/trends/#tags)
 - [`GET /api/v2/search`](https://docs.joinmastodon.org/methods/search/)
 
@@ -147,8 +148,6 @@ These emdpoints are planned to be implemented somewhere in the future.
 - [`GET /api/v1/accounts/familiar_followers`](https://github.com/mastodon/mastodon/pull/17700)
 - [`GET /api/v1/accounts/lookup`](https://github.com/mastodon/mastodon/pull/15740)
 - [`GET /api/v1/trends/links`](https://github.com/mastodon/mastodon/pull/16917)
-- [`GET /api/v1/trends/statuses`](https://github.com/mastodon/mastodon/pull/17431)
-- [`GET /api/v1/trends/tags`](https://github.com/mastodon/mastodon/pull/16917)
 - [`POST /api/v1/polls/:id/votes`](https://docs.joinmastodon.org/methods/statuses/polls/)
 - [`GET /api/v1/featured_tags`](https://docs.joinmastodon.org/methods/accounts/featured_tags/)
 - [`POST /api/v1/featured_tags`](https://docs.joinmastodon.org/methods/accounts/featured_tags/)
index 11e5c2c985a8e0e4301a06cbd09bebe01a7a912b..2baa76b5be676a69ec16434de78b7e3a8905f8a6 100644 (file)
@@ -374,6 +374,21 @@ class Post
                return self::selectView('post-thread-user-view', $selected, $condition, $params);
        }
 
+       /**
+        * Select rows from the post-thread-view view
+        *
+        * @param array $selected  Array of selected fields, empty for all
+        * @param array $condition Array of fields for condition
+        * @param array $params    Array of several parameters
+        *
+        * @return boolean|object
+        * @throws \Exception
+        */
+       public static function selectPostThread(array $selected = [], array $condition = [], array $params = [])
+       {
+               return self::selectView('post-thread-view', $selected, $condition, $params);
+       }
+
        /**
         * Select rows from the given view for a given user
         *
diff --git a/src/Module/Api/Mastodon/Trends.php b/src/Module/Api/Mastodon/Trends.php
deleted file mode 100644 (file)
index c42a276..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-<?php
-/**
- * @copyright Copyright (C) 2010-2022, the Friendica project
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <https://www.gnu.org/licenses/>.
- *
- */
-
-namespace Friendica\Module\Api\Mastodon;
-
-use Friendica\Core\System;
-use Friendica\DI;
-use Friendica\Model\Tag;
-use Friendica\Module\BaseApi;
-
-/**
- * @see https://docs.joinmastodon.org/methods/instance/trends/
- */
-class Trends extends BaseApi
-{
-       /**
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        */
-       protected function rawContent(array $request = [])
-       {
-               $request = $this->getRequest([
-                       'limit' => 20, // Maximum number of results to return. Defaults to 10.
-               ], $request);
-
-               $trending = [];
-               $tags = Tag::getGlobalTrendingHashtags(24, 20);
-               foreach ($tags as $tag) {
-                       $tag['name'] = $tag['term'];
-                       $history = [['day' => (string)time(), 'uses' => (string)$tag['score'], 'accounts' => (string)$tag['authors']]];
-                       $hashtag = new \Friendica\Object\Api\Mastodon\Tag(DI::baseUrl(), $tag, $history);
-                       $trending[] = $hashtag->toArray();
-               }
-
-               System::jsonExit(array_slice($trending, 0, $request['limit']));
-       }
-}
diff --git a/src/Module/Api/Mastodon/Trends/Statuses.php b/src/Module/Api/Mastodon/Trends/Statuses.php
new file mode 100644 (file)
index 0000000..70b23f5
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2022, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Module\Api\Mastodon\Trends;
+
+use Friendica\Core\Protocol;
+use Friendica\Core\System;
+use Friendica\Database\DBA;
+use Friendica\DI;
+use Friendica\Model\Post;
+use Friendica\Module\BaseApi;
+use Friendica\Util\DateTimeFormat;
+
+/**
+ * @see https://docs.joinmastodon.org/methods/trends/#statuses
+ */
+class Statuses extends BaseApi
+{
+       /**
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       protected function rawContent(array $request = [])
+       {
+               $request = $this->getRequest([
+                       'limit' => 10, // Maximum number of results to return. Defaults to 10.
+               ], $request);
+
+               $trending = [];
+               $condition = ["NOT `private` AND `commented` > ?", DateTimeFormat::utc('now -1 day')];
+               $condition = DBA::mergeConditions($condition, ['network' => Protocol::FEDERATED]);
+               $statuses = Post::selectPostThread(['uri-id'], $condition, ['limit' => $request['limit'], 'order' => ['total-comments' => true]]);
+               while ($status = Post::fetch($statuses)) {
+                       $trending[] = DI::mstdnStatus()->createFromUriId($status['uri-id']);
+               }
+               DBA::close($statuses);
+
+               System::jsonExit($trending);
+       }
+}
diff --git a/src/Module/Api/Mastodon/Trends/Tags.php b/src/Module/Api/Mastodon/Trends/Tags.php
new file mode 100644 (file)
index 0000000..2014b7a
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2022, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Module\Api\Mastodon\Trends;
+
+use Friendica\Core\System;
+use Friendica\DI;
+use Friendica\Model\Tag;
+use Friendica\Module\BaseApi;
+
+/**
+ * @see https://docs.joinmastodon.org/methods/instance/trends/
+ */
+class Tags extends BaseApi
+{
+       /**
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       protected function rawContent(array $request = [])
+       {
+               $request = $this->getRequest([
+                       'limit' => 20, // Maximum number of results to return. Defaults to 10.
+               ], $request);
+
+               $trending = [];
+               $tags = Tag::getGlobalTrendingHashtags(24, 20);
+               foreach ($tags as $tag) {
+                       $tag['name'] = $tag['term'];
+                       $history = [['day' => (string)time(), 'uses' => (string)$tag['score'], 'accounts' => (string)$tag['authors']]];
+                       $hashtag = new \Friendica\Object\Api\Mastodon\Tag(DI::baseUrl(), $tag, $history);
+                       $trending[] = $hashtag->toArray();
+               }
+
+               System::jsonExit(array_slice($trending, 0, $request['limit']));
+       }
+}
index 942f2237d538e279b1bbb8050e205d73450a6781..2f613f9749d336bd70b312775d284983188de7e4 100644 (file)
@@ -55,7 +55,7 @@
 use Friendica\Database\DBA;
 
 if (!defined('DB_UPDATE_VERSION')) {
-       define('DB_UPDATE_VERSION', 1491);
+       define('DB_UPDATE_VERSION', 1492);
 }
 
 return [
index 2452dc341b90dba023859a6a48210c909db4d77d..6c4f00a9869da58dcd88457143fd38eb71801709 100644 (file)
                        "question-end-time" => ["post-question", "end-time"],
                        "has-categories" => "0",
                        "has-media" => "EXISTS(SELECT `id` FROM `post-media` WHERE `post-media`.`uri-id` = `post-thread`.`uri-id`)",
+                       "total-comments" => "(SELECT COUNT(*) FROM `post` WHERE `parent-uri-id` = `post-thread`.`uri-id` AND `gravity` = 6)",
                        "signed_text" => ["diaspora-interaction", "interaction"],
                        "parent-guid" => ["parent-item-uri", "guid"],
                        "parent-network" => ["parent-post", "network"],
index 9b2d3d93654512cf06d41fe52f0b0333004bbf21..81a9d2e2fd57406e59dd231318f91a703b72a4d4 100644 (file)
@@ -295,10 +295,10 @@ return [
                        '/timelines/list/{id:\d+}'           => [Module\Api\Mastodon\Timelines\ListTimeline::class,   [R::GET         ]],
                        '/timelines/public'                  => [Module\Api\Mastodon\Timelines\PublicTimeline::class, [R::GET         ]],
                        '/timelines/tag/{hashtag}'           => [Module\Api\Mastodon\Timelines\Tag::class,            [R::GET         ]],
-                       '/trends'                            => [Module\Api\Mastodon\Trends::class,                   [R::GET         ]],
+                       '/trends'                            => [Module\Api\Mastodon\Trends\Tags::class,              [R::GET         ]],
                        '/trends/links'                      => [Module\Api\Mastodon\Unimplemented::class,            [R::GET         ]], // not implemented
-                       '/trends/statuses'                   => [Module\Api\Mastodon\Unimplemented::class,            [R::GET         ]], // not implemented
-                       '/trends/tags'                       => [Module\Api\Mastodon\Trends::class,                   [R::GET         ]],
+                       '/trends/statuses'                   => [Module\Api\Mastodon\Trends\Statuses::class,          [R::GET         ]],
+                       '/trends/tags'                       => [Module\Api\Mastodon\Trends\Tags::class,              [R::GET         ]],
                ],
                '/v{version:\d+}' => [
                        '/admin/accounts'                    => [Module\Api\Mastodon\Unimplemented::class,            [R::GET         ]], // not supported