Favourite.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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\Favourite\Controller;
  20. use App\Core\DB\DB;
  21. use App\Core\Form;
  22. use function App\Core\I18n\_m;
  23. use App\Core\Log;
  24. use App\Core\Router\Router;
  25. use App\Util\Common;
  26. use App\Util\Exception\ClientException;
  27. use App\Util\Exception\InvalidFormException;
  28. use App\Util\Exception\NoLoggedInUser;
  29. use App\Util\Exception\NoSuchNoteException;
  30. use App\Util\Exception\RedirectException;
  31. use App\Util\Exception\ServerException;
  32. use Component\Collection\Util\Controller\FeedController;
  33. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  34. use Symfony\Component\HttpFoundation\Request;
  35. class Favourite extends FeedController
  36. {
  37. /**
  38. * @throws InvalidFormException
  39. * @throws NoLoggedInUser
  40. * @throws NoSuchNoteException
  41. * @throws RedirectException
  42. * @throws ServerException
  43. */
  44. public function favouriteAddNote(Request $request, int $id): bool|array
  45. {
  46. $user = Common::ensureLoggedIn();
  47. $actor_id = $user->getId();
  48. $opts = ['id' => $id];
  49. $add_favourite_note = DB::find('note', $opts);
  50. if (\is_null($add_favourite_note)) {
  51. throw new NoSuchNoteException();
  52. }
  53. $form_add_to_favourite = Form::create([
  54. ['add_favourite', SubmitType::class,
  55. [
  56. 'label' => _m('Favourite note!'),
  57. 'attr' => [
  58. 'title' => _m('Favourite this note!'),
  59. ],
  60. ],
  61. ],
  62. ]);
  63. $form_add_to_favourite->handleRequest($request);
  64. if ($form_add_to_favourite->isSubmitted()) {
  65. if (!\is_null(\Plugin\Favourite\Favourite::favourNote(note_id: $id, actor_id: $actor_id))) {
  66. DB::flush();
  67. } else {
  68. throw new ClientException(_m('Note already favoured!'));
  69. }
  70. // Redirect user to where they came from
  71. // Prevent open redirect
  72. if (!\is_null($from = $this->string('from'))) {
  73. if (Router::isAbsolute($from)) {
  74. Log::warning("Actor {$actor_id} attempted to favourite a note and then get redirected to another host, or the URL was invalid ({$from})");
  75. throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive)
  76. } else {
  77. // TODO anchor on element id
  78. throw new RedirectException(url: $from);
  79. }
  80. } else {
  81. // If we don't have a URL to return to, go to the instance root
  82. throw new RedirectException('root');
  83. }
  84. }
  85. return [
  86. '_template' => 'favourite/add_to_favourites.html.twig',
  87. 'note' => $add_favourite_note,
  88. 'add_favourite' => $form_add_to_favourite->createView(),
  89. ];
  90. }
  91. /**
  92. * @throws InvalidFormException
  93. * @throws NoLoggedInUser
  94. * @throws NoSuchNoteException
  95. * @throws RedirectException
  96. * @throws ServerException
  97. */
  98. public function favouriteRemoveNote(Request $request, int $id): array
  99. {
  100. $user = Common::ensureLoggedIn();
  101. $actor_id = $user->getId();
  102. $opts = ['id' => $id];
  103. $remove_favourite_note = DB::find('note', $opts);
  104. if (\is_null($remove_favourite_note)) {
  105. throw new NoSuchNoteException();
  106. }
  107. $form_remove_favourite = Form::create([
  108. ['remove_favourite', SubmitType::class,
  109. [
  110. 'label' => _m('Remove favourite'),
  111. 'attr' => [
  112. 'title' => _m('Remove note from favourites.'),
  113. ],
  114. ],
  115. ],
  116. ]);
  117. $form_remove_favourite->handleRequest($request);
  118. if ($form_remove_favourite->isSubmitted()) {
  119. if (!\is_null(\Plugin\Favourite\Favourite::unfavourNote(note_id: $id, actor_id: $actor_id))) {
  120. DB::flush();
  121. } else {
  122. throw new ClientException(_m('Note already unfavoured!'));
  123. }
  124. // Redirect user to where they came from
  125. // Prevent open redirect
  126. if (!\is_null($from = $this->string('from'))) {
  127. if (Router::isAbsolute($from)) {
  128. Log::warning("Actor {$actor_id} attempted to reply to a note and then get redirected to another host, or the URL was invalid ({$from})");
  129. throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive)
  130. } else {
  131. // TODO anchor on element id
  132. throw new RedirectException(url: $from);
  133. }
  134. } else {
  135. // If we don't have a URL to return to, go to the instance root
  136. throw new RedirectException('root');
  137. }
  138. }
  139. $note = DB::find('note', ['id' => $id]);
  140. return [
  141. '_template' => 'favourite/remove_from_favourites.html.twig',
  142. 'note' => $note,
  143. 'remove_favourite' => $form_remove_favourite->createView(),
  144. ];
  145. }
  146. public function favouritesViewByActorId(Request $request, int $id)
  147. {
  148. $notes = DB::dql(
  149. <<< 'EOF'
  150. select n from note n
  151. join note_favourite f with n.id = f.note_id
  152. where f.actor_id = :id
  153. order by f.created DESC
  154. EOF,
  155. ['id' => $id],
  156. );
  157. return [
  158. '_template' => 'collection/notes.html.twig',
  159. 'page_title' => 'Favourites feed.',
  160. 'notes' => $notes,
  161. ];
  162. }
  163. public function favouritesViewByActorNickname(Request $request, string $nickname)
  164. {
  165. $user = DB::findOneBy('local_user', ['nickname' => $nickname]);
  166. return self::favouritesViewByActorId($request, $user->getId());
  167. }
  168. /**
  169. * Reverse favourites stream
  170. *
  171. * @throws NoLoggedInUser user not logged in
  172. *
  173. * @return array template
  174. */
  175. public function reverseFavouritesViewByActorId(Request $request, int $id): array
  176. {
  177. $notes = DB::dql(
  178. <<< 'EOF'
  179. select n from note n
  180. join note_favourite f with n.id = f.note_id
  181. where f.actor_id != :id
  182. and n.actor_id = :id
  183. order by f.created DESC
  184. EOF,
  185. ['id' => $id],
  186. );
  187. return [
  188. '_template' => 'collection/notes.html.twig',
  189. 'page_title' => 'Reverse favourites feed.',
  190. 'notes' => $notes,
  191. ];
  192. }
  193. public function reverseFavouritesViewByActorNickname(Request $request, string $nickname)
  194. {
  195. $user = DB::findOneBy('local_user', ['nickname' => $nickname]);
  196. return self::reverseFavouritesViewByActorId($request, $user->getId());
  197. }
  198. }