Circle.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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\DB;
  22. use App\Core\Event;
  23. use function App\Core\I18n\_m;
  24. use App\Core\Modules\Component;
  25. use App\Core\Router\RouteLoader;
  26. use App\Core\Router\Router;
  27. use App\Entity\Actor;
  28. use App\Entity\Feed;
  29. use App\Entity\LocalUser;
  30. use App\Util\Common;
  31. use App\Util\Nickname;
  32. use Component\Circle\Controller as CircleController;
  33. use Component\Circle\Entity\ActorCircle;
  34. use Component\Circle\Entity\ActorCircleSubscription;
  35. use Component\Circle\Entity\ActorTag;
  36. use Component\Collection\Util\MetaCollectionTrait;
  37. use Component\Tag\Tag;
  38. use Functional as F;
  39. use Symfony\Component\HttpFoundation\Request;
  40. /**
  41. * Component responsible for handling and representing ActorCircles and ActorTags
  42. *
  43. * @author Hugo Sales <hugo@hsal.es>
  44. * @author Phablulo <phablulo@gmail.com>
  45. * @author Diogo Peralta Cordeiro <@diogo.site>
  46. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  47. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  48. */
  49. class Circle extends Component
  50. {
  51. use MetaCollectionTrait;
  52. public const TAG_CIRCLE_REGEX = '/' . Nickname::BEFORE_MENTIONS . '@#([\pL\pN_\-\.]{1,64})/';
  53. protected string $slug = 'circle';
  54. protected string $plural_slug = 'circles';
  55. public function onAddRoute(RouteLoader $r): bool
  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): bool
  86. {
  87. if ($section === 'profile' && $request->get('_route') === 'settings') {
  88. $tabs[] = [
  89. 'title' => 'Self tags',
  90. 'desc' => 'Add or remove tags on yourself',
  91. 'id' => 'settings-self-tags',
  92. 'controller' => CircleController\SelfTagsSettings::settingsSelfTags($request, Common::actor(), 'settings-self-tags-details'),
  93. ];
  94. }
  95. return Event::next;
  96. }
  97. public function onPostingFillTargetChoices(Request $request, Actor $actor, array &$targets): bool
  98. {
  99. $circles = $actor->getCircles();
  100. foreach ($circles as $circle) {
  101. $tag = $circle->getTag();
  102. $targets["#{$tag}"] = $tag;
  103. }
  104. return Event::next;
  105. }
  106. // Meta Collection -------------------------------------------------------------------
  107. private function getActorIdFromVars(array $vars): int
  108. {
  109. $id = $vars['request']->get('id', null);
  110. if ($id) {
  111. return (int) $id;
  112. }
  113. $nick = $vars['request']->get('nickname');
  114. $user = LocalUser::getByNickname($nick);
  115. return $user->getId();
  116. }
  117. public static function createCircle(Actor|int $tagger_id, string $tag): int
  118. {
  119. $tagger_id = \is_int($tagger_id) ? $tagger_id : $tagger_id->getId();
  120. $circle = ActorCircle::create([
  121. 'tagger' => $tagger_id,
  122. 'tag' => $tag,
  123. 'description' => null, // TODO
  124. 'private' => false, // TODO
  125. ]);
  126. DB::persist($circle);
  127. Cache::delete(Actor::cacheKeys($tagger_id)['circles']);
  128. return $circle->getId();
  129. }
  130. protected function createCollection(Actor $owner, array $vars, string $name)
  131. {
  132. $this->createCircle($owner, $name);
  133. DB::persist(ActorTag::create([
  134. 'tagger' => $owner->getId(),
  135. 'tagged' => self::getActorIdFromVars($vars),
  136. 'tag' => $name,
  137. ]));
  138. }
  139. protected function removeItem(Actor $owner, array $vars, $items, array $collections)
  140. {
  141. $tagger_id = $owner->getId();
  142. $tagged_id = $this->getActorIdFromVars($vars);
  143. $circles_to_remove_tagged_from = DB::findBy(ActorCircle::class, ['id' => $items]);
  144. foreach ($circles_to_remove_tagged_from as $circle) {
  145. DB::removeBy(ActorCircleSubscription::class, ['actor_id' => $tagged_id, 'circle_id' => $circle->getId()]);
  146. }
  147. $tags = F\map($circles_to_remove_tagged_from, fn ($x) => $x->getTag());
  148. foreach ($tags as $tag) {
  149. DB::removeBy(ActorTag::class, ['tagger' => $tagger_id, 'tagged' => $tagged_id, 'tag' => $tag]);
  150. }
  151. Cache::delete(Actor::cacheKeys($tagger_id)['circles']);
  152. }
  153. protected function addItem(Actor $owner, array $vars, $items, array $collections)
  154. {
  155. $tagger_id = $owner->getId();
  156. $tagged_id = $this->getActorIdFromVars($vars);
  157. $circles_to_add_tagged_to = DB::findBy(ActorCircle::class, ['id' => $items]);
  158. foreach ($circles_to_add_tagged_to as $circle) {
  159. DB::persist(ActorCircleSubscription::create(['actor_id' => $tagged_id, 'circle_id' => $circle->getId()]));
  160. }
  161. $tags = F\map($circles_to_add_tagged_to, fn ($x) => $x->getTag());
  162. foreach ($tags as $tag) {
  163. DB::persist(ActorTag::create(['tagger' => $tagger_id, 'tagged' => $tagged_id, 'tag' => $tag]));
  164. }
  165. Cache::delete(Actor::cacheKeys($tagger_id)['circles']);
  166. }
  167. /**
  168. * @see MetaCollectionPlugin->shouldAddToRightPanel
  169. */
  170. protected function shouldAddToRightPanel(Actor $user, $vars, Request $request): bool
  171. {
  172. return \in_array($vars['path'], ['actor_view_nickname', 'actor_view_id']);
  173. }
  174. protected function getCollectionsBy(Actor $owner, ?array $vars = null, bool $ids_only = false): array
  175. {
  176. $tagged_id = !\is_null($vars) ? $this->getActorIdFromVars($vars) : null;
  177. $circles = \is_null($tagged_id) ? $owner->getCircles() : F\select($owner->getCircles(), function ($x) use ($tagged_id) {
  178. foreach ($x->getActorTags() as $at) {
  179. if ($at->getTagged() === $tagged_id) {
  180. return true;
  181. }
  182. }
  183. return false;
  184. });
  185. return $ids_only ? array_map(fn ($x) => $x->getId(), $circles) : $circles;
  186. }
  187. public function onCreateDefaultFeeds(int $actor_id, LocalUser $user, int &$ordering)
  188. {
  189. DB::persist(Feed::create([
  190. 'actor_id' => $actor_id,
  191. 'url' => Router::url($route = 'actor_circles_view_by_nickname', ['nickname' => $user->getNickname()]),
  192. 'route' => $route,
  193. 'title' => _m('Circles'),
  194. 'ordering' => $ordering++,
  195. ]));
  196. return Event::next;
  197. }
  198. }