ActorCircle.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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\Circle\Entity;
  20. use App\Core\Cache;
  21. use App\Core\DB;
  22. use App\Core\Entity;
  23. use App\Core\Router;
  24. use App\Entity\Actor;
  25. use DateTimeInterface;
  26. /**
  27. * Entity for List of actors
  28. * This entity only makes sense when considered together with the ActorTag one.
  29. * Because, every circle entry will be an ActorTag.
  30. *
  31. * @category DB
  32. * @package GNUsocial
  33. *
  34. * @author Zach Copley <zach@status.net>
  35. * @copyright 2010 StatusNet Inc.
  36. * @author Mikael Nordfeldth <mmn@hethane.se>
  37. * @copyright 2009-2014 Free Software Foundation, Inc http://www.fsf.org
  38. * @author Hugo Sales <hugo@hsal.es>
  39. * @author Diogo Peralta Cordeiro <@diogo.site>
  40. * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
  41. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  42. */
  43. class ActorCircle extends Entity
  44. {
  45. // {{{ Autocode
  46. // @codeCoverageIgnoreStart
  47. private int $id;
  48. private ?int $tagger = null;
  49. private string $tag;
  50. private ?string $description = null;
  51. private ?bool $private = false;
  52. private DateTimeInterface $created;
  53. private DateTimeInterface $modified;
  54. public function setId(int $id): self
  55. {
  56. $this->id = $id;
  57. return $this;
  58. }
  59. public function getId(): int
  60. {
  61. return $this->id;
  62. }
  63. public function setTagger(?int $tagger): self
  64. {
  65. $this->tagger = $tagger;
  66. return $this;
  67. }
  68. public function getTagger(): ?int
  69. {
  70. return $this->tagger;
  71. }
  72. public function setTag(string $tag): self
  73. {
  74. $this->tag = mb_substr($tag, 0, 64);
  75. return $this;
  76. }
  77. public function getTag(): string
  78. {
  79. return $this->tag;
  80. }
  81. public function setDescription(?string $description): self
  82. {
  83. $this->description = $description;
  84. return $this;
  85. }
  86. public function getDescription(): ?string
  87. {
  88. return $this->description;
  89. }
  90. public function setPrivate(?bool $private): self
  91. {
  92. $this->private = $private;
  93. return $this;
  94. }
  95. public function getPrivate(): ?bool
  96. {
  97. return $this->private;
  98. }
  99. public function setCreated(DateTimeInterface $created): self
  100. {
  101. $this->created = $created;
  102. return $this;
  103. }
  104. public function getCreated(): DateTimeInterface
  105. {
  106. return $this->created;
  107. }
  108. public function setModified(DateTimeInterface $modified): self
  109. {
  110. $this->modified = $modified;
  111. return $this;
  112. }
  113. public function getModified(): DateTimeInterface
  114. {
  115. return $this->modified;
  116. }
  117. // @codeCoverageIgnoreEnd
  118. // }}} Autocode
  119. /**
  120. * For use with MetaCollection trait only
  121. */
  122. public function getName(): string
  123. {
  124. return $this->tag;
  125. }
  126. /**
  127. * @return ActorTag[]
  128. */
  129. public function getActorTags(bool $db_reference = false): array
  130. {
  131. $handle = fn () => DB::findBy('actor_tag', ['tagger' => $this->getTagger(), 'tag' => $this->getTag()]);
  132. if ($db_reference) {
  133. return $handle();
  134. }
  135. return Cache::get(
  136. "circle-{$this->getId()}-tagged",
  137. $handle,
  138. );
  139. }
  140. /**
  141. * @return Actor[]
  142. */
  143. public function getTaggedActors(): array
  144. {
  145. return Cache::get(
  146. "circle-{$this->getId()}-tagged-actors",
  147. function () {
  148. if ($this->getTagger()) {
  149. return DB::dql('SELECT a FROM actor AS a JOIN actor_tag AS at WITH at.tagged = a.id WHERE at.tag = :tag AND at.tagger = :tagger', ['tag' => $this->getTag(), 'tagger' => $this->getTagger()]);
  150. } else { // Self-tag
  151. return DB::dql('SELECT a FROM actor AS a JOIN actor_tag AS at WITH at.tagged = a.id WHERE at.tag = :tag AND at.tagger = at.tagged', ['tag' => $this->getTag()]);
  152. }
  153. },
  154. );
  155. }
  156. /**
  157. * @return Actor[]
  158. */
  159. public function getSubscribedActors(?int $offset = null, ?int $limit = null): array
  160. {
  161. return Cache::get(
  162. "circle-{$this->getId()}-subscribers",
  163. fn () => DB::dql(
  164. <<< 'EOQ'
  165. SELECT a
  166. FROM actor a
  167. JOIN actor_circle_subscription s
  168. WITH a.id = s.actor_id
  169. ORDER BY s.created DESC, a.id DESC
  170. EOQ,
  171. options: [
  172. 'offset' => $offset,
  173. 'limit' => $limit,
  174. ],
  175. ),
  176. );
  177. }
  178. public function getUrl(int $type = Router::ABSOLUTE_PATH): string
  179. {
  180. return Router::url('actor_circle_view_by_circle_id', ['circle_id' => $this->getId()], type: $type);
  181. }
  182. public static function schemaDef(): array
  183. {
  184. return [
  185. 'name' => 'actor_circle',
  186. 'description' => 'An actor can have lists of actors, to separate their feed or quickly mention his friend',
  187. 'fields' => [
  188. 'id' => ['type' => 'serial', 'not null' => true, 'description' => 'unique identifier'], // An actor can be tagged by many actors
  189. 'tagger' => ['type' => 'int', 'default' => null, 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'many to one', 'name' => 'actor_list_tagger_fkey', 'description' => 'user making the tag, null if self-tag. If null, is the special global self-tag circle'],
  190. 'tag' => ['type' => 'varchar', 'length' => 64, 'foreign key' => true, 'target' => 'ActorTag.tag', 'multiplicity' => 'many to one', 'not null' => true, 'description' => 'actor tag'], // Join with ActorTag // // so, Doctrine doesn't like that the target is not unique, even though the pair is // Many Actor Circles can reference (and probably will) an Actor Tag
  191. 'description' => ['type' => 'text', 'description' => 'description of the people tag'],
  192. 'private' => ['type' => 'bool', 'default' => false, 'description' => 'is this tag private'],
  193. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
  194. 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  195. ],
  196. 'primary key' => ['id'], // But we will mostly refer to them with `tagger` and `tag`
  197. 'indexes' => [
  198. 'actor_list_modified_idx' => ['modified'],
  199. 'actor_list_tagger_tag_idx' => ['tagger', 'tag'], // The actual identifier we will use the most
  200. 'actor_list_tag_idx' => ['tag'],
  201. 'actor_list_tagger_idx' => ['tagger'],
  202. ],
  203. ];
  204. }
  205. }