ActorCircle.php 7.4 KB

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