NoteRepeat.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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;
  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 static function cacheKeys(int|Note $note_id, null|int|Actor|LocalUser $actor_id = null): array
  73. {
  74. $note_id = \is_int($note_id) ? $note_id : $note_id->getId();
  75. $actor_id = \is_null($actor_id) ? null : (\is_int($actor_id) ? $actor_id : $actor_id->getId());
  76. return [
  77. 'is_repeat' => "note-repeat-is-{$note_id}",
  78. 'repeats' => "note-repeats-{$note_id}",
  79. 'actor_repeat' => "note-repeat-{$note_id}-{$actor_id}",
  80. ];
  81. }
  82. public static function getNoteActorRepeat(int|Note $note_id, int $actor_id): ?self
  83. {
  84. $note_id = \is_int($note_id) ? $note_id : $note_id->getId();
  85. return Cache::get(
  86. self::cacheKeys($note_id, $actor_id)['actor_repeat'],
  87. fn () => DB::findOneBy('note_repeat', [
  88. 'actor_id' => $actor_id,
  89. 'repeat_of' => $note_id,
  90. ], return_null: true),
  91. );
  92. }
  93. /**
  94. * @return bool Returns true if Note provided is a repeat of another Note
  95. */
  96. public static function isNoteRepeat(Note $note): bool
  97. {
  98. return Cache::get(self::cacheKeys($note)['is_repeat'], fn () => DB::count(self::class, ['note_id' => $note->getId()]) > 0);
  99. }
  100. public static function getNoteRepeats(Note $note): array
  101. {
  102. return Cache::getList(
  103. self::cacheKeys($note)['repeats'],
  104. fn () => DB::dql(
  105. <<<'EOF'
  106. select n from note as n
  107. inner join note_repeat as nr
  108. with nr.note_id = n.id
  109. where nr.repeat_of = :note_id
  110. order by n.created DESC, n.id DESC
  111. EOF,
  112. ['note_id' => $note->getId()],
  113. ),
  114. );
  115. }
  116. public static function schemaDef(): array
  117. {
  118. return [
  119. 'name' => 'note_repeat',
  120. 'fields' => [
  121. 'note_id' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'description' => 'The id of the repeat itself'],
  122. 'actor_id' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'description' => 'Who made this repeat'],
  123. 'repeat_of' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'description' => 'Note this is a repeat of'],
  124. ],
  125. 'primary key' => ['note_id'],
  126. 'foreign keys' => [
  127. 'note_id_to_id_fkey' => ['note', ['note_id' => 'id']],
  128. 'note_repeat_of_id_fkey' => ['note', ['repeat_of' => 'id']],
  129. 'actor_reply_to_id_fkey' => ['actor', ['actor_id' => 'id']],
  130. ],
  131. 'indexes' => [
  132. 'note_repeat_of_idx' => ['repeat_of'],
  133. ],
  134. ];
  135. }
  136. }