From 7d38c927e1ece8f5996ba3650e2c8e2fc4f06106 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Mon, 3 Jan 2022 20:31:39 +0000 Subject: [PATCH] [ENTITY][Note][CACHE] Consolidate cache keys to helper function and add ensureCanInteract --- src/Entity/Note.php | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/src/Entity/Note.php b/src/Entity/Note.php index 40d2b25265..9527f60c90 100644 --- a/src/Entity/Note.php +++ b/src/Entity/Note.php @@ -30,10 +30,13 @@ use App\Core\Event; use App\Core\Log; use App\Core\Router\Router; use App\Core\VisibilityScope; +use App\Util\Exception\ClientException; +use App\Util\Exception\NoSuchNoteException; use App\Util\Formatting; use Component\Avatar\Avatar; use Component\Conversation\Entity\Conversation; use Component\Language\Entity\Language; +use function App\Core\I18n\_m; /** * Entity for notices @@ -221,6 +224,18 @@ class Note extends Entity // @codeCoverageIgnoreEnd // }}} Autocode + public static function cacheKeys(int $note_id) + { + return [ + 'note' => "note-{$note_id}", + 'attachments' => "note-attachments-{$note_id}", + 'attachments-title' => "note-attachments-with-title-{$note_id}", + 'links' => "note-links-{$note_id}", + 'tags' => "note-tags-{$note_id}", + 'replies' => "note-replies-{$note_id}", + ]; + } + public function getConversation(): Conversation { return Conversation::getByPK(['id' => $this->getConversationId()]); @@ -258,17 +273,17 @@ class Note extends Entity public static function getById(int $note_id): self { - return Cache::get("note-{$note_id}", fn () => DB::findOneBy('note', ['id' => $note_id])); + return Cache::get(self::cacheKeys($note_id)['note'], fn () => DB::findOneBy('note', ['id' => $note_id])); } public function getNoteLanguageShortDisplay(): ?string { - return !\is_null($this->language_id) ? Language::getById($this->language_id)->getShortDisplay() : null; + return !\is_null($this->getLanguageId()) ? Language::getById($this->getLanguageId())->getShortDisplay() : null; } public function getLanguageLocale(): ?string { - return !\is_null($this->language_id) ? Language::getById($this->language_id)->getLocale() : null; + return !\is_null($this->getLanguageId()) ? Language::getById($this->getLanguageId())->getLocale() : null; } public static function getAllNotesByActor(Actor $actor): array @@ -279,7 +294,7 @@ class Note extends Entity public function getAttachments(): array { - return Cache::getList('note-attachments-' . $this->id, function () { + return Cache::getList(self::cacheKeys($this->getId())['attachments'], function () { return DB::dql( <<<'EOF' select att from attachment att @@ -293,7 +308,7 @@ class Note extends Entity public function getAttachmentsWithTitle(): array { - return Cache::getList('note-attachments-with-title-' . $this->id, function () { + return Cache::getList(self::cacheKeys($this->getId())['attachments-title'], function () { $from_db = DB::dql( <<<'EOF' select att, atn.title @@ -313,7 +328,7 @@ class Note extends Entity public function getLinks(): array { - return Cache::getList('note-links-' . $this->id, function () { + return Cache::getList(self::cacheKeys($this->getId())['links'], function () { return DB::dql( <<<'EOF' select l from link l @@ -327,7 +342,7 @@ class Note extends Entity public function getTags(): array { - return Cache::getList('note-tags-' . $this->getId(), fn () => DB::findBy('note_tag', ['note_id' => $this->getId()])); + return Cache::getList(self::cacheKeys($this->getId())['tags'], fn () => DB::findBy('note_tag', ['note_id' => $this->getId()])); } /** @@ -352,7 +367,7 @@ class Note extends Entity */ public function getReplies(): array { - return DB::findBy('note', ['reply_to' => $this->getId()], order_by: ['created' => 'DESC', 'id' => 'DESC']); + return Cache::getList(self::cacheKeys($this->getId())['replies'], fn () => DB::findBy('note', ['reply_to' => $this->getId()], order_by: ['created' => 'DESC', 'id' => 'DESC'])); } /** @@ -492,6 +507,17 @@ class Note extends Entity return $activity; } + public static function ensureCanInteract(?Note $note, LocalUser|Actor $actor): Note + { + if (\is_null($note)) { + throw new NoSuchNoteException(); + } elseif (!$note->isVisibleTo($actor)) { + throw new ClientException(_m('You don\'t have permissions to view this note.'), 401); + } else { + return $note; + } + } + public static function schemaDef(): array { return [