Add support to Mastodon Tag Trends for paging, local tags, and faster refresh

= with phpcbf format auto-correction
This commit is contained in:
Hank Grabowski 2023-02-16 22:55:23 -05:00
parent 90717b602e
commit c5effdadec
2 changed files with 51 additions and 28 deletions

View File

@ -534,8 +534,11 @@ class Tag
$searchpath = DI::baseUrl() . '/search?tag='; $searchpath = DI::baseUrl() . '/search?tag=';
$taglist = DBA::select('tag-view', ['type', 'name', 'url', 'cid'], $taglist = DBA::select(
['uri-id' => $item['uri-id'], 'type' => [self::HASHTAG, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]]); 'tag-view',
['type', 'name', 'url', 'cid'],
['uri-id' => $item['uri-id'], 'type' => [self::HASHTAG, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]]
);
while ($tag = DBA::fetch($taglist)) { while ($tag = DBA::fetch($taglist)) {
if ($tag['url'] == '') { if ($tag['url'] == '') {
$tag['url'] = $searchpath . rawurlencode($tag['name']); $tag['url'] = $searchpath . rawurlencode($tag['name']);
@ -544,7 +547,7 @@ class Tag
$orig_tag = $tag['url']; $orig_tag = $tag['url'];
$prefix = self::TAG_CHARACTER[$tag['type']]; $prefix = self::TAG_CHARACTER[$tag['type']];
switch($tag['type']) { switch ($tag['type']) {
case self::HASHTAG: case self::HASHTAG:
if ($orig_tag != $tag['url']) { if ($orig_tag != $tag['url']) {
$item['body'] = str_replace($orig_tag, $tag['url'], $item['body']); $item['body'] = str_replace($orig_tag, $tag['url'], $item['body']);
@ -639,17 +642,17 @@ class Tag
* *
* @param int $period Period in hours to consider posts * @param int $period Period in hours to consider posts
* @param int $limit Number of returned tags * @param int $limit Number of returned tags
* @param int $offset Page offset in results
* @return array * @return array
* @throws \Exception * @throws \Exception
*/ */
public static function getGlobalTrendingHashtags(int $period, $limit = 10): array public static function getGlobalTrendingHashtags(int $period, int $limit = 10, int $offset = 0): array
{ {
$tags = DI::cache()->get('global_trending_tags-' . $period . '-' . $limit); $tags = DI::cache()->get("global_trending_tags-$period");
if (!empty($tags)) { if (empty($tags)) {
return $tags; $tags = self::setGlobalTrendingHashtags($period, 1000);
} else {
return self::setGlobalTrendingHashtags($period, $limit);
} }
return array_slice($tags, $limit * $offset, $limit);
} }
/** /**
@ -665,7 +668,9 @@ class Tag
} }
$blocked = explode(',', $blocked_txt); $blocked = explode(',', $blocked_txt);
array_walk($blocked, function(&$value) { $value = "'" . DBA::escape(trim($value)) . "'";}); array_walk($blocked, function (&$value) {
$value = "'" . DBA::escape(trim($value)) . "'";
});
return ' AND NOT `name` IN (' . implode(',', $blocked) . ')'; return ' AND NOT `name` IN (' . implode(',', $blocked) . ')';
} }
@ -683,8 +688,11 @@ class Tag
* Get a uri-id that is at least X hours old. * Get a uri-id that is at least X hours old.
* We use the uri-id in the query for the hash tags since this is much faster * We use the uri-id in the query for the hash tags since this is much faster
*/ */
$post = Post::selectFirstThread(['uri-id'], ["`uid` = ? AND `received` < ?", 0, DateTimeFormat::utc('now - ' . $period . ' hour')], $post = Post::selectFirstThread(
['order' => ['received' => true]]); ['uri-id'],
["`uid` = ? AND `received` < ?", 0, DateTimeFormat::utc('now - ' . $period . ' hour')],
['order' => ['received' => true]]
);
if (empty($post['uri-id'])) { if (empty($post['uri-id'])) {
return []; return [];
@ -692,17 +700,20 @@ class Tag
$block_sql = self::getBlockedSQL(); $block_sql = self::getBlockedSQL();
$tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`, COUNT(DISTINCT(`author-id`)) as `authors` $tagsStmt = DBA::p(
"SELECT `name` AS `term`, COUNT(*) AS `score`, COUNT(DISTINCT(`author-id`)) as `authors`
FROM `tag-search-view` FROM `tag-search-view`
WHERE `private` = ? AND `uid` = ? AND `uri-id` > ? $block_sql WHERE `private` = ? AND `uid` = ? AND `uri-id` > ? $block_sql
GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?", GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?",
Item::PUBLIC, 0, $post['uri-id'], Item::PUBLIC,
0,
$post['uri-id'],
$limit $limit
); );
if (DBA::isResult($tagsStmt)) { if (DBA::isResult($tagsStmt)) {
$tags = DBA::toArray($tagsStmt); $tags = DBA::toArray($tagsStmt);
DI::cache()->set('global_trending_tags-' . $period . '-' . $limit, $tags, Duration::DAY); DI::cache()->set("global_trending_tags-$period", $tags, Duration::HOUR);
return $tags; return $tags;
} }
@ -714,17 +725,17 @@ class Tag
* *
* @param int $period Period in hours to consider posts * @param int $period Period in hours to consider posts
* @param int $limit Number of returned tags * @param int $limit Number of returned tags
* @param int $offset Page offset in results
* @return array * @return array
* @throws \Exception * @throws \Exception
*/ */
public static function getLocalTrendingHashtags(int $period, $limit = 10): array public static function getLocalTrendingHashtags(int $period, $limit = 10, int $offset = 0): array
{ {
$tags = DI::cache()->get('local_trending_tags-' . $period . '-' . $limit); $tags = DI::cache()->get("local_trending_tags-$period");
if (!empty($tags)) { if (empty($tags)) {
return $tags; $tags = self::setLocalTrendingHashtags($period, 1000);
} else {
return self::setLocalTrendingHashtags($period, $limit);
} }
return array_slice($tags, $limit * $offset, $limit);
} }
/** /**
@ -739,25 +750,30 @@ class Tag
{ {
// Get a uri-id that is at least X hours old. // Get a uri-id that is at least X hours old.
// We use the uri-id in the query for the hash tags since this is much faster // We use the uri-id in the query for the hash tags since this is much faster
$post = Post::selectFirstThread(['uri-id'], ["`uid` = ? AND `received` < ?", 0, DateTimeFormat::utc('now - ' . $period . ' hour')], $post = Post::selectFirstThread(
['order' => ['received' => true]]); ['uri-id'],
["`uid` = ? AND `received` < ?", 0, DateTimeFormat::utc('now - ' . $period . ' hour')],
['order' => ['received' => true]]
);
if (empty($post['uri-id'])) { if (empty($post['uri-id'])) {
return []; return [];
} }
$block_sql = self::getBlockedSQL(); $block_sql = self::getBlockedSQL();
$tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`, COUNT(DISTINCT(`author-id`)) as `authors` $tagsStmt = DBA::p(
"SELECT `name` AS `term`, COUNT(*) AS `score`, COUNT(DISTINCT(`author-id`)) as `authors`
FROM `tag-search-view` FROM `tag-search-view`
WHERE `private` = ? AND `wall` AND `origin` AND `uri-id` > ? $block_sql WHERE `private` = ? AND `wall` AND `origin` AND `uri-id` > ? $block_sql
GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?", GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?",
Item::PUBLIC, $post['uri-id'], Item::PUBLIC,
$post['uri-id'],
$limit $limit
); );
if (DBA::isResult($tagsStmt)) { if (DBA::isResult($tagsStmt)) {
$tags = DBA::toArray($tagsStmt); $tags = DBA::toArray($tagsStmt);
DI::cache()->set('local_trending_tags-' . $period . '-' . $limit, $tags, Duration::DAY); DI::cache()->set("local_trending_tags-$period", $tags, Duration::HOUR);
return $tags; return $tags;
} }

View File

@ -37,11 +37,18 @@ class Tags extends BaseApi
protected function rawContent(array $request = []) protected function rawContent(array $request = [])
{ {
$request = $this->getRequest([ $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,
'friendica_local' => false,
], $request); ], $request);
$trending = []; $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) { foreach ($tags as $tag) {
$tag['name'] = $tag['term']; $tag['name'] = $tag['term'];
$history = [['day' => (string)time(), 'uses' => (string)$tag['score'], 'accounts' => (string)$tag['authors']]]; $history = [['day' => (string)time(), 'uses' => (string)$tag['score'], 'accounts' => (string)$tag['authors']]];