NoteFavourite.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\Favourite\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. use DateTimeInterface;
  27. class NoteFavourite extends Entity
  28. {
  29. // {{{ Autocode
  30. // @codeCoverageIgnoreStart
  31. private int $note_id;
  32. private int $actor_id;
  33. private DateTimeInterface $created;
  34. private DateTimeInterface $modified;
  35. public function setNoteId(int $note_id): self
  36. {
  37. $this->note_id = $note_id;
  38. return $this;
  39. }
  40. public function getNoteId(): int
  41. {
  42. return $this->note_id;
  43. }
  44. public function setActorId(int $actor_id): self
  45. {
  46. $this->actor_id = $actor_id;
  47. return $this;
  48. }
  49. public function getActorId(): int
  50. {
  51. return $this->actor_id;
  52. }
  53. public function setCreated(DateTimeInterface $created): self
  54. {
  55. $this->created = $created;
  56. return $this;
  57. }
  58. public function getCreated(): DateTimeInterface
  59. {
  60. return $this->created;
  61. }
  62. public function setModified(DateTimeInterface $modified): self
  63. {
  64. $this->modified = $modified;
  65. return $this;
  66. }
  67. public function getModified(): DateTimeInterface
  68. {
  69. return $this->modified;
  70. }
  71. // @codeCoverageIgnoreEnd
  72. // }}} Autocode
  73. public static function cacheKeys(int|Note $note_id, int|Actor|LocalUser|null $actor_id = null): array
  74. {
  75. $note_id = \is_int($note_id) ? $note_id : $note_id->getId();
  76. $actor_id = \is_null($actor_id) ? null : (\is_int($actor_id) ? $actor_id : $actor_id->getId());
  77. return [
  78. 'favourite' => "note-favourite-{$note_id}-{$actor_id}",
  79. 'favourites' => "note-favourites-{$note_id}",
  80. 'favourites-actors' => "note-favourites-actors-{$note_id}",
  81. ];
  82. }
  83. public static function getNoteFavourites(Note $note): array
  84. {
  85. return Cache::getList(
  86. self::cacheKeys($note)['favourites'],
  87. fn () => DB::findBy('note_favourite', ['note_id' => $note->getId()]),
  88. );
  89. }
  90. public static function getNoteFavouriteActors(Note $note): array
  91. {
  92. return Cache::getList(
  93. self::cacheKeys($note)['favourites-actors'],
  94. fn () => DB::dql(
  95. <<<'EOF'
  96. select a from actor a
  97. inner join note_favourite nf
  98. with a.id = nf.actor_id
  99. where nf.note_id = :note_id
  100. order by nf.created DESC
  101. EOF,
  102. ['note_id' => $note->getId()],
  103. ),
  104. );
  105. }
  106. /**
  107. * @see Entity->getNotificationTargetIds
  108. */
  109. public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array
  110. {
  111. if (!\array_key_exists('object', $ids_already_known)) {
  112. $target_ids = Note::getById($this->getNoteId())->getNotificationTargetIds();
  113. } else {
  114. $target_ids = $ids_already_known['object'];
  115. }
  116. // Additional actors that should know about this
  117. if ($include_additional && \array_key_exists('additional', $ids_already_known)) {
  118. array_push($target_ids, ...$ids_already_known['additional']);
  119. } else {
  120. return $target_ids;
  121. }
  122. return array_unique($target_ids);
  123. }
  124. public static function schemaDef()
  125. {
  126. return [
  127. 'name' => 'note_favourite',
  128. 'fields' => [
  129. 'note_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'App\Entity\Note.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'note that is the favorite of'],
  130. 'actor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'App\Entity\Actor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'actor who favourited this note'], // note: formerly referenced notice.id, but we can now record remote users' favorites
  131. 'created' => ['type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'],
  132. 'modified' => ['type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'],
  133. ],
  134. 'primary key' => ['note_id', 'actor_id'],
  135. 'indexes' => [
  136. 'fave_note_id_idx' => ['note_id'],
  137. 'fave_actor_id_idx' => ['actor_id', 'modified'],
  138. 'fave_modified_idx' => ['modified'],
  139. ],
  140. ];
  141. }
  142. }