From a47a01abee14750c49d56a9d82788fdfe6e1f3c5 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sat, 4 Dec 2021 14:06:22 +0000 Subject: [PATCH] [ENTITY][ActorTagBlock][NoteTagBlock] Add ActorTagBlock and NoteTagBlock --- src/Entity/ActorTagBlock.php | 107 ++++++++++++++++++++++++++ src/Entity/NoteTagBlock.php | 144 +++++++++++++++++++++++++++++++++++ 2 files changed, 251 insertions(+) create mode 100644 src/Entity/ActorTagBlock.php create mode 100644 src/Entity/NoteTagBlock.php diff --git a/src/Entity/ActorTagBlock.php b/src/Entity/ActorTagBlock.php new file mode 100644 index 0000000000..efc41248c6 --- /dev/null +++ b/src/Entity/ActorTagBlock.php @@ -0,0 +1,107 @@ +. +// }}} + +namespace App\Entity; + +use App\Core\Entity; +use Component\Tag\Tag; +use DateTimeInterface; + +/** + * Entity for User's Note Tag block + * + * @category DB + * @package GNUsocial + * + * @author Hugo Sales + * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later + */ +class ActorTagBlock extends Entity +{ + // {{{ Autocode + // @codeCoverageIgnoreStart + private int $blocker; + private string $tag; + private string $canonical; + private DateTimeInterface $modified; + + public function setBlocker(int $blocker): self + { + $this->blocker = $blocker; + return $this; + } + + public function getBlocker(): int + { + return $this->blocker; + } + + public function setTag(string $tag): self + { + $this->tag = $tag; + return $this; + } + + public function getTag(): string + { + return $this->tag; + } + + public function setCanonical(string $canonical): self + { + $this->canonical = $canonical; + return $this; + } + + public function getCanonical(): string + { + return $this->canonical; + } + + public function setModified(DateTimeInterface $modified): self + { + $this->modified = $modified; + return $this; + } + + public function getModified(): DateTimeInterface + { + return $this->modified; + } + + // @codeCoverageIgnoreEnd + // }}} Autocode + + public static function schemaDef(): array + { + return [ + 'name' => 'actor_tag_block', + 'fields' => [ + 'blocker' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'many to one', 'name' => 'actor_block_blocker_fkey', 'not null' => true, 'description' => 'user making the block'], + 'tag' => ['type' => 'varchar', 'length' => Tag::MAX_TAG_LENGTH, 'not null' => true, 'description' => 'hash tag this is blocking'], + 'canonical' => ['type' => 'varchar', 'length' => Tag::MAX_TAG_LENGTH, 'foreign key' => true, 'target' => 'NoteTag.canonical', 'multiplicity' => 'many to one', 'not null' => true, 'description' => 'ascii slug of tag'], + 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'], + ], + 'primary key' => ['blocker', 'canonical'], + ]; + } +} diff --git a/src/Entity/NoteTagBlock.php b/src/Entity/NoteTagBlock.php new file mode 100644 index 0000000000..4f79b66a5e --- /dev/null +++ b/src/Entity/NoteTagBlock.php @@ -0,0 +1,144 @@ +. +// }}} + +namespace App\Entity; + +use App\Core\Cache; +use App\Core\DB\DB; +use App\Core\Entity; +use Component\Tag\Tag; +use DateTimeInterface; +use Functional as F; + +/** + * Entity for User's Note Tag block + * + * @category DB + * @package GNUsocial + * + * @author Hugo Sales + * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later + */ +class NoteTagBlock extends Entity +{ + // {{{ Autocode + // @codeCoverageIgnoreStart + private int $blocker; + private string $tag; + private string $canonical; + private bool $use_canonical; + private DateTimeInterface $modified; + + public function setBlocker(int $blocker): self + { + $this->blocker = $blocker; + return $this; + } + + public function getBlocker(): int + { + return $this->blocker; + } + + public function setTag(string $tag): self + { + $this->tag = $tag; + return $this; + } + + public function getTag(): string + { + return $this->tag; + } + + public function setCanonical(string $canonical): self + { + $this->canonical = $canonical; + return $this; + } + + public function getCanonical(): string + { + return $this->canonical; + } + + public function setUseCanonical(bool $use_canonical): self + { + $this->use_canonical = $use_canonical; + return $this; + } + + public function getUseCanonical(): bool + { + return $this->use_canonical; + } + + public function setModified(DateTimeInterface $modified): self + { + $this->modified = $modified; + return $this; + } + + public function getModified(): DateTimeInterface + { + return $this->modified; + } + + // @codeCoverageIgnoreEnd + // }}} Autocode + + public static function cacheKey(int $actor_id) + { + return "note-tag-blocks-{$actor_id}"; + } + + public static function getFromActorId(int $actor_id) + { + return Cache::getList(self::cacheKey($actor_id), fn () => DB::findBy('note_tag_block', ['blocker' => $actor_id])); + } + + /** + * Check whether $note_tag should + */ + public static function checkBlocksNoteTag(NoteTag $note_tag, array $note_tag_blocks): bool + { + return F\some($note_tag_blocks, fn ($ntb) => ($ntb->getUseCanonical() && $note_tag->getCanonical() === $ntb->getCanonical()) || $note_tag->getTag() === $ntb->getTag()); + } + + public static function schemaDef(): array + { + return [ + 'name' => 'note_tag_block', + 'fields' => [ + 'blocker' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'many to one', 'name' => 'actor_block_blocker_fkey', 'not null' => true, 'description' => 'user making the block'], + 'tag' => ['type' => 'varchar', 'length' => Tag::MAX_TAG_LENGTH, 'not null' => true, 'description' => 'hash tag this is blocking'], + 'canonical' => ['type' => 'varchar', 'length' => Tag::MAX_TAG_LENGTH, 'foreign key' => true, 'target' => 'NoteTag.canonical', 'multiplicity' => 'many to one', 'not null' => true, 'description' => 'ascii slug of tag'], + 'use_canonical' => ['type' => 'bool', 'not null' => true, 'description' => 'whether the user wanted to block canonical tags'], + 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'], + ], + 'primary key' => ['blocker', 'canonical'], + 'indexes' => [ + 'note_tag_block_blocker_idx' => ['blocker'], + ], + ]; + } +}