Notification.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\Notification\Entity;
  20. use App\Core\DB;
  21. use App\Core\Entity;
  22. use App\Entity\Activity;
  23. use App\Entity\Actor;
  24. use DateTimeInterface;
  25. /**
  26. * Entity for Notifications
  27. *
  28. * A Notification when isolated is a form of transient notification.
  29. * When together with a persistent form of notification such as attentions or mentions,
  30. * it records that the target was notified - which avoids re-notifying upon objects reconstructions.
  31. *
  32. * @category DB
  33. * @package GNUsocial
  34. *
  35. * @author Diogo Peralta Cordeiro <@diogo.site>
  36. * @copyright 2021-2022 Free Software Foundation, Inc http://www.fsf.org
  37. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  38. */
  39. class Notification extends Entity
  40. {
  41. // {{{ Autocode
  42. // @codeCoverageIgnoreStart
  43. private int $activity_id;
  44. private int $target_id;
  45. private ?string $reason = null;
  46. private DateTimeInterface $created;
  47. private DateTimeInterface $modified;
  48. public function setActivityId(int $activity_id): self
  49. {
  50. $this->activity_id = $activity_id;
  51. return $this;
  52. }
  53. public function getActivityId(): int
  54. {
  55. return $this->activity_id;
  56. }
  57. public function setTargetId(int $target_id): self
  58. {
  59. $this->target_id = $target_id;
  60. return $this;
  61. }
  62. public function getTargetId(): int
  63. {
  64. return $this->target_id;
  65. }
  66. public function setReason(?string $reason): self
  67. {
  68. $this->reason = \is_null($reason) ? null : mb_substr($reason, 0, 191);
  69. return $this;
  70. }
  71. public function getReason(): ?string
  72. {
  73. return $this->reason;
  74. }
  75. public function setCreated(DateTimeInterface $created): self
  76. {
  77. $this->created = $created;
  78. return $this;
  79. }
  80. public function getCreated(): DateTimeInterface
  81. {
  82. return $this->created;
  83. }
  84. public function setModified(DateTimeInterface $modified): self
  85. {
  86. $this->modified = $modified;
  87. return $this;
  88. }
  89. public function getModified(): DateTimeInterface
  90. {
  91. return $this->modified;
  92. }
  93. // @codeCoverageIgnoreEnd
  94. // }}} Autocode
  95. public function getTarget(): Actor
  96. {
  97. return Actor::getById($this->getTargetId());
  98. }
  99. /**
  100. * Pull the complete list of known activity context notifications for this activity.
  101. *
  102. * @return int[] actor ids (also group profiles)
  103. */
  104. public static function getNotificationTargetIdsByActivity(int|Activity $activity_id): array
  105. {
  106. $notifications = DB::findBy(self::class, ['activity_id' => \is_int($activity_id) ? $activity_id : $activity_id->getId()]);
  107. $targets = [];
  108. foreach ($notifications as $notification) {
  109. $targets[] = $notification->getTargetId();
  110. }
  111. return $targets;
  112. }
  113. /**
  114. * @return int[]
  115. */
  116. public function getNotificationTargetsByActivity(int|Activity $activity_id): array
  117. {
  118. return DB::findBy(Actor::class, ['id' => $this->getNotificationTargetIdsByActivity($activity_id)]);
  119. }
  120. /**
  121. * @return int[]
  122. */
  123. public static function getAllActivitiesTargetedAtActor(Actor $actor): array
  124. {
  125. return DB::dql(<<<'EOF'
  126. SELECT act FROM \App\Entity\Activity AS act
  127. WHERE act.object_type = 'note' AND act.id IN
  128. (SELECT att.activity_id FROM \Component\Notification\Entity\Notification AS att WHERE att.target_id = :id)
  129. EOF, ['id' => $actor->getId()]);
  130. }
  131. public static function schemaDef(): array
  132. {
  133. return [
  134. 'name' => 'notification',
  135. 'description' => 'Activity notification for actors (that are not a mention and not result of a subscription)',
  136. 'fields' => [
  137. 'activity_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Activity.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'activity_id to give attention'],
  138. 'target_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'actor_id for feed receiver'],
  139. 'reason' => ['type' => 'varchar', 'length' => 191, 'description' => 'Optional reason why this was brought to the attention of actor_id'],
  140. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
  141. 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  142. ],
  143. 'primary key' => ['activity_id', 'target_id'],
  144. 'indexes' => [
  145. 'attention_activity_id_idx' => ['activity_id'],
  146. 'attention_target_id_idx' => ['target_id'],
  147. ],
  148. ];
  149. }
  150. }