From c1780544335345e98e537600dbfabdd094e4d9f5 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Fri, 26 Nov 2021 11:48:35 +0000 Subject: [PATCH] [COMPONENT][Tag] Add stream for multiple tags --- components/Tag/Controller/Tag.php | 34 ++++++++++++++++++++++++++----- components/Tag/Tag.php | 3 ++- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/components/Tag/Controller/Tag.php b/components/Tag/Controller/Tag.php index c4a6849111..798adec0d4 100644 --- a/components/Tag/Controller/Tag.php +++ b/components/Tag/Controller/Tag.php @@ -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', + ); + } } diff --git a/components/Tag/Tag.php b/components/Tag/Tag.php index 83b475efad..871a94e770 100644 --- a/components/Tag/Tag.php +++ b/components/Tag/Tag.php @@ -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; }