Favourite.php 8.4 KB

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