Circles.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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;
  22. use App\Core\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. /**
  28. * @extends MetaCollectionController<Circles>
  29. */
  30. class Circles extends MetaCollectionController
  31. {
  32. protected const SLUG = 'circle';
  33. protected const PLURAL_SLUG = 'circles';
  34. protected string $page_title = 'Actor circles';
  35. public function createCollection(int $owner_id, string $name): bool
  36. {
  37. return !\is_null(\Component\Circle\Circle::createCircle($owner_id, $name));
  38. }
  39. public function getCollectionUrl(int $owner_id, ?string $owner_nickname, int $collection_id): string
  40. {
  41. return Router::url(
  42. 'actor_circle_view_by_circle_id',
  43. ['circle_id' => $collection_id],
  44. );
  45. }
  46. /**
  47. * @return Circles[]
  48. */
  49. public function getCollectionItems(int $owner_id, int $collection_id): array
  50. {
  51. return []; // TODO
  52. }
  53. /**
  54. * @return Circles[]
  55. */
  56. public function feedByCircleId(int $circle_id)
  57. {
  58. // Owner id isn't used
  59. return $this->getCollectionItems(0, $circle_id);
  60. }
  61. /**
  62. * @return Circles[]
  63. */
  64. public function feedByTaggerIdAndTag(int $tagger_id, string $tag)
  65. {
  66. // Owner id isn't used
  67. $circle_id = ActorCircle::getByPK(['tagger' => $tagger_id, 'tag' => $tag])->getId();
  68. return $this->getCollectionItems($tagger_id, $circle_id);
  69. }
  70. /**
  71. * @return Circles[]
  72. */
  73. public function feedByTaggerNicknameAndTag(string $tagger_nickname, string $tag)
  74. {
  75. $tagger_id = LocalUser::getByNickname($tagger_nickname)->getId();
  76. $circle_id = ActorCircle::getByPK(['tagger' => $tagger_id, 'tag' => $tag])->getId();
  77. return $this->getCollectionItems($tagger_id, $circle_id);
  78. }
  79. public function getCollectionsByActorId(int $owner_id): array
  80. {
  81. return DB::findBy(ActorCircle::class, ['tagger' => $owner_id], order_by: ['id' => 'desc']);
  82. }
  83. public function getCollectionBy(int $owner_id, int $collection_id): self
  84. {
  85. return DB::findOneBy(ActorCircle::class, ['id' => $collection_id, 'actor_id' => $owner_id]);
  86. }
  87. public function setCollectionName(int $actor_id, string $actor_nickname, ActorCircle $collection, string $name): void
  88. {
  89. foreach ($collection->getActorTags(db_reference: true) as $at) {
  90. $at->setTag($name);
  91. }
  92. $collection->setTag($name);
  93. Cache::delete(Actor::cacheKeys($actor_id)['circles']);
  94. }
  95. public function removeCollection(int $actor_id, string $actor_nickname, ActorCircle $collection): void
  96. {
  97. foreach ($collection->getActorTags(db_reference: true) as $at) {
  98. DB::remove($at);
  99. }
  100. DB::remove($collection);
  101. Cache::delete(Actor::cacheKeys($actor_id)['circles']);
  102. }
  103. }