]> git.mxchange.org Git - friendica.git/commitdiff
API: Added trending links
authorMichael <heluecht@pirati.ca>
Mon, 28 Nov 2022 20:19:57 +0000 (20:19 +0000)
committerMichael <heluecht@pirati.ca>
Mon, 28 Nov 2022 20:19:57 +0000 (20:19 +0000)
database.sql
doc/API-Mastodon.md
src/Factory/Api/Mastodon/Card.php
src/Module/Api/Mastodon/Trends/Links.php [new file with mode: 0644]
src/Module/Api/Mastodon/Trends/Statuses.php
src/Object/Api/Mastodon/Card.php
static/dbview.config.php
static/routes.config.php

index cccb233d37e7642a62dcea2e596d6f6fe3be3166..c68ae094c1aa5e11f531e980a9ae27fcebb663fa 100644 (file)
@@ -2438,6 +2438,7 @@ CREATE VIEW `post-thread-view` AS SELECT
        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`,
+       (SELECT COUNT(DISTINCT(`author-id`)) FROM `post` WHERE `parent-uri-id` = `post-thread`.`uri-id` AND `gravity` = 6) AS `total-actors`,
        `diaspora-interaction`.`interaction` AS `signed_text`,
        `parent-item-uri`.`guid` AS `parent-guid`,
        `parent-post`.`network` AS `parent-network`,
index 2850f0ebf0915e21b8979bc9223b48630fe0a39e..0e2f57430395a91ddab6e6c22f092695fbceaac1 100644 (file)
@@ -135,6 +135,7 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en
 - [`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/links`](https://github.com/mastodon/mastodon/pull/16917)
 - [`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,7 +148,6 @@ These emdpoints are planned to be implemented somewhere in the future.
 - [`POST /api/v1/accounts/:id/remove_from_followers`](https://github.com/mastodon/mastodon/pull/16864)
 - [`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)
 - [`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 659ab3a3de1e530952973f4c2b993f5b08d298cf..ac508418479ff685750b864890b13a582c54ffab 100644 (file)
@@ -30,13 +30,14 @@ use Friendica\Util\Strings;
 class Card extends BaseFactory
 {
        /**
-        * @param int $uriId Uri-ID of the item
+        * @param int   $uriId   Uri-ID of the item
+        * @param array $history Link request history
         *
         * @return \Friendica\Object\Api\Mastodon\Card
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException*@throws \Exception
         */
-       public function createFromUriId(int $uriId): \Friendica\Object\Api\Mastodon\Card
+       public function createFromUriId(int $uriId, array $history = []): \Friendica\Object\Api\Mastodon\Card
        {
                $item = Post::selectFirst(['body'], ['uri-id' => $uriId]);
                if (!empty($item['body'])) {
@@ -76,6 +77,6 @@ class Card extends BaseFactory
                        }
                }
 
-               return new \Friendica\Object\Api\Mastodon\Card($data);
+               return new \Friendica\Object\Api\Mastodon\Card($data, $history);
        }
 }
diff --git a/src/Module/Api/Mastodon/Trends/Links.php b/src/Module/Api/Mastodon/Trends/Links.php
new file mode 100644 (file)
index 0000000..ee535c7
--- /dev/null
@@ -0,0 +1,60 @@
+<?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/#links
+ */
+class Links 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);
+
+               $condition = ["EXISTS(SELECT `id` FROM `post-media` WHERE `post-media`.`uri-id` = `post-thread-view`.`uri-id` AND `type` = ? AND NOT `name` IS NULL AND NOT `description` IS NULL) AND NOT `private` AND `commented` > ? AND `created` > ?",
+                       Post\Media::HTML, DateTimeFormat::utc('now -1 day'), DateTimeFormat::utc('now -1 week')];
+               $condition = DBA::mergeConditions($condition, ['network' => Protocol::FEDERATED]);
+
+               $trending = [];
+               $statuses = Post::selectPostThread(['uri-id', 'total-comments', 'total-actors'], $condition, ['limit' => $request['limit'], 'order' => ['total-actors' => true]]);
+               while ($status = Post::fetch($statuses)) {
+                       $history = [['day' => (string)time(), 'uses' => (string)$status['total-comments'], 'accounts' => (string)$status['total-actors']]];
+                       $trending[] = DI::mstdnCard()->createFromUriId($status['uri-id'], $history)->toArray();
+               }
+               DBA::close($statuses);
+
+               System::jsonExit($trending);
+       }
+}
index 8974403d770789eb16add8ed2c8a756cebb56da2..26eb52d7ae3d829de433711bbd195897c38e1721 100644 (file)
@@ -39,6 +39,8 @@ class Statuses extends BaseApi
         */
        protected function rawContent(array $request = [])
        {
+               $uid = self::getCurrentUserID();
+
                $request = $this->getRequest([
                        'limit' => 10, // Maximum number of results to return. Defaults to 10.
                ], $request);
@@ -47,9 +49,9 @@ class Statuses extends BaseApi
                $condition = DBA::mergeConditions($condition, ['network' => Protocol::FEDERATED]);
 
                $trending = [];
-               $statuses = Post::selectPostThread(['uri-id'], $condition, ['limit' => $request['limit'], 'order' => ['total-comments' => true]]);
+               $statuses = Post::selectPostThread(['uri-id'], $condition, ['limit' => $request['limit'], 'order' => ['total-actors' => true]]);
                while ($status = Post::fetch($statuses)) {
-                       $trending[] = DI::mstdnStatus()->createFromUriId($status['uri-id']);
+                       $trending[] = DI::mstdnStatus()->createFromUriId($status['uri-id'], $uid);
                }
                DBA::close($statuses);
 
index 4bd6c6e7697d152b35062124c667260a083f7c83..bf876171906c678fb5478b4676e6502784206927 100644 (file)
@@ -59,7 +59,7 @@ class Card extends BaseDataTransferObject
         * @param array   $attachment Attachment record
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public function __construct(array $attachment)
+       public function __construct(array $attachment, array $history = [])
        {
                $this->url           = $attachment['url'] ?? '';
                $this->title         = $attachment['title'] ?? '';
@@ -72,6 +72,7 @@ class Card extends BaseDataTransferObject
                $this->width         = $attachment['width'] ?? 0;
                $this->height        = $attachment['height'] ?? 0;
                $this->image         = $attachment['image'] ?? '';
+               $this->history       = $history;
        }
 
        /**
index 6c4f00a9869da58dcd88457143fd38eb71801709..d06904a91f0f91fe96aff09f3061370c7d6fb6aa 100644 (file)
                        "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)",
+                       "total-actors" => "(SELECT COUNT(DISTINCT(`author-id`)) 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 81a9d2e2fd57406e59dd231318f91a703b72a4d4..da5dda0278dde875d4fd2db295f424446c8c7d00 100644 (file)
@@ -296,7 +296,7 @@ return [
                        '/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\Tags::class,              [R::GET         ]],
-                       '/trends/links'                      => [Module\Api\Mastodon\Unimplemented::class,            [R::GET         ]], // not implemented
+                       '/trends/links'                      => [Module\Api\Mastodon\Trends\Links::class,             [R::GET         ]],
                        '/trends/statuses'                   => [Module\Api\Mastodon\Trends\Statuses::class,          [R::GET         ]],
                        '/trends/tags'                       => [Module\Api\Mastodon\Trends\Tags::class,              [R::GET         ]],
                ],