Notification.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\DB;
  21. use App\Core\Entity;
  22. use App\Entity\Activity;
  23. use App\Entity\Actor;
  24. use DateTimeInterface;
  25. /**
  26. * Entity for attentions
  27. *
  28. * @category DB
  29. * @package GNUsocial
  30. *
  31. * @author Zach Copley <zach@status.net>
  32. * @copyright 2010 StatusNet Inc.
  33. * @author Mikael Nordfeldth <mmn@hethane.se>
  34. * @copyright 2009-2014 Free Software Foundation, Inc http://www.fsf.org
  35. * @author Hugo Sales <hugo@hsal.es>
  36. * @copyright 2020-2021 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 array of integer actor ids (also group profiles)
  103. */
  104. public static function getNotificationTargetIdsByActivity(int|Activity $activity_id): array
  105. {
  106. $notifications = DB::findBy('notification', ['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. public function getNotificationTargetsByActivity(int|Activity $activity_id): array
  114. {
  115. return DB::findBy('actor', ['id' => $this->getNotificationTargetIdsByActivity($activity_id)]);
  116. }
  117. public static function schemaDef(): array
  118. {
  119. return [
  120. 'name' => 'notification',
  121. 'description' => 'Activity notification for actors (that are not a mention and not result of a subscription)',
  122. 'fields' => [
  123. 'activity_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Activity.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'activity_id to give attention'],
  124. 'target_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'actor_id for feed receiver'],
  125. 'reason' => ['type' => 'varchar', 'length' => 191, 'description' => 'Optional reason why this was brought to the attention of actor_id'],
  126. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
  127. 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  128. ],
  129. 'primary key' => ['activity_id', 'target_id'],
  130. 'indexes' => [
  131. 'attention_activity_id_idx' => ['activity_id'],
  132. 'attention_target_id_idx' => ['target_id'],
  133. ],
  134. ];
  135. }
  136. }