NoteTagBlock.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Component\Tag\Entity;
  20. use App\Core\Cache;
  21. use App\Core\DB\DB;
  22. use App\Core\Entity;
  23. use Component\Tag\Tag;
  24. use DateTimeInterface;
  25. use Functional as F;
  26. /**
  27. * Entity for User's Note Tag block
  28. *
  29. * @category DB
  30. * @package GNUsocial
  31. *
  32. * @author Hugo Sales <hugo@hsal.es>
  33. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  34. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  35. */
  36. class NoteTagBlock extends Entity
  37. {
  38. // {{{ Autocode
  39. // @codeCoverageIgnoreStart
  40. private int $blocker;
  41. private string $tag;
  42. private string $canonical;
  43. private bool $use_canonical;
  44. private DateTimeInterface $modified;
  45. public function setBlocker(int $blocker): self
  46. {
  47. $this->blocker = $blocker;
  48. return $this;
  49. }
  50. public function getBlocker(): int
  51. {
  52. return $this->blocker;
  53. }
  54. public function setTag(string $tag): self
  55. {
  56. $this->tag = mb_substr($tag, 0, 64);
  57. return $this;
  58. }
  59. public function getTag(): string
  60. {
  61. return $this->tag;
  62. }
  63. public function setCanonical(string $canonical): self
  64. {
  65. $this->canonical = mb_substr($canonical, 0, 64);
  66. return $this;
  67. }
  68. public function getCanonical(): string
  69. {
  70. return $this->canonical;
  71. }
  72. public function setUseCanonical(bool $use_canonical): self
  73. {
  74. $this->use_canonical = $use_canonical;
  75. return $this;
  76. }
  77. public function getUseCanonical(): bool
  78. {
  79. return $this->use_canonical;
  80. }
  81. public function setModified(DateTimeInterface $modified): self
  82. {
  83. $this->modified = $modified;
  84. return $this;
  85. }
  86. public function getModified(): DateTimeInterface
  87. {
  88. return $this->modified;
  89. }
  90. // @codeCoverageIgnoreEnd
  91. // }}} Autocode
  92. public static function cacheKey(int $actor_id)
  93. {
  94. return "note-tag-blocks-{$actor_id}";
  95. }
  96. public static function getByActorId(int $actor_id)
  97. {
  98. return Cache::getList(self::cacheKey($actor_id), fn () => DB::findBy('note_tag_block', ['blocker' => $actor_id]));
  99. }
  100. /**
  101. * Check whether $note_tag is considered blocked by one of
  102. * $note_tag_blocks
  103. */
  104. public static function checkBlocksNoteTag(NoteTag $note_tag, array $note_tag_blocks): bool
  105. {
  106. return F\some($note_tag_blocks, fn ($ntb) => ($ntb->getUseCanonical() && $note_tag->getCanonical() === $ntb->getCanonical()) || $note_tag->getTag() === $ntb->getTag());
  107. }
  108. public static function schemaDef(): array
  109. {
  110. return [
  111. 'name' => 'note_tag_block',
  112. 'fields' => [
  113. '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'],
  114. 'tag' => ['type' => 'varchar', 'length' => Tag::MAX_TAG_LENGTH, 'not null' => true, 'description' => 'hash tag this is blocking'],
  115. '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'],
  116. 'use_canonical' => ['type' => 'bool', 'not null' => true, 'description' => 'whether the user wanted to block canonical tags'],
  117. 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  118. ],
  119. 'primary key' => ['blocker', 'canonical'],
  120. 'indexes' => [
  121. 'note_tag_block_blocker_idx' => ['blocker'],
  122. ],
  123. ];
  124. }
  125. }