Favourite.php 9.2 KB

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