NoteRepeat.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 Plugin\RepeatNote\Entity;
  20. use App\Core\Cache;
  21. use App\Core\DB\DB;
  22. use App\Core\Entity;
  23. use App\Entity\Actor;
  24. use App\Entity\LocalUser;
  25. use App\Entity\Note;
  26. /**
  27. * Entity for notices
  28. *
  29. * @category DB
  30. * @package GNUsocial
  31. *
  32. * @author Eliseu Amaro <mail@eliseuama.ro>
  33. * @copyright 2020-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 NoteRepeat extends Entity
  37. {
  38. // {{{ Autocode
  39. // @codeCoverageIgnoreStart
  40. private int $note_id;
  41. private int $actor_id;
  42. private int $repeat_of;
  43. public function setNoteId(int $note_id): self
  44. {
  45. $this->note_id = $note_id;
  46. return $this;
  47. }
  48. public function getNoteId(): int
  49. {
  50. return $this->note_id;
  51. }
  52. public function setActorId(int $actor_id): self
  53. {
  54. $this->actor_id = $actor_id;
  55. return $this;
  56. }
  57. public function getActorId(): int
  58. {
  59. return $this->actor_id;
  60. }
  61. public function setRepeatOf(int $repeat_of): self
  62. {
  63. $this->repeat_of = $repeat_of;
  64. return $this;
  65. }
  66. public function getRepeatOf(): int
  67. {
  68. return $this->repeat_of;
  69. }
  70. // @codeCoverageIgnoreEnd
  71. // }}} Autocode
  72. public function getAttentionTargetIds(): array
  73. {
  74. return Note::getById($this->getNoteId())->getAttentionTargetIds();
  75. }
  76. public function getNotifiedTargetIds(): array
  77. {
  78. return Note::getById($this->getNoteId())->getNotifiedTargetIds();
  79. }
  80. public static function cacheKeys(int|Note $note_id, null|int|Actor|LocalUser $actor_id = null): array
  81. {
  82. $note_id = \is_int($note_id) ? $note_id : $note_id->getId();
  83. $actor_id = \is_null($actor_id) ? null : (\is_int($actor_id) ? $actor_id : $actor_id->getId());
  84. return [
  85. 'is_repeat' => "note-repeat-is-{$note_id}",
  86. 'repeats' => "note-repeats-{$note_id}",
  87. 'actor_repeat' => "note-repeat-{$note_id}-{$actor_id}",
  88. ];
  89. }
  90. public static function getNoteActorRepeat(int|Note $note_id, int $actor_id): ?self
  91. {
  92. $note_id = \is_int($note_id) ? $note_id : $note_id->getId();
  93. return Cache::get(
  94. self::cacheKeys($note_id, $actor_id)['actor_repeat'],
  95. fn () => DB::findOneBy('note_repeat', [
  96. 'actor_id' => $actor_id,
  97. 'repeat_of' => $note_id,
  98. ], return_null: true),
  99. );
  100. }
  101. /**
  102. * @return bool Returns true if Note provided is a repeat of another Note
  103. */
  104. public static function isNoteRepeat(Note $note): bool
  105. {
  106. return Cache::get(self::cacheKeys($note)['is_repeat'], fn () => DB::count(self::class, ['note_id' => $note->getId()]) > 0);
  107. }
  108. public static function getNoteRepeats(Note $note): array
  109. {
  110. return Cache::getList(
  111. self::cacheKeys($note)['repeats'],
  112. fn () => DB::dql(
  113. <<<'EOF'
  114. select n from note as n
  115. inner join note_repeat as nr
  116. with nr.note_id = n.id
  117. where nr.repeat_of = :note_id
  118. order by n.created DESC, n.id DESC
  119. EOF,
  120. ['note_id' => $note->getId()],
  121. ),
  122. );
  123. }
  124. public static function schemaDef(): array
  125. {
  126. return [
  127. 'name' => 'note_repeat',
  128. 'fields' => [
  129. 'note_id' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'description' => 'The id of the repeat itself'],
  130. 'actor_id' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'description' => 'Who made this repeat'],
  131. 'repeat_of' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'description' => 'Note this is a repeat of'],
  132. ],
  133. 'primary key' => ['note_id'],
  134. 'foreign keys' => [
  135. 'note_id_to_id_fkey' => ['note', ['note_id' => 'id']],
  136. 'note_repeat_of_id_fkey' => ['note', ['repeat_of' => 'id']],
  137. 'actor_reply_to_id_fkey' => ['actor', ['actor_id' => 'id']],
  138. ],
  139. 'indexes' => [
  140. 'note_repeat_of_idx' => ['repeat_of'],
  141. ],
  142. ];
  143. }
  144. }