PinnedNotes.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\DB;
  31. use App\Core\Event;
  32. use App\Core\Modules\Plugin;
  33. use App\Core\Router\RouteLoader;
  34. use App\Core\Router\Router;
  35. use App\Entity\Actor;
  36. use App\Entity\LocalUser;
  37. use App\Entity\Note;
  38. use App\Util\Common;
  39. use App\Util\Formatting;
  40. use App\Util\Nickname;
  41. use Component\Collection\Collection;
  42. use Doctrine\Common\Collections\ExpressionBuilder;
  43. use Doctrine\ORM\Query\Expr;
  44. use Doctrine\ORM\QueryBuilder;
  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(RouteLoader $r): bool
  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, &$res): bool
  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', ['pinnednotes' => $notes['notes']]);
  81. return Event::next;
  82. }
  83. public function onCollectionQueryAddJoins(QueryBuilder &$note_qb, QueryBuilder &$actor_qb): bool
  84. {
  85. $note_qb->leftJoin(E\PinnedNotes::class, 'pinned', Expr\Join::WITH, 'note.id = pinned.note_id');
  86. return Event::next;
  87. }
  88. public function onCollectionQueryCreateExpression(ExpressionBuilder $eb, string $term, ?string $language, ?Actor $actor, &$note_expr, &$actor_expr): bool
  89. {
  90. if ($term === 'pinned:true') {
  91. $note_expr = $eb->neq('pinned', null);
  92. return Event::stop;
  93. }
  94. if (str_starts_with($term, 'actor:')) {
  95. $actor_id = (int) mb_substr($term, 6);
  96. $actor_expr = $eb->eq('actor.id', $actor_id);
  97. return Event::stop;
  98. }
  99. return Event::next;
  100. }
  101. public function onAddNoteActions(Request $request, Note $note, array &$actions): bool
  102. {
  103. $user = Common::user();
  104. if ($user->getId() !== $note->getActorId()) {
  105. return Event::next;
  106. }
  107. $opts = ['note_id' => $note->getId(), 'actor_id' => $user->getId()];
  108. $is_pinned = !\is_null(DB::findOneBy(E\PinnedNotes::class, $opts, return_null: true));
  109. $router_args = ['id' => $note->getId()];
  110. $router_type = Router::ABSOLUTE_PATH;
  111. $action_url = Router::url('toggle_note_pin', $router_args, $router_type);
  112. $action_url .= '?from=' . urlencode($request->getRequestUri()); // so we can go back
  113. $actions[] = [
  114. 'url' => $action_url,
  115. 'title' => ($is_pinned ? 'Unpin' : 'Pin') . ' this note',
  116. 'classes' => 'button-container pin-button-container ' . ($is_pinned ? 'pinned' : ''),
  117. 'id' => 'pin-button-container-' . $note->getId(),
  118. ];
  119. return Event::next;
  120. }
  121. public function onEndShowStyles(array &$styles, string $route): bool
  122. {
  123. $styles[] = 'plugins/PinnedNotes/assets/css/pinned-notes.css';
  124. return Event::next;
  125. }
  126. // Activity Pub handling stuff
  127. public function onActivityPubAddActivityStreamsTwoData(string $type_name, &$type): bool
  128. {
  129. if ($type_name === 'Note') {
  130. $actor = \Plugin\ActivityPub\ActivityPub::getActorByUri($type->get('attributedTo'));
  131. // Note uri is `https://domain:port/object/note/3`.
  132. // is it always like that? I honestly don't know, but I see
  133. // no other way of getting Note id:
  134. $uri_parts = explode('/', $type->get('id'));
  135. $note_id = end($uri_parts);
  136. $is_pinned = !\is_null(DB::findOneBy(E\PinnedNotes::class, ['actor_id' => $actor->getId(), 'note_id' => $note_id], return_null: true));
  137. $type->set('featured', $is_pinned);
  138. } elseif ($type_name === 'Person') {
  139. $actor = \Plugin\ActivityPub\ActivityPub::getActorByUri($type->get('id'));
  140. $router_args = ['id' => $actor->getId()];
  141. $router_type = Router::ABSOLUTE_URL;
  142. $action_url = Router::url('list_pinned_notes_by_id', $router_args, $router_type);
  143. $type->set('featured', $action_url);
  144. }
  145. return Event::next;
  146. }
  147. }