2021-09-14 13:40:50 +01:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2021-09-14 13:40:50 +01:00
|
|
|
namespace Component\Tag\Controller;
|
|
|
|
|
2021-09-20 16:17:13 +01:00
|
|
|
use App\Core\Cache;
|
2021-09-14 13:40:50 +01:00
|
|
|
use App\Core\Controller;
|
2021-09-21 16:39:36 +01:00
|
|
|
use App\Util\Common;
|
2021-09-20 16:17:13 +01:00
|
|
|
use Component\Tag\Tag as CompTag;
|
2021-11-26 11:48:35 +00:00
|
|
|
use Functional as F;
|
2021-09-14 13:40:50 +01:00
|
|
|
|
|
|
|
class Tag extends Controller
|
|
|
|
{
|
2021-11-28 13:09:04 +00:00
|
|
|
private function process(string|array $tag_or_tags, callable $key, string $query, string $template)
|
2021-09-14 13:40:50 +01:00
|
|
|
{
|
2021-11-24 15:51:01 +00:00
|
|
|
$actor = Common::actor();
|
|
|
|
$page = $this->int('page') ?: 1;
|
|
|
|
$lang = $this->string('lang');
|
|
|
|
if (\is_null($lang)) {
|
|
|
|
$langs = $actor->getPreferredLanguageChoices();
|
|
|
|
$lang = $langs[array_key_first($langs)];
|
|
|
|
}
|
2021-11-26 11:48:35 +00:00
|
|
|
if (\is_string($tag_or_tags)) {
|
|
|
|
$canonical = CompTag::canonicalTag($tag_or_tags, $lang);
|
|
|
|
} else {
|
|
|
|
$canonical = F\map($tag_or_tags, fn ($t) => CompTag::canonicalTag($t, $lang));
|
|
|
|
}
|
2021-11-28 13:09:04 +00:00
|
|
|
$results = Cache::pagedStream(
|
2021-11-26 11:48:35 +00:00
|
|
|
key: $key($canonical),
|
|
|
|
query: $query,
|
2021-09-21 16:39:36 +01:00
|
|
|
query_args: ['canon' => $canonical],
|
2021-11-24 15:51:01 +00:00
|
|
|
actor: $actor,
|
2021-10-10 09:26:18 +01:00
|
|
|
page: $page,
|
2021-09-20 16:17:13 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
return [
|
2021-11-28 13:09:04 +00:00
|
|
|
'_template' => $template,
|
|
|
|
'results' => $results,
|
2021-09-20 16:17:13 +01:00
|
|
|
'page' => $page,
|
|
|
|
];
|
2021-09-14 13:40:50 +01:00
|
|
|
}
|
2021-11-26 11:48:35 +00:00
|
|
|
|
2021-11-28 13:09:04 +00:00
|
|
|
public function single_note_tag(string $tag)
|
2021-11-26 11:48:35 +00:00
|
|
|
{
|
|
|
|
return $this->process(
|
|
|
|
tag_or_tags: $tag,
|
2021-11-28 13:09:04 +00:00
|
|
|
key: fn ($canonical) => "note-tag-feed-{$canonical}",
|
2021-11-26 11:48:35 +00:00
|
|
|
query: 'select n from note n join note_tag nt with n.id = nt.note_id where nt.canonical = :canon order by nt.created DESC, nt.note_id DESC',
|
2021-11-28 13:09:04 +00:00
|
|
|
template: 'note_tag_feed.html.twig',
|
2021-11-26 11:48:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-28 13:09:04 +00:00
|
|
|
public function multi_note_tags(string $tags)
|
2021-11-26 11:48:35 +00:00
|
|
|
{
|
|
|
|
$tags = explode(',', $tags);
|
|
|
|
return $this->process(
|
|
|
|
tag_or_tags: $tags,
|
2021-11-28 13:09:04 +00:00
|
|
|
key: fn ($canonical) => 'note-tags-feed-' . implode('-', $canonical),
|
2021-11-26 11:48:35 +00:00
|
|
|
query: 'select n from note n join note_tag nt with n.id = nt.note_id where nt.canonical in (:canon) order by nt.created DESC, nt.note_id DESC',
|
2021-11-28 13:09:04 +00:00
|
|
|
template: 'note_tag_feed.html.twig',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function single_actor_tag(string $tag)
|
|
|
|
{
|
|
|
|
return $this->process(
|
|
|
|
tag_or_tags: $tag,
|
|
|
|
key: fn ($canonical) => "actor-tag-feed-{$canonical}",
|
|
|
|
query: 'select a from actor a join actor_tag at with a.id = at.tagged where at.canonical = :canon order by at.modified DESC',
|
|
|
|
template: 'actor_tag_feed.html.twig',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-30 16:47:31 +00:00
|
|
|
public function multi_actor_tag(string $tags)
|
2021-11-28 13:09:04 +00:00
|
|
|
{
|
|
|
|
$tags = explode(',', $tags);
|
|
|
|
return $this->process(
|
2021-11-30 16:47:31 +00:00
|
|
|
tag_or_tags: $tags,
|
2021-11-28 13:09:04 +00:00
|
|
|
key: fn ($canonical) => 'actor-tags-feed-' . implode('-', $canonical),
|
|
|
|
query: 'select a from actor a join actor_tag at with a.id = at.tagged where at.canonical = :canon order by at.modified DESC',
|
|
|
|
template: 'actor_tag_feed.html.twig',
|
2021-11-26 11:48:35 +00:00
|
|
|
);
|
|
|
|
}
|
2021-09-14 13:40:50 +01:00
|
|
|
}
|