Circles.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\Controller;
  20. use App\Core\Cache;
  21. use App\Core\DB\DB;
  22. use App\Core\Router\Router;
  23. use App\Entity\Actor;
  24. use App\Entity\LocalUser;
  25. use Component\Circle\Entity\ActorCircle;
  26. use Component\Collection\Util\Controller\MetaCollectionController;
  27. class Circles extends MetaCollectionController
  28. {
  29. protected string $slug = 'circle';
  30. protected string $plural_slug = 'circles';
  31. protected string $page_title = 'Actor circles';
  32. public function createCollection(int $owner_id, string $name)
  33. {
  34. return \Component\Circle\Circle::createCircle($owner_id, $name);
  35. }
  36. public function getCollectionUrl(int $owner_id, ?string $owner_nickname, int $collection_id): string
  37. {
  38. return Router::url(
  39. 'actor_circle_view_by_circle_id',
  40. ['circle_id' => $collection_id],
  41. );
  42. }
  43. public function getCollectionItems(int $owner_id, $collection_id): array
  44. {
  45. $notes = []; // TODO: Use Feed::query
  46. return [
  47. '_template' => 'collection/notes.html.twig',
  48. 'notes' => $notes,
  49. ];
  50. }
  51. public function feedByCircleId(int $circle_id)
  52. {
  53. // Owner id isn't used
  54. return $this->getCollectionItems(0, $circle_id);
  55. }
  56. public function feedByTaggerIdAndTag(int $tagger_id, string $tag)
  57. {
  58. // Owner id isn't used
  59. $circle_id = ActorCircle::getByPK(['tagger' => $tagger_id, 'tag' => $tag])->getId();
  60. return $this->getCollectionItems($tagger_id, $circle_id);
  61. }
  62. public function feedByTaggerNicknameAndTag(string $tagger_nickname, string $tag)
  63. {
  64. $tagger_id = LocalUser::getByNickname($tagger_nickname)->getId();
  65. $circle_id = ActorCircle::getByPK(['tagger' => $tagger_id, 'tag' => $tag])->getId();
  66. return $this->getCollectionItems($tagger_id, $circle_id);
  67. }
  68. public function getCollectionsByActorId(int $owner_id): array
  69. {
  70. return DB::findBy(ActorCircle::class, ['tagger' => $owner_id], order_by: ['id' => 'desc']);
  71. }
  72. public function getCollectionBy(int $owner_id, int $collection_id): ActorCircle
  73. {
  74. return DB::findOneBy(ActorCircle::class, ['id' => $collection_id, 'actor_id' => $owner_id]);
  75. }
  76. public function setCollectionName(int $actor_id, string $actor_nickname, ActorCircle $collection, string $name)
  77. {
  78. foreach ($collection->getActorTags(db_reference: true) as $at) {
  79. $at->setTag($name);
  80. }
  81. $collection->setTag($name);
  82. Cache::delete(Actor::cacheKeys($actor_id)['circles']);
  83. }
  84. public function removeCollection(int $actor_id, string $actor_nickname, ActorCircle $collection)
  85. {
  86. foreach ($collection->getActorTags(db_reference: true) as $at) {
  87. DB::remove($at);
  88. }
  89. DB::remove($collection);
  90. Cache::delete(Actor::cacheKeys($actor_id)['circles']);
  91. }
  92. }