Circle.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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;
  20. use App\Core\Cache;
  21. use App\Core\DB;
  22. use App\Core\Event;
  23. use function App\Core\I18n\_m;
  24. use App\Core\Modules\Component;
  25. use App\Core\Router;
  26. use App\Entity\Actor;
  27. use App\Entity\Feed;
  28. use App\Entity\LocalUser;
  29. use App\Util\Nickname;
  30. use Component\Circle\Controller as CircleController;
  31. use Component\Circle\Entity\ActorCircle;
  32. use Component\Circle\Entity\ActorCircleSubscription;
  33. use Component\Circle\Entity\ActorTag;
  34. use Component\Collection\Util\MetaCollectionTrait;
  35. use Component\Tag\Tag;
  36. use EventResult;
  37. use Functional as F;
  38. use Symfony\Component\HttpFoundation\Request;
  39. /**
  40. * Component responsible for handling and representing ActorCircles and ActorTags
  41. *
  42. * @author Hugo Sales <hugo@hsal.es>
  43. * @author Phablulo <phablulo@gmail.com>
  44. * @author Diogo Peralta Cordeiro <@diogo.site>
  45. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  46. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  47. */
  48. class Circle extends Component
  49. {
  50. /** @phpstan-use MetaCollectionTrait<ActorCircle> */
  51. use MetaCollectionTrait;
  52. public const TAG_CIRCLE_REGEX = '/' . Nickname::BEFORE_MENTIONS . '@#([\pL\pN_\-\.]{1,64})/';
  53. protected const SLUG = 'circle';
  54. protected const PLURAL_SLUG = 'circles';
  55. public function onAddRoute(Router $r): EventResult
  56. {
  57. $r->connect('actor_circle_view_by_circle_id', '/circle/{circle_id<\d+>}', [CircleController\Circle::class, 'circleById']);
  58. // View circle members by (tagger id or nickname) and tag
  59. $r->connect('actor_circle_view_by_circle_tagger_tag', '/circle/actor/{tagger_id<\d+>/{tag<' . Tag::TAG_SLUG_REGEX . '>}}', [CircleController\Circle::class, 'circleByTaggerIdAndTag']);
  60. $r->connect('actor_circle_view_by_circle_tagger_tag', '/circle/@{nickname<' . Nickname::DISPLAY_FMT . '>}/{tag<' . Tag::TAG_SLUG_REGEX . '>}', [CircleController\Circle::class, 'circleByTaggerNicknameAndTag']);
  61. // View all circles by actor id or nickname
  62. $r->connect(
  63. id: 'actor_circles_view_by_actor_id',
  64. uri_path: '/actor/{tag<' . Tag::TAG_SLUG_REGEX . '>}/circles',
  65. target: [CircleController\Circles::class, 'collectionsViewByActorId'],
  66. );
  67. $r->connect(
  68. id: 'actor_circles_view_by_nickname',
  69. uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/circles',
  70. target: [CircleController\Circles::class, 'collectionsViewByActorNickname'],
  71. );
  72. $r->connect('actor_circle_view_feed_by_circle_id', '/circle/{circle_id<\d+>}/feed', [CircleController\Circles::class, 'feedByCircleId']);
  73. // View circle feed by (tagger id or nickname) and tag
  74. $r->connect('actor_circle_view_feed_by_circle_tagger_tag', '/circle/actor/{tagger_id<\d+>/{tag<' . Tag::TAG_SLUG_REGEX . '>}}/feed', [CircleController\Circles::class, 'feedByTaggerIdAndTag']);
  75. $r->connect('actor_circle_view_feed_by_circle_tagger_tag', '/circle/@{nickname<' . Nickname::DISPLAY_FMT . '>}/{tag<' . Tag::TAG_SLUG_REGEX . '>}/feed', [CircleController\Circles::class, 'feedByTaggerNicknameAndTag']);
  76. return Event::next;
  77. }
  78. public static function cacheKeys(string $tag_single_or_multi): array
  79. {
  80. return [
  81. 'actor_single' => "actor-tag-feed-{$tag_single_or_multi}",
  82. 'actor_multi' => "actor-tags-feed-{$tag_single_or_multi}",
  83. ];
  84. }
  85. public function onPopulateSettingsTabs(Request $request, string $section, array &$tabs): EventResult
  86. {
  87. if ($section === 'profile' && \in_array($request->get('_route'), ['person_actor_settings', 'group_actor_settings'])) {
  88. $tabs[] = [
  89. 'title' => _m('Self tags'),
  90. 'desc' => _m('Add or remove tags to this actor'),
  91. 'id' => 'settings-self-tags',
  92. 'controller' => CircleController\SelfTagsSettings::settingsSelfTags($request, Actor::getById((int) $request->get('id')), 'settings-self-tags-details'),
  93. ];
  94. }
  95. return Event::next;
  96. }
  97. /**
  98. * @param Actor[] $targets
  99. */
  100. public function onPostingFillTargetChoices(Request $request, Actor $actor, array &$targets): EventResult
  101. {
  102. $circles = $actor->getCircles();
  103. foreach ($circles as $circle) {
  104. $tag = $circle->getTag();
  105. $targets["#{$tag}"] = $tag;
  106. }
  107. return Event::next;
  108. }
  109. // Meta Collection -------------------------------------------------------------------
  110. /**
  111. * @param array<string, mixed> $vars
  112. */
  113. private function getActorIdFromVars(array $vars): int
  114. {
  115. $id = $vars['request']->get('id', null);
  116. if ($id) {
  117. return (int) $id;
  118. }
  119. $nick = $vars['request']->get('nickname');
  120. $user = LocalUser::getByNickname($nick);
  121. return $user->getId();
  122. }
  123. public static function createCircle(Actor|int $tagger_id, string $tag): int|null
  124. {
  125. $tagger_id = \is_int($tagger_id) ? $tagger_id : $tagger_id->getId();
  126. $circle = ActorCircle::create([
  127. 'tagger' => $tagger_id,
  128. 'tag' => $tag,
  129. 'description' => null, // TODO
  130. 'private' => false, // TODO
  131. ]);
  132. DB::persist($circle);
  133. Cache::delete(Actor::cacheKeys($tagger_id)['circles']);
  134. return $circle->getId();
  135. }
  136. /**
  137. * @param array<string, mixed> $vars
  138. */
  139. protected function createCollection(Actor $owner, array $vars, string $name): void
  140. {
  141. $this->createCircle($owner, $name);
  142. DB::persist(ActorTag::create([
  143. 'tagger' => $owner->getId(),
  144. 'tagged' => self::getActorIdFromVars($vars),
  145. 'tag' => $name,
  146. ]));
  147. }
  148. /**
  149. * @param array<string, mixed> $vars
  150. * @param array<int> $items
  151. * @param array<mixed> $collections
  152. */
  153. protected function removeItem(Actor $owner, array $vars, array $items, array $collections): bool
  154. {
  155. $tagger_id = $owner->getId();
  156. $tagged_id = $this->getActorIdFromVars($vars);
  157. $circles_to_remove_tagged_from = DB::findBy(ActorCircle::class, ['id' => $items]);
  158. foreach ($circles_to_remove_tagged_from as $circle) {
  159. DB::removeBy(ActorCircleSubscription::class, ['actor_id' => $tagged_id, 'circle_id' => $circle->getId()]);
  160. }
  161. $tags = F\map($circles_to_remove_tagged_from, fn ($x) => $x->getTag());
  162. foreach ($tags as $tag) {
  163. DB::removeBy(ActorTag::class, ['tagger' => $tagger_id, 'tagged' => $tagged_id, 'tag' => $tag]);
  164. }
  165. Cache::delete(Actor::cacheKeys($tagger_id)['circles']);
  166. return true;
  167. }
  168. /**
  169. * @param array<string, mixed> $vars
  170. * @param array<int> $items
  171. * @param array<mixed> $collections
  172. */
  173. protected function addItem(Actor $owner, array $vars, array $items, array $collections): void
  174. {
  175. $tagger_id = $owner->getId();
  176. $tagged_id = $this->getActorIdFromVars($vars);
  177. $circles_to_add_tagged_to = DB::findBy(ActorCircle::class, ['id' => $items]);
  178. foreach ($circles_to_add_tagged_to as $circle) {
  179. DB::persist(ActorCircleSubscription::create(['actor_id' => $tagged_id, 'circle_id' => $circle->getId()]));
  180. }
  181. $tags = F\map($circles_to_add_tagged_to, fn ($x) => $x->getTag());
  182. foreach ($tags as $tag) {
  183. DB::persist(ActorTag::create(['tagger' => $tagger_id, 'tagged' => $tagged_id, 'tag' => $tag]));
  184. }
  185. Cache::delete(Actor::cacheKeys($tagger_id)['circles']);
  186. }
  187. /**
  188. * @see MetaCollectionPlugin->shouldAddToRightPanel
  189. *
  190. * @param array<string, mixed> $vars
  191. */
  192. protected function shouldAddToRightPanel(Actor $user, array $vars, Request $request): bool
  193. {
  194. return \in_array($vars['path'], ['actor_view_nickname', 'actor_view_id']);
  195. }
  196. /**
  197. * Retrieves an array of Collections owned by an Actor.
  198. * In this case, Collections of those within Actor's own circle of Actors, aka ActorCircle.
  199. *
  200. * Differs from the overwritten method in MetaCollectionsTrait, since retrieved Collections come from the $owner
  201. * itself, and from every Actor that is a part of its ActorCircle.
  202. *
  203. * @param Actor $owner the Actor, and by extension its own circle of Actors
  204. * @param null|array<string, mixed> $vars Page vars sent by AppendRightPanelBlock event
  205. * @param bool $ids_only true if only the Collections ids are to be returned
  206. *
  207. * @return ($ids_only is true ? int[] : ActorCircle[])
  208. */
  209. protected function getCollectionsBy(Actor $owner, ?array $vars = null, bool $ids_only = false): array
  210. {
  211. $tagged_id = !\is_null($vars) ? $this->getActorIdFromVars($vars) : null;
  212. $circles = \is_null($tagged_id) ? $owner->getCircles() : F\select($owner->getCircles(), function ($x) use ($tagged_id) {
  213. foreach ($x->getActorTags() as $at) {
  214. if ($at->getTagged() === $tagged_id) {
  215. return true;
  216. }
  217. }
  218. return false;
  219. });
  220. return $ids_only ? array_map(fn ($x) => $x->getId(), $circles) : $circles;
  221. }
  222. public function onCreateDefaultFeeds(int $actor_id, LocalUser $user, int &$ordering): EventResult
  223. {
  224. DB::persist(Feed::create([
  225. 'actor_id' => $actor_id,
  226. 'url' => Router::url($route = 'actor_circles_view_by_nickname', ['nickname' => $user->getNickname()]),
  227. 'route' => $route,
  228. 'title' => _m('Circles'),
  229. 'ordering' => $ordering++,
  230. ]));
  231. return Event::next;
  232. }
  233. }