1
0
Fork 0

API: Added trending links

This commit is contained in:
Michael 2022-11-28 20:19:57 +00:00
parent e391328cbf
commit de76e860ad
8 changed files with 74 additions and 8 deletions

View file

@ -2438,6 +2438,7 @@ CREATE VIEW `post-thread-view` AS SELECT
0 AS `has-categories`, 0 AS `has-categories`,
EXISTS(SELECT `id` FROM `post-media` WHERE `post-media`.`uri-id` = `post-thread`.`uri-id`) AS `has-media`, 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(*) 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`, `diaspora-interaction`.`interaction` AS `signed_text`,
`parent-item-uri`.`guid` AS `parent-guid`, `parent-item-uri`.`guid` AS `parent-guid`,
`parent-post`.`network` AS `parent-network`, `parent-post`.`network` AS `parent-network`,

View 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/public`](https://docs.joinmastodon.org/methods/timelines/)
- [`GET /api/v1/timelines/tag/:hashtag`](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`](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/statuses`](https://docs.joinmastodon.org/methods/trends/#statuses)
- [`GET /api/v1/trends/tags`](https://docs.joinmastodon.org/methods/trends/#tags) - [`GET /api/v1/trends/tags`](https://docs.joinmastodon.org/methods/trends/#tags)
- [`GET /api/v2/search`](https://docs.joinmastodon.org/methods/search/) - [`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) - [`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/familiar_followers`](https://github.com/mastodon/mastodon/pull/17700)
- [`GET /api/v1/accounts/lookup`](https://github.com/mastodon/mastodon/pull/15740) - [`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/) - [`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/) - [`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/) - [`POST /api/v1/featured_tags`](https://docs.joinmastodon.org/methods/accounts/featured_tags/)

View file

@ -30,13 +30,14 @@ use Friendica\Util\Strings;
class Card extends BaseFactory 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 * @return \Friendica\Object\Api\Mastodon\Card
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws \ImagickException*@throws \Exception * @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]); $item = Post::selectFirst(['body'], ['uri-id' => $uriId]);
if (!empty($item['body'])) { 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);
} }
} }

View file

@ -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);
}
}

View file

@ -39,6 +39,8 @@ class Statuses extends BaseApi
*/ */
protected function rawContent(array $request = []) protected function rawContent(array $request = [])
{ {
$uid = self::getCurrentUserID();
$request = $this->getRequest([ $request = $this->getRequest([
'limit' => 10, // Maximum number of results to return. Defaults to 10. 'limit' => 10, // Maximum number of results to return. Defaults to 10.
], $request); ], $request);
@ -47,9 +49,9 @@ class Statuses extends BaseApi
$condition = DBA::mergeConditions($condition, ['network' => Protocol::FEDERATED]); $condition = DBA::mergeConditions($condition, ['network' => Protocol::FEDERATED]);
$trending = []; $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)) { while ($status = Post::fetch($statuses)) {
$trending[] = DI::mstdnStatus()->createFromUriId($status['uri-id']); $trending[] = DI::mstdnStatus()->createFromUriId($status['uri-id'], $uid);
} }
DBA::close($statuses); DBA::close($statuses);

View file

@ -59,7 +59,7 @@ class Card extends BaseDataTransferObject
* @param array $attachment Attachment record * @param array $attachment Attachment record
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public function __construct(array $attachment) public function __construct(array $attachment, array $history = [])
{ {
$this->url = $attachment['url'] ?? ''; $this->url = $attachment['url'] ?? '';
$this->title = $attachment['title'] ?? ''; $this->title = $attachment['title'] ?? '';
@ -72,6 +72,7 @@ class Card extends BaseDataTransferObject
$this->width = $attachment['width'] ?? 0; $this->width = $attachment['width'] ?? 0;
$this->height = $attachment['height'] ?? 0; $this->height = $attachment['height'] ?? 0;
$this->image = $attachment['image'] ?? ''; $this->image = $attachment['image'] ?? '';
$this->history = $history;
} }
/** /**

View file

@ -665,6 +665,7 @@
"has-categories" => "0", "has-categories" => "0",
"has-media" => "EXISTS(SELECT `id` FROM `post-media` WHERE `post-media`.`uri-id` = `post-thread`.`uri-id`)", "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-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"], "signed_text" => ["diaspora-interaction", "interaction"],
"parent-guid" => ["parent-item-uri", "guid"], "parent-guid" => ["parent-item-uri", "guid"],
"parent-network" => ["parent-post", "network"], "parent-network" => ["parent-post", "network"],

View file

@ -296,7 +296,7 @@ return [
'/timelines/public' => [Module\Api\Mastodon\Timelines\PublicTimeline::class, [R::GET ]], '/timelines/public' => [Module\Api\Mastodon\Timelines\PublicTimeline::class, [R::GET ]],
'/timelines/tag/{hashtag}' => [Module\Api\Mastodon\Timelines\Tag::class, [R::GET ]], '/timelines/tag/{hashtag}' => [Module\Api\Mastodon\Timelines\Tag::class, [R::GET ]],
'/trends' => [Module\Api\Mastodon\Trends\Tags::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/statuses' => [Module\Api\Mastodon\Trends\Statuses::class, [R::GET ]],
'/trends/tags' => [Module\Api\Mastodon\Trends\Tags::class, [R::GET ]], '/trends/tags' => [Module\Api\Mastodon\Trends\Tags::class, [R::GET ]],
], ],