AttachmentCollections.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. /**
  20. * Attachments Albums for GNU social
  21. *
  22. * @package GNUsocial
  23. * @category Plugin
  24. *
  25. * @author Phablulo <phablulo@gmail.com>
  26. * @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
  27. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  28. */
  29. namespace Plugin\AttachmentCollections;
  30. use App\Core\DB\DB;
  31. use App\Core\Event;
  32. use function App\Core\I18n\_m;
  33. use App\Core\Modules\Plugin;
  34. use App\Core\Router\RouteLoader;
  35. use App\Core\Router\Router;
  36. use App\Entity\Actor;
  37. use App\Entity\Feed;
  38. use App\Entity\LocalUser;
  39. use App\Util\Nickname;
  40. use Component\Collection\Util\MetaCollectionTrait;
  41. use Plugin\AttachmentCollections\Controller\AttachmentCollections as AttachmentCollectionsController;
  42. use Plugin\AttachmentCollections\Entity\AttachmentCollection;
  43. use Plugin\AttachmentCollections\Entity\AttachmentCollectionEntry;
  44. use Symfony\Component\HttpFoundation\Request;
  45. class AttachmentCollections extends Plugin
  46. {
  47. use MetaCollectionTrait;
  48. protected string $slug = 'collection';
  49. protected string $plural_slug = 'collections';
  50. protected function createCollection(Actor $owner, array $vars, string $name)
  51. {
  52. $col = AttachmentCollection::create([
  53. 'name' => $name,
  54. 'actor_id' => $owner->getId(),
  55. ]);
  56. DB::persist($col);
  57. DB::persist(AttachmentCollectionEntry::create([
  58. 'attachment_id' => $vars['vars']['attachment_id'],
  59. 'note_id' => $vars['vars']['note_id'],
  60. 'collection_id' => $col->getId(),
  61. ]));
  62. }
  63. protected function removeItem(Actor $owner, array $vars, array $items, array $collections)
  64. {
  65. return DB::dql(<<<'EOF'
  66. DELETE FROM \Plugin\AttachmentCollections\Entity\AttachmentCollectionEntry AS entry
  67. WHERE entry.attachment_id = :attach_id AND entry.note_id = :note_id
  68. AND entry.collection_id IN (
  69. SELECT album.id FROM \Plugin\AttachmentCollections\Entity\AttachmentCollection AS album
  70. WHERE album.actor_id = :user_id
  71. AND album.id IN (:ids)
  72. )
  73. EOF, [
  74. 'attach_id' => $vars['vars']['attachment_id'],
  75. 'note_id' => $vars['vars']['note_id'],
  76. 'user_id' => $owner->getId(),
  77. 'ids' => $items,
  78. ]);
  79. }
  80. protected function addItem(Actor $owner, array $vars, array $items, array $collections)
  81. {
  82. foreach ($items as $id) {
  83. // prevent user from putting something in a collection (s)he doesn't own:
  84. if (\in_array($id, $collections)) {
  85. DB::persist(AttachmentCollectionEntry::create([
  86. 'attachment_id' => $vars['vars']['attachment_id'],
  87. 'note_id' => $vars['vars']['note_id'],
  88. 'collection_id' => $id,
  89. ]));
  90. }
  91. }
  92. }
  93. protected function shouldAddToRightPanel(Actor $user, $vars, Request $request): bool
  94. {
  95. return $vars['path'] === 'note_attachment_show';
  96. }
  97. protected function getCollectionsBy(Actor $owner, ?array $vars = null, bool $ids_only = false): array
  98. {
  99. if (\is_null($vars)) {
  100. $res = DB::findBy(AttachmentCollection::class, ['actor_id' => $owner->getId()]);
  101. } else {
  102. $res = DB::dql(
  103. <<<'EOF'
  104. SELECT entry.collection_id FROM \Plugin\AttachmentCollections\Entity\AttachmentCollectionEntry AS entry
  105. INNER JOIN \Plugin\AttachmentCollections\Entity\AttachmentCollection AS attachment_collection
  106. WITH attachment_collection.id = entry.collection_id
  107. WHERE entry.attachment_id = :attach_id AND entry.note_id = :note_id AND attachment_collection.actor_id = :id
  108. EOF,
  109. [
  110. 'id' => $owner->getId(),
  111. 'note_id' => $vars['vars']['note_id'],
  112. 'attach_id' => $vars['vars']['attachment_id'],
  113. ],
  114. );
  115. }
  116. if (!$ids_only) {
  117. return $res;
  118. }
  119. return array_map(fn ($x) => $x['collection_id'], $res);
  120. }
  121. public function onAddRoute(RouteLoader $r): bool
  122. {
  123. // View all collections by actor id and nickname
  124. $r->connect(
  125. id: 'collections_view_by_actor_id',
  126. uri_path: '/actor/{id<\d+>}/collections',
  127. target: [AttachmentCollectionsController::class, 'collectionsViewByActorId'],
  128. );
  129. $r->connect(
  130. id: 'collections_view_by_nickname',
  131. uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/collections',
  132. target: [AttachmentCollectionsController::class, 'collectionsViewByActorNickname'],
  133. );
  134. // View notes from a collection by actor id and nickname
  135. $r->connect(
  136. id: 'collection_notes_view_by_actor_id',
  137. uri_path: '/actor/{id<\d+>}/collections/{cid<\d+>}',
  138. target: [AttachmentCollectionsController::class, 'collectionsEntryViewNotesByActorId'],
  139. );
  140. $r->connect(
  141. id: 'collection_notes_view_by_nickname',
  142. uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/collections/{cid<\d+>}',
  143. target: [AttachmentCollectionsController::class, 'collectionsEntryViewNotesByNickname'],
  144. );
  145. return Event::next;
  146. }
  147. public function onCreateDefaultFeeds(int $actor_id, LocalUser $user, int &$ordering)
  148. {
  149. DB::persist(Feed::create([
  150. 'actor_id' => $actor_id,
  151. 'url' => Router::url($route = 'collections_view_by_nickname', ['nickname' => $user->getNickname()]),
  152. 'route' => $route,
  153. 'title' => _m('Attachment Collections'),
  154. 'ordering' => $ordering++,
  155. ]));
  156. return Event::next;
  157. }
  158. }