PinnedNotes.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 Plugin\PinnedNotes\Controller;
  20. use App\Core\DB;
  21. use App\Core\Form;
  22. use function App\Core\I18n\_m;
  23. use App\Core\Router;
  24. use App\Entity\Actor;
  25. use App\Entity\LocalUser;
  26. use App\Entity\Note;
  27. use App\Util\Common;
  28. use App\Util\Exception\ClientException;
  29. use App\Util\Exception\NoSuchNoteException;
  30. use App\Util\Exception\RedirectException;
  31. use Component\Collection\Collection;
  32. use Component\Collection\Util\Controller\FeedController;
  33. use Plugin\PinnedNotes\Entity as E;
  34. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  35. use Symfony\Component\HttpFoundation\Request;
  36. /**
  37. * @extends FeedController<Note>
  38. */
  39. class PinnedNotes extends FeedController
  40. {
  41. public function listPinsByNickname(Request $request, string $nickname)
  42. {
  43. $actor = LocalUser::getByNickname($request->attributes->get('nickname'))->getActor();
  44. return $this->listPins($request, $actor->getId());
  45. }
  46. public function listPinsById(Request $request, int $id)
  47. {
  48. return $this->listPins($request, $id);
  49. }
  50. public function listPins(Request $request, int $id)
  51. {
  52. $locale = Common::currentLanguage()->getLocale();
  53. $actor = DB::findOneBy(Actor::class, ['id' => $id]);
  54. $page = (int) ($request->query->get('page') ?? 1);
  55. return Collection::query('pinned:true actor:' . $id, $page, $locale, $actor);
  56. }
  57. public function togglePin(Request $request, int $id)
  58. {
  59. $user = Common::ensureLoggedIn();
  60. $note = DB::findOneBy('note', ['id' => $id]);
  61. if ($user->getId() !== $note?->getActorId()) {
  62. throw new NoSuchNoteException();
  63. }
  64. $opts = ['note_id' => $id, 'actor_id' => $user->getId()];
  65. $is_pinned = !\is_null(DB::findOneBy(E\PinnedNotes::class, $opts, return_null: true));
  66. $form = Form::create([
  67. ['toggle_pin', SubmitType::class, [
  68. 'label' => _m(($is_pinned ? 'Unpin' : 'Pin') . ' this note'),
  69. 'attr' => [
  70. 'title' => _m(($is_pinned ? 'Unpin' : 'Pin') . ' this note'),
  71. ],
  72. ]],
  73. ]);
  74. $form->handleRequest($request);
  75. if ($form->isSubmitted()) {
  76. $opts = ['note_id' => $id, 'actor_id' => $user->getId()];
  77. if ($is_pinned) {
  78. $pinned = DB::findOneBy(E\PinnedNotes::class, $opts);
  79. DB::remove($pinned);
  80. } else {
  81. DB::persist(E\PinnedNotes::create($opts));
  82. }
  83. DB::flush();
  84. // redirect user to where they came from, but prevent open redirect
  85. if (!\is_null($from = $this->string('from'))) {
  86. if (Router::isAbsolute($from)) {
  87. throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive)
  88. } else {
  89. throw new RedirectException(url: $from);
  90. }
  91. } else {
  92. // If we don't have a URL to return to, go to the instance root
  93. throw new RedirectException('root');
  94. }
  95. }
  96. return [
  97. '_template' => 'PinnedNotes/toggle.html.twig',
  98. 'note' => $note,
  99. 'title' => _m($is_pinned ? 'Unpin note' : 'Pin note'),
  100. 'toggle_form' => $form->createView(),
  101. ];
  102. }
  103. }