mirror of
https://github.com/friendica/friendica
synced 2025-04-19 11:10:10 +00:00
Merge pull request #12807 from HankG/mastodon-trending-api-updates
Mastodon trending api updates
This commit is contained in:
commit
65cd493431
5 changed files with 108 additions and 31 deletions
|
@ -41,6 +41,7 @@ class Links extends BaseApi
|
|||
{
|
||||
$request = $this->getRequest([
|
||||
'limit' => 10, // Maximum number of results to return. Defaults to 10.
|
||||
'offset' => 0, // Offset in set, Defaults to 0.
|
||||
], $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` > ?",
|
||||
|
@ -48,13 +49,17 @@ class Links extends BaseApi
|
|||
$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]]);
|
||||
$statuses = Post::selectPostThread(['uri-id', 'total-comments', 'total-actors'], $condition, ['limit' => [$request['offset'], $request['limit']], 'offset' => $request['offset'], '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);
|
||||
|
||||
if (!empty($trending)) {
|
||||
self::setLinkHeaderByOffsetLimit($request['offset'], $request['limit']);
|
||||
}
|
||||
|
||||
System::jsonExit($trending);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,6 +44,7 @@ class Statuses extends BaseApi
|
|||
|
||||
$request = $this->getRequest([
|
||||
'limit' => 10, // Maximum number of results to return. Defaults to 10.
|
||||
'offset' => 0, // Offset in set, Defaults to 0.
|
||||
], $request);
|
||||
|
||||
$condition = ["NOT `private` AND `commented` > ? AND `created` > ?", DateTimeFormat::utc('now -1 day'), DateTimeFormat::utc('now -1 week')];
|
||||
|
@ -52,7 +53,7 @@ class Statuses extends BaseApi
|
|||
$display_quotes = self::appSupportsQuotes();
|
||||
|
||||
$trending = [];
|
||||
$statuses = Post::selectPostThread(['uri-id'], $condition, ['limit' => $request['limit'], 'order' => ['total-actors' => true]]);
|
||||
$statuses = Post::selectPostThread(['uri-id'], $condition, ['limit' => [$request['offset'], $request['limit']], 'order' => ['total-actors' => true]]);
|
||||
while ($status = Post::fetch($statuses)) {
|
||||
try {
|
||||
$trending[] = DI::mstdnStatus()->createFromUriId($status['uri-id'], $uid, $display_quotes);
|
||||
|
@ -62,6 +63,10 @@ class Statuses extends BaseApi
|
|||
}
|
||||
DBA::close($statuses);
|
||||
|
||||
if (!empty($trending)) {
|
||||
self::setLinkHeaderByOffsetLimit($request['offset'], $request['limit']);
|
||||
}
|
||||
|
||||
System::jsonExit($trending);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,11 +37,18 @@ class Tags extends BaseApi
|
|||
protected function rawContent(array $request = [])
|
||||
{
|
||||
$request = $this->getRequest([
|
||||
'limit' => 20, // Maximum number of results to return. Defaults to 10.
|
||||
'limit' => 20, // Maximum number of results to return. Defaults to 20.
|
||||
'offset' => 0, // Offset in set. Defaults to 0.
|
||||
'friendica_local' => false, // Whether to return local tag trends instead of global, defaults to false
|
||||
], $request);
|
||||
|
||||
$trending = [];
|
||||
$tags = Tag::getGlobalTrendingHashtags(24, 20);
|
||||
if ($request['friendica_local']) {
|
||||
$tags = Tag::getLocalTrendingHashtags(24, $request['limit'], $request['offset']);
|
||||
} else {
|
||||
$tags = Tag::getGlobalTrendingHashtags(24, $request['limit'], $request['offset']);
|
||||
}
|
||||
|
||||
foreach ($tags as $tag) {
|
||||
$tag['name'] = $tag['term'];
|
||||
$history = [['day' => (string)time(), 'uses' => (string)$tag['score'], 'accounts' => (string)$tag['authors']]];
|
||||
|
@ -49,6 +56,10 @@ class Tags extends BaseApi
|
|||
$trending[] = $hashtag->toArray();
|
||||
}
|
||||
|
||||
System::jsonExit(array_slice($trending, 0, $request['limit']));
|
||||
if (!empty($trending)) {
|
||||
self::setLinkHeaderByOffsetLimit($request['offset'], $request['limit']);
|
||||
}
|
||||
|
||||
System::jsonExit($trending);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -168,6 +168,34 @@ class BaseApi extends BaseModule
|
|||
return 'Link: <' . $next . '>; rel="next", <' . $prev . '>; rel="prev"';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the "link" header with "next" and "prev" links for an offset/limit type call
|
||||
* @return string
|
||||
*/
|
||||
protected static function getOffsetAndLimitLinkHeader(int $offset, int $limit): string
|
||||
{
|
||||
$request = self::$request;
|
||||
|
||||
unset($request['offset']);
|
||||
$request['limit'] = $limit;
|
||||
|
||||
$prev_request = $next_request = $request;
|
||||
|
||||
$prev_request['offset'] = $offset - $limit;
|
||||
$next_request['offset'] = $offset + $limit;
|
||||
|
||||
$command = DI::baseUrl() . '/' . DI::args()->getCommand();
|
||||
|
||||
$prev = $command . '?' . http_build_query($prev_request);
|
||||
$next = $command . '?' . http_build_query($next_request);
|
||||
|
||||
if ($prev_request['offset'] >= 0) {
|
||||
return 'Link: <' . $next . '>; rel="next", <' . $prev . '>; rel="prev"';
|
||||
} else {
|
||||
return 'Link: <' . $next . '>; rel="next"';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the "link" header with "next" and "prev" links
|
||||
* @return void
|
||||
|
@ -180,6 +208,18 @@ class BaseApi extends BaseModule
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the "link" header with "next" and "prev" links
|
||||
* @return void
|
||||
*/
|
||||
protected static function setLinkHeaderByOffsetLimit(int $offset, int $limit)
|
||||
{
|
||||
$header = self::getOffsetAndLimitLinkHeader($offset, $limit);
|
||||
if (!empty($header)) {
|
||||
header($header);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the app is known to support quoted posts
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue