[COMPONENT][Tag] Add stream for multiple tags

This commit is contained in:
Hugo Sales 2021-11-26 11:48:35 +00:00
parent edf1b30e89
commit c178054433
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 31 additions and 6 deletions

View File

@ -8,10 +8,11 @@ use App\Core\Cache;
use App\Core\Controller;
use App\Util\Common;
use Component\Tag\Tag as CompTag;
use Functional as F;
class Tag extends Controller
{
public function tag(string $tag)
private function process(string|array $tag_or_tags, callable $key, string $query)
{
$actor = Common::actor();
$page = $this->int('page') ?: 1;
@ -20,10 +21,14 @@ class Tag extends Controller
$langs = $actor->getPreferredLanguageChoices();
$lang = $langs[array_key_first($langs)];
}
$canonical = CompTag::canonicalTag($tag, $lang);
$notes = Cache::pagedStream(
key: "tag-{$canonical}",
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',
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));
}
$notes = Cache::pagedStream(
key: $key($canonical),
query: $query,
query_args: ['canon' => $canonical],
actor: $actor,
page: $page,
@ -35,4 +40,23 @@ class Tag extends Controller
'page' => $page,
];
}
public function single_tag(string $tag)
{
return $this->process(
tag_or_tags: $tag,
key: fn ($canonical) => "tag-{$canonical}",
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',
);
}
public function multi_tags(string $tags)
{
$tags = explode(',', $tags);
return $this->process(
tag_or_tags: $tags,
key: fn ($canonical) => 'tags-' . implode('-', $canonical),
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',
);
}
}

View File

@ -52,7 +52,8 @@ class Tag extends Component
public function onAddRoute($r): bool
{
$r->connect('tag', '/tag/{tag<' . self::TAG_SLUG_REGEX . '>}', [Controller\Tag::class, 'tag']);
$r->connect('single_tag', '/tag/{tag<' . self::TAG_SLUG_REGEX . '>}', [Controller\Tag::class, 'single_tag']);
$r->connect('multiple_tags', '/tags/{tags<(' . self::TAG_SLUG_REGEX . ',)+' . self::TAG_SLUG_REGEX . '>}', [Controller\Tag::class, 'multi_tags']);
return Event::next;
}