From ca9945a4be763757f53907320c54b46cc64193b1 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Fri, 1 Apr 2022 00:11:01 +0100 Subject: [PATCH] [ENTITY][Actor][COMPONENT][Tag] Add `Actor->getNoteTags(?string $note_type)` which gets a cached list of NoteTags for notes of type $note_type for the actor --- components/Tag/Tag.php | 31 ++++++++++++++++++++++++++++++- src/Entity/Actor.php | 12 +++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/components/Tag/Tag.php b/components/Tag/Tag.php index 81c60d0ea0..f6fe16788d 100644 --- a/components/Tag/Tag.php +++ b/components/Tag/Tag.php @@ -72,7 +72,7 @@ class Tag extends Component if (!self::validate($tag)) { return null; // Ignore invalid tag candidates } - $canonical_tag = self::canonicalTag($tag, \is_null($lang_id) ? null : Language::getById($lang_id)->getLocale()); + $canonical_tag = self::canonicalTag($tag, \is_null($lang_id) ? null : Language::getById($lang_id)->getLocale()); DB::persist($note_tag = NoteTag::create([ 'tag' => $tag, 'canonical' => $canonical_tag, @@ -86,6 +86,35 @@ class Tag extends Component return $note_tag; } + /** + * @return NoteTag[] + */ + public static function getNoteTags(int $actor_id, ?string $note_type): array + { + $query = <<<'EOF' + select nt from \App\Entity\Note n + join \Component\Tag\Entity\NoteTag nt with n.id = nt.note_id + where n.actor_id = :id + EOF; + if (\is_null($note_type)) { + return Cache::getList( + Actor::cacheKeys($actor_id, 'any')['note-tags'], + fn () => DB::dql( + $query, + ['id' => $actor_id], + ), + ); + } else { + return Cache::getList( + Actor::cacheKeys($actor_id, $note_type)['note-tags'], + fn () => DB::dql( + $query . ' and n.type = :type', + ['id' => $actor_id, 'type' => $note_type], + ), + ); + } + } + /** * Process note by extracting any tags present */ diff --git a/src/Entity/Actor.php b/src/Entity/Actor.php index d9580232f9..2d5707b388 100644 --- a/src/Entity/Actor.php +++ b/src/Entity/Actor.php @@ -42,6 +42,7 @@ use Component\Group\Entity\LocalGroup; use Component\Language\Entity\ActorLanguage; use Component\Language\Entity\Language; use Component\Subscription\Entity\ActorSubscription; +use Component\Tag\Tag; use Functional as F; /** @@ -263,7 +264,8 @@ class Actor extends Entity 'id' => "actor-id-{$actor_id}", 'nickname' => "actor-nickname-id-{$actor_id}", 'fullname' => "actor-fullname-id-{$actor_id}", - 'self-tags' => "actor-self-tags-{$actor_id}", + 'self-tags' => "actor-self-tags-{$actor_id}", + 'note-tags' => "actor-note-tags-{$actor_id}-{$other}", // $other is note type 'circles' => "actor-circles-{$actor_id}", 'subscribers' => "subscribers-{$actor_id}", 'subscribed' => "subscribed-{$actor_id}", @@ -347,6 +349,14 @@ class Actor extends Entity ); } + /** + * @return NoteTag[] + */ + public function getNoteTags(?string $note_type = null): array + { + return Tag::getNoteTags($this->getId(), $note_type); + } + private function getSubCount(string $which, string $column): int { return Cache::get(