ActorSubscription.php 5.1 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 Component\Subscription\Entity;
  20. use App\Core\Entity;
  21. use App\Entity\Actor;
  22. use App\Entity\LocalUser;
  23. use Component\Group\Entity\LocalGroup;
  24. use DateTimeInterface;
  25. /**
  26. * Entity for subscription
  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 ActorSubscription extends Entity
  40. {
  41. // {{{ Autocode
  42. // @codeCoverageIgnoreStart
  43. private int $subscriber_id;
  44. private int $subscribed_id;
  45. private DateTimeInterface $created;
  46. private DateTimeInterface $modified;
  47. public function setSubscriberId(int $subscriber_id): self
  48. {
  49. $this->subscriber_id = $subscriber_id;
  50. return $this;
  51. }
  52. public function getSubscriberId(): int
  53. {
  54. return $this->subscriber_id;
  55. }
  56. public function setSubscribedId(int $subscribed_id): self
  57. {
  58. $this->subscribed_id = $subscribed_id;
  59. return $this;
  60. }
  61. public function getSubscribedId(): int
  62. {
  63. return $this->subscribed_id;
  64. }
  65. public function setCreated(DateTimeInterface $created): self
  66. {
  67. $this->created = $created;
  68. return $this;
  69. }
  70. public function getCreated(): DateTimeInterface
  71. {
  72. return $this->created;
  73. }
  74. public function setModified(DateTimeInterface $modified): self
  75. {
  76. $this->modified = $modified;
  77. return $this;
  78. }
  79. public function getModified(): DateTimeInterface
  80. {
  81. return $this->modified;
  82. }
  83. // @codeCoverageIgnoreEnd
  84. // }}} Autocode
  85. public function getSubscriber(): Actor
  86. {
  87. return Actor::getById($this->getSubscriberId());
  88. }
  89. public function getSubscribed(): Actor
  90. {
  91. return Actor::getById($this->getSubscribedId());
  92. }
  93. public static function cacheKeys(LocalUser|LocalGroup|Actor $subject, LocalUser|LocalGroup|Actor $target): array
  94. {
  95. return [
  96. 'subscribed' => "subscription-{$subject->getId()}-{$target->getId()}",
  97. ];
  98. }
  99. /**
  100. * @see Entity->getNotificationTargetIds
  101. */
  102. public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array
  103. {
  104. if (!\array_key_exists('object', $ids_already_known)) {
  105. $target_ids = [$this->getSubscribedId()]; // The object of any subscription is the one subscribed (or unsubscribed)
  106. } else {
  107. $target_ids = $ids_already_known['object'];
  108. }
  109. // Additional actors that should know about this
  110. if ($include_additional && \array_key_exists('additional', $ids_already_known)) {
  111. array_push($target_ids, ...$ids_already_known['additional']);
  112. } else {
  113. return $target_ids;
  114. }
  115. return array_unique($target_ids);
  116. }
  117. public static function schemaDef(): array
  118. {
  119. return [
  120. 'name' => 'actor_subscription',
  121. 'fields' => [
  122. 'subscriber_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'name' => 'subscription_subscriber_fkey', 'not null' => true, 'description' => 'actor listening'],
  123. 'subscribed_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'name' => 'subscription_subscribed_fkey', 'not null' => true, 'description' => 'actor being listened to'],
  124. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
  125. 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  126. ],
  127. 'primary key' => ['subscriber_id', 'subscribed_id'],
  128. 'indexes' => [
  129. 'subscription_subscriber_idx' => ['subscriber_id', 'created'],
  130. 'subscription_subscribed_idx' => ['subscribed_id', 'created'],
  131. ],
  132. ];
  133. }
  134. }