From 60b15ea79d2207be0a513fbf5f325fe63f5adb1e Mon Sep 17 00:00:00 2001 From: Eliseu Amaro Date: Thu, 21 Oct 2021 20:14:22 +0100 Subject: [PATCH] [Plugins][FAVOURITE] No longer a form, a link to a new page is provided instead. The amount of forms per page were blocking rendering for the majority of its duration. --- plugins/DeleteNote/DeleteNote.php | 5 +- plugins/Favourite/Controller/Favourite.php | 123 ++++++++++++++++-- plugins/Favourite/Favourite.php | 76 +++-------- .../favourite/add_to_favourites.html.twig | 25 ++++ .../remove_from_favourites.html.twig | 25 ++++ plugins/Repeat/Repeat.php | 5 +- plugins/Reply/Reply.php | 5 +- .../assets/default_theme/css/pages/feeds.css | 1 - templates/cards/note/view.html.twig | 2 +- templates/network/feed.html.twig | 1 - 10 files changed, 192 insertions(+), 76 deletions(-) create mode 100644 plugins/Favourite/templates/favourite/add_to_favourites.html.twig create mode 100644 plugins/Favourite/templates/favourite/remove_from_favourites.html.twig diff --git a/plugins/DeleteNote/DeleteNote.php b/plugins/DeleteNote/DeleteNote.php index 33167507f5..c2b94d7887 100644 --- a/plugins/DeleteNote/DeleteNote.php +++ b/plugins/DeleteNote/DeleteNote.php @@ -46,13 +46,14 @@ use Symfony\Component\HttpFoundation\Request; */ class DeleteNote extends NoteHandlerPlugin { + // TODO: Refactoring to link instead of a form /** * HTML rendering event that adds the repeat form as a note * action, if a user is logged in * * @throws RedirectException */ - public function onAddNoteActions(Request $request, Note $note, array &$actions) +/* public function onAddNoteActions(Request $request, Note $note, array &$actions) { if (($user = Common::user()) === null) { return Event::next; @@ -100,5 +101,5 @@ class DeleteNote extends NoteHandlerPlugin } $actions[] = $form_delete->createView(); return Event::next; - } + }*/ } diff --git a/plugins/Favourite/Controller/Favourite.php b/plugins/Favourite/Controller/Favourite.php index 9bbbff2baf..ffde83e3b6 100644 --- a/plugins/Favourite/Controller/Favourite.php +++ b/plugins/Favourite/Controller/Favourite.php @@ -1,6 +1,6 @@ $id]; + $add_favourite_note = DB::find('note', $opts); + if (!$user || $add_favourite_note === null) { + throw new NoSuchNoteException(); + } + + $form_add_to_favourite = Form::create([ + ['add_favourite', SubmitType::class, + [ + 'label' => _m('Favourite note!'), + 'attr' => [ + 'title' => _m('Favourite this note!') + ], + ], + ], + ]); + + $form_add_to_favourite->handleRequest($request); + + if ($form_add_to_favourite->isSubmitted()) { + $opts = ['note_id' => $id, 'actor_id' => $user->getId()]; + DB::persist(FavouriteEntity::create($opts)); + DB::flush(); + // TODO: proper redirect from where the user came from + throw new RedirectException(); + } + + return [ + '_template' => 'favourite/add_to_favourites.html.twig', + 'note' => $add_favourite_note, + 'add_favourite' => $form_add_to_favourite->createView(), + ]; + } + + /** + * @throws RedirectException + * @throws NoSuchNoteException + * @throws InvalidFormException + * @throws \App\Util\Exception\ServerException + * @throws NoLoggedInUser + */ + public function noteRemoveFavourite(Request $request, int $id): array + { + $user = Common::ensureLoggedIn(); + $opts = ['note_id' => $id, 'actor_id' => $user->getId()]; + $remove_favourite_note = DB::find('favourite', $opts); + if (!$user || $remove_favourite_note === null) { + throw new NoSuchNoteException(); + } + + $form_remove_favourite = Form::create([ + ['remove_favourite', SubmitType::class, + [ + 'label' => _m('Remove favourite'), + 'attr' => [ + 'title' => _m('Remove note from favourites.') + ], + ], + ], + ]); + + $form_remove_favourite->handleRequest($request); + if ($form_remove_favourite->isSubmitted()) { + DB::remove($remove_favourite_note); + DB::flush(); + // TODO: proper redirect from where the user came from + throw new RedirectException(); + } + + $note = DB::find('note', ['id' => $id]); + return [ + '_template' => 'favourite/remove_from_favourites.html.twig', + 'note' => $note, + 'remove_favourite' => $form_remove_favourite->createView(), + ]; + } + public function favouritesByActorId(Request $request, int $id) { $notes = DB::dql( @@ -43,11 +140,12 @@ class Favourite Event::handle('FormatNoteList', [$notes, &$notes_out]); return [ - '_template' => 'network/feed.html.twig', - 'notes' => $notes_out, + '_template' => 'network/feed.html.twig', + 'notes' => $notes_out, 'page_title' => 'Favourites timeline.', ]; } + public function favouritesByActorNickname(Request $request, string $nickname) { $user = DB::findOneBy('local_user', ['nickname' => $nickname]); @@ -57,18 +155,18 @@ class Favourite /** * Reverse favourites stream * - * @throws \App\Util\Exception\NoLoggedInUser user not logged in - * * @return array template + * @throws NoLoggedInUser user not logged in + * */ public function reverseFavouritesByActorId(Request $request, int $id): array { $notes = DB::dql( 'select n from App\Entity\Note n, Plugin\Favourite\Entity\Favourite f ' - . 'where n.id = f.note_id ' - . 'and f.actor_id != :id ' - . 'and n.actor_id = :id ' - . 'order by f.created DESC', + . 'where n.id = f.note_id ' + . 'and f.actor_id != :id ' + . 'and n.actor_id = :id ' + . 'order by f.created DESC', ['id' => $id], ); @@ -76,11 +174,12 @@ class Favourite Event::handle('FormatNoteList', [$notes, &$notes_out]); return [ - '_template' => 'network/feed.html.twig', - 'notes' => $notes, + '_template' => 'network/feed.html.twig', + 'notes' => $notes, 'page_title' => 'Reverse favourites timeline.', ]; } + public function reverseFavouritesByActorNickname(Request $request, string $nickname) { $user = DB::findOneBy('local_user', ['nickname' => $nickname]); diff --git a/plugins/Favourite/Favourite.php b/plugins/Favourite/Favourite.php index 4830fa538d..1ea514dd52 100644 --- a/plugins/Favourite/Favourite.php +++ b/plugins/Favourite/Favourite.php @@ -25,8 +25,6 @@ namespace Plugin\Favourite; use App\Core\DB\DB; use App\Core\Event; -use App\Core\Form; -use function App\Core\I18n\_m; use App\Core\Modules\NoteHandlerPlugin; use App\Core\Router\RouteLoader; use App\Core\Router\Router; @@ -36,8 +34,6 @@ use App\Util\Exception\InvalidFormException; use App\Util\Exception\NoSuchNoteException; use App\Util\Exception\RedirectException; use App\Util\Nickname; -use Symfony\Component\Form\Extension\Core\Type\HiddenType; -use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\HttpFoundation\Request; class Favourite extends NoteHandlerPlugin @@ -58,61 +54,24 @@ class Favourite extends NoteHandlerPlugin return Event::next; } - // if note is favoured, "is_set" is 1 + // If note is favourite, "is_set" is 1 $opts = ['note_id' => $note->getId(), 'actor_id' => $user->getId()]; - $is_set = DB::find('favourite', $opts) !== null; - $form_fav = Form::create([ - ['submit_favourite', SubmitType::class, - [ - 'label' => ' ', - 'attr' => [ - 'class' => ($is_set ? 'note-actions-set' : 'note-actions-unset') . ' button-container favourite-button-container', - 'title' => $is_set ? _m('Note already favourite!') : _m('Favourite this note!'), - ], - ], - ], - ['note_id', HiddenType::class, ['data' => $note->getId()]], - ["favourite-{$note->getId()}", HiddenType::class, ['data' => $is_set ? '1' : '0']], - ]); + $is_favourite = DB::find('favourite', $opts) !== null; - // Form handler - $ret = self::noteActionHandle( - $request, - $form_fav, - $note, - "favourite-{$note->getId()}", - /** - * Called from form handler - * - * @param Note $note to be favoured - * @param Form $data input - * - * @throws RedirectException Always thrown in order to prevent accidental form re-submit from browser - */ - function (Note $note, Form $data) use ($opts) { - $favourite_note = DB::find('favourite', $opts); - if ($data["favourite-{$note->getId()}"] === '0' && $favourite_note === null) { - DB::persist(Entity\Favourite::create($opts)); - DB::flush(); - } else { - if ($data["favourite-{$note->getId()}"] === '1' && $favourite_note !== null) { - DB::remove($favourite_note); - DB::flush(); - } - } + // Generating URL for favourite action route + $args = ['id' => $note->getId()]; + $type = Router::ABSOLUTE_PATH; + $favourite_action_url = $is_favourite ? + Router::url('note_remove_favourite', $args, $type) : + Router::url('note_add_favourite', $args, $type); - // Prevent accidental refreshes from resubmitting the form - throw new RedirectException(); + $extra_classes = $is_favourite ? "note-actions-set" : "note-actions-unset"; + $favourite_action = [ + "url" => $favourite_action_url, + "classes" => "button-container favourite-button-container $extra_classes" + ]; - return Event::stop; - }, - ); - - if ($ret !== null) { - return $ret; - } - - $actions[] = $form_fav->createView(); + $actions[] = $favourite_action; return Event::next; } @@ -125,8 +84,15 @@ class Favourite extends NoteHandlerPlugin public function onAddRoute(RouteLoader $r): bool { + // Add/remove note to/from favourites + $r->connect(id: 'note_add_favourite', uri_path: '/note/{id<\d+>}/add_favourite', target: [Controller\Favourite::class, 'noteAddFavourite']); + $r->connect(id: 'note_remove_favourite', uri_path: '/note/{id<\d+>}/remove_favourite', target: [Controller\Favourite::class, 'noteRemoveFavourite']); + + // View all favourites by actor id $r->connect(id: 'actor_favourites_id', uri_path: '/actor/{id<\d+>}/favourites', target: [Controller\Favourite::class, 'favouritesByActorId']); $r->connect(id: 'actor_reverse_favourites_id', uri_path: '/actor/{id<\d+>}/reverse_favourites', target: [Controller\Favourite::class, 'reverseFavouritesByActorId']); + + // View all favourites by nickname $r->connect(id: 'actor_favourites_nickname', uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/favourites', target: [Controller\Favourite::class, 'favouritesByActorNickname']); $r->connect(id: 'actor_reverse_favourites_nickname', uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/reverse_favourites', target: [Controller\Favourite::class, 'reverseFavouritesByActorNickname']); return Event::next; diff --git a/plugins/Favourite/templates/favourite/add_to_favourites.html.twig b/plugins/Favourite/templates/favourite/add_to_favourites.html.twig new file mode 100644 index 0000000000..4f406bfe20 --- /dev/null +++ b/plugins/Favourite/templates/favourite/add_to_favourites.html.twig @@ -0,0 +1,25 @@ +{% extends 'stdgrid.html.twig' %} + +{% block meta %} + {{ parent() }} +{% endblock %} + +{% block title %}{{ 'Favourite ' | trans }}{{ note.getActorNickname() }}{{ '\'s note.' | trans }}{% endblock %} + +{% block header %} + {{ parent() }} +{% endblock %} + +{% block left %} + {{ parent() }} +{% endblock %} + +{% block body %} + {{ parent() }} +
+
+ {% include '/cards/note/view.html.twig' with {'note': note} only %} + {{ form(add_favourite) }} +
+
+{% endblock body %} diff --git a/plugins/Favourite/templates/favourite/remove_from_favourites.html.twig b/plugins/Favourite/templates/favourite/remove_from_favourites.html.twig new file mode 100644 index 0000000000..3c6d0ab8f2 --- /dev/null +++ b/plugins/Favourite/templates/favourite/remove_from_favourites.html.twig @@ -0,0 +1,25 @@ +{% extends 'stdgrid.html.twig' %} + +{% block meta %} + {{ parent() }} +{% endblock %} + +{% block title %}{{ 'Remove favourite from ' | trans }}{{ note.getActorNickname() }}{{ '\'s note.' | trans }}{% endblock %} + +{% block header %} + {{ parent() }} +{% endblock %} + +{% block left %} + {{ parent() }} +{% endblock %} + +{% block body %} + {{ parent() }} +
+
+ {% include '/cards/note/view.html.twig' with {'note': note} only %} + {{ form(remove_favourite) }} +
+
+{% endblock body %} diff --git a/plugins/Repeat/Repeat.php b/plugins/Repeat/Repeat.php index bc4e877be5..800500ecad 100644 --- a/plugins/Repeat/Repeat.php +++ b/plugins/Repeat/Repeat.php @@ -35,13 +35,14 @@ use Symfony\Component\HttpFoundation\Request; class Repeat extends NoteHandlerPlugin { + // TODO: Refactoring to link instead of a form /** * HTML rendering event that adds the repeat form as a note * action, if a user is logged in * * @throws RedirectException */ - public function onAddNoteActions(Request $request, Note $note, array &$actions) +/* public function onAddNoteActions(Request $request, Note $note, array &$actions) { if (($user = Common::user()) === null) { return Event::next; @@ -94,5 +95,5 @@ class Repeat extends NoteHandlerPlugin } $actions[] = $form_repeat->createView(); return Event::next; - } + }*/ } diff --git a/plugins/Reply/Reply.php b/plugins/Reply/Reply.php index 19c24a8606..dc60c96d94 100644 --- a/plugins/Reply/Reply.php +++ b/plugins/Reply/Reply.php @@ -44,13 +44,14 @@ class Reply extends NoteHandlerPlugin return Event::next; } + // TODO: Refactoring to link instead of a form /** * HTML rendering event that adds the reply form as a note action, * if a user is logged in * * @throws RedirectException */ - public function onAddNoteActions(Request $request, Note $note, array &$actions) +/* public function onAddNoteActions(Request $request, Note $note, array &$actions) { if (($user = Common::user()) === null) { return Event::next; @@ -97,5 +98,5 @@ class Reply extends NoteHandlerPlugin } $actions[] = $form->createView(); return Event::next; - } + }*/ } diff --git a/public/assets/default_theme/css/pages/feeds.css b/public/assets/default_theme/css/pages/feeds.css index c21f040946..6872a094b9 100644 --- a/public/assets/default_theme/css/pages/feeds.css +++ b/public/assets/default_theme/css/pages/feeds.css @@ -210,7 +210,6 @@ -o-mask-image: url("../../icons/delete.svg"); -moz-mask-image: url("../../icons/delete.svg"); mask-image: url("../../icons/delete.svg"); - background: #FF634766 } .note-content { diff --git a/templates/cards/note/view.html.twig b/templates/cards/note/view.html.twig index 4348b21ecc..abe76c3ba1 100644 --- a/templates/cards/note/view.html.twig +++ b/templates/cards/note/view.html.twig @@ -17,7 +17,7 @@ {% if app.user %}
{% for current_action in get_note_actions(note) %} - {{ form(current_action) }} + {% endfor %}
{% endif %} diff --git a/templates/network/feed.html.twig b/templates/network/feed.html.twig index 560ad6cbf8..99cd1b1488 100644 --- a/templates/network/feed.html.twig +++ b/templates/network/feed.html.twig @@ -21,5 +21,4 @@ {% endif %} - {% endblock body %} \ No newline at end of file