PinnedNotes.php 4.0 KB

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