PinnedNotes.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. * WebMonetization 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\PinnedNotes;
  30. use App\Core\DB;
  31. use App\Core\Event;
  32. use App\Core\Modules\Plugin;
  33. use App\Core\Router;
  34. use App\Entity\Actor;
  35. use App\Entity\LocalUser;
  36. use App\Entity\Note;
  37. use App\Util\Common;
  38. use App\Util\Formatting;
  39. use App\Util\Nickname;
  40. use Component\Collection\Collection;
  41. use Doctrine\Common\Collections\ExpressionBuilder;
  42. use Doctrine\ORM\Query\Expr;
  43. use Doctrine\ORM\QueryBuilder;
  44. use EventResult;
  45. use Plugin\PinnedNotes\Controller as C;
  46. use Plugin\PinnedNotes\Entity as E;
  47. use Symfony\Component\HttpFoundation\Request;
  48. class PinnedNotes extends Plugin
  49. {
  50. public function onAddRoute(Router $r): EventResult
  51. {
  52. // Pin and unpin notes
  53. $r->connect(id: 'toggle_note_pin', uri_path: '/object/note/{id<\d+>}/pin', target: [C\PinnedNotes::class, 'togglePin']);
  54. // list of user pins, by id and nickname
  55. // it's meant to be used by the ActivityPub plugin
  56. $r->connect(
  57. id: 'list_pinned_notes_by_id',
  58. uri_path: '/actor/{id<\d+>}/pinned_notes',
  59. target: [C\PinnedNotes::class, 'listPinsById'],
  60. );
  61. $r->connect(
  62. id: 'list_pinned_notes_by_nickname',
  63. uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/pinned_notes',
  64. target: [C\PinnedNotes::class, 'listPinsByNickname'],
  65. );
  66. return Event::next;
  67. }
  68. public function onBeforeFeed(Request $request, array &$res): EventResult
  69. {
  70. $path = $request->attributes->get('_route');
  71. if ($path === 'actor_view_nickname') {
  72. $actor = LocalUser::getByNickname($request->attributes->get('nickname'))->getActor();
  73. } elseif ($path === 'actor_view_id') {
  74. $actor = DB::findOneBy(Actor::class, ['id' => $request->attributes->get('id')]);
  75. } else {
  76. return Event::next;
  77. }
  78. $locale = Common::currentLanguage()->getLocale();
  79. $notes = Collection::query('pinned:true actor:' . $actor->getId(), 1, $locale, $actor);
  80. $res[] = Formatting::twigRenderFile('PinnedNotes/notes.html.twig', ['pinned_notes' => $notes['notes']]);
  81. return Event::next;
  82. }
  83. public function onCollectionQueryAddJoins(QueryBuilder &$note_qb, QueryBuilder &$actor_qb): EventResult
  84. {
  85. if (!\in_array('pinned_notes', $note_qb->getAllAliases())) {
  86. $note_qb->leftJoin(E\PinnedNotes::class, 'pinned_notes', Expr\Join::WITH, 'note.id = pinned_notes.note_id');
  87. }
  88. return Event::next;
  89. }
  90. public function onCollectionQueryCreateExpression(ExpressionBuilder $eb, string $term, ?string $language, ?Actor $actor, &$note_expr, &$actor_expr): EventResult
  91. {
  92. if ($term === 'pinned:true') {
  93. $note_expr = $eb->neq('pinned_notes', null);
  94. return Event::stop;
  95. }
  96. if (str_starts_with($term, 'actor:')) {
  97. $actor_id = (int) mb_substr($term, 6);
  98. $actor_expr = $eb->eq('actor.id', $actor_id);
  99. return Event::stop;
  100. }
  101. return Event::next;
  102. }
  103. public function onAddNoteActions(Request $request, Note $note, array &$actions): EventResult
  104. {
  105. $user = Common::user();
  106. if ($user->getId() !== $note->getActorId()) {
  107. return Event::next;
  108. }
  109. $opts = ['note_id' => $note->getId(), 'actor_id' => $user->getId()];
  110. $is_pinned = !\is_null(DB::findOneBy(E\PinnedNotes::class, $opts, return_null: true));
  111. $router_args = ['id' => $note->getId()];
  112. $router_type = Router::ABSOLUTE_PATH;
  113. $action_url = Router::url('toggle_note_pin', $router_args, $router_type);
  114. $action_url .= '?from=' . urlencode($request->getRequestUri()); // so we can go back
  115. $actions[] = [
  116. 'url' => $action_url,
  117. 'title' => ($is_pinned ? 'Unpin' : 'Pin') . ' this note',
  118. 'classes' => 'button-container pin-button-container ' . ($is_pinned ? 'pinned' : ''),
  119. 'id' => 'pin-button-container-' . $note->getId(),
  120. ];
  121. return Event::next;
  122. }
  123. public function onEndShowStyles(array &$styles, string $route): EventResult
  124. {
  125. $styles[] = 'plugins/PinnedNotes/assets/css/pinned-notes.css';
  126. return Event::next;
  127. }
  128. // Activity Pub handling stuff
  129. public function onActivityStreamsTwoContext(array &$activity_streams_two_context): EventResult
  130. {
  131. $activity_streams_two_context[] = ['toot' => 'http://joinmastodon.org/ns#'];
  132. $activity_streams_two_context[] = [
  133. 'featured' => [
  134. '@id' => 'toot:featured',
  135. '@type' => '@id',
  136. ],
  137. ];
  138. return Event::next;
  139. }
  140. public function onActivityPubAddActivityStreamsTwoData(string $type_name, object &$type): EventResult
  141. {
  142. if ($type_name === 'Person') {
  143. $actor = \Plugin\ActivityPub\Util\Explorer::getOneFromUri($type->get('id'));
  144. $router_args = ['id' => $actor->getId()];
  145. $router_type = Router::ABSOLUTE_URL;
  146. $action_url = Router::url('list_pinned_notes_by_id', $router_args, $router_type);
  147. $type->set('featured', $action_url);
  148. }
  149. return Event::next;
  150. }
  151. }