[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.

This commit is contained in:
Eliseu Amaro 2021-10-21 20:14:22 +01:00 committed by Diogo Peralta Cordeiro
parent a6d5752748
commit 60b15ea79d
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
10 changed files with 192 additions and 76 deletions

View File

@ -46,13 +46,14 @@ use Symfony\Component\HttpFoundation\Request;
*/ */
class DeleteNote extends NoteHandlerPlugin class DeleteNote extends NoteHandlerPlugin
{ {
// TODO: Refactoring to link instead of a form
/** /**
* HTML rendering event that adds the repeat form as a note * HTML rendering event that adds the repeat form as a note
* action, if a user is logged in * action, if a user is logged in
* *
* @throws RedirectException * @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) { if (($user = Common::user()) === null) {
return Event::next; return Event::next;
@ -100,5 +101,5 @@ class DeleteNote extends NoteHandlerPlugin
} }
$actions[] = $form_delete->createView(); $actions[] = $form_delete->createView();
return Event::next; return Event::next;
} }*/
} }

View File

@ -1,6 +1,6 @@
<?php <?php
declare(strict_types = 1); declare(strict_types=1);
// {{{ License // {{{ License
@ -23,12 +23,109 @@ declare(strict_types = 1);
namespace Plugin\Favourite\Controller; namespace Plugin\Favourite\Controller;
use App\Core\Controller;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\Form;
use App\Util\Common;
use App\Util\Exception\InvalidFormException;
use App\Util\Exception\NoLoggedInUser;
use App\Util\Exception\NoSuchNoteException;
use App\Util\Exception\RedirectException;
use Plugin\Favourite\Entity\Favourite as FavouriteEntity;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use function App\Core\I18n\_m;
class Favourite class Favourite extends Controller
{ {
/**
* @throws RedirectException
* @throws NoSuchNoteException
* @throws InvalidFormException
* @throws \App\Util\Exception\ServerException
*/
public function noteAddFavourite(Request $request, int $id): bool|array
{
$user = Common::ensureLoggedIn();
$opts = ['id' => $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) public function favouritesByActorId(Request $request, int $id)
{ {
$notes = DB::dql( $notes = DB::dql(
@ -43,11 +140,12 @@ class Favourite
Event::handle('FormatNoteList', [$notes, &$notes_out]); Event::handle('FormatNoteList', [$notes, &$notes_out]);
return [ return [
'_template' => 'network/feed.html.twig', '_template' => 'network/feed.html.twig',
'notes' => $notes_out, 'notes' => $notes_out,
'page_title' => 'Favourites timeline.', 'page_title' => 'Favourites timeline.',
]; ];
} }
public function favouritesByActorNickname(Request $request, string $nickname) public function favouritesByActorNickname(Request $request, string $nickname)
{ {
$user = DB::findOneBy('local_user', ['nickname' => $nickname]); $user = DB::findOneBy('local_user', ['nickname' => $nickname]);
@ -57,18 +155,18 @@ class Favourite
/** /**
* Reverse favourites stream * Reverse favourites stream
* *
* @throws \App\Util\Exception\NoLoggedInUser user not logged in
*
* @return array template * @return array template
* @throws NoLoggedInUser user not logged in
*
*/ */
public function reverseFavouritesByActorId(Request $request, int $id): array public function reverseFavouritesByActorId(Request $request, int $id): array
{ {
$notes = DB::dql( $notes = DB::dql(
'select n from App\Entity\Note n, Plugin\Favourite\Entity\Favourite f ' 'select n from App\Entity\Note n, Plugin\Favourite\Entity\Favourite f '
. 'where n.id = f.note_id ' . 'where n.id = f.note_id '
. 'and f.actor_id != :id ' . 'and f.actor_id != :id '
. 'and n.actor_id = :id ' . 'and n.actor_id = :id '
. 'order by f.created DESC', . 'order by f.created DESC',
['id' => $id], ['id' => $id],
); );
@ -76,11 +174,12 @@ class Favourite
Event::handle('FormatNoteList', [$notes, &$notes_out]); Event::handle('FormatNoteList', [$notes, &$notes_out]);
return [ return [
'_template' => 'network/feed.html.twig', '_template' => 'network/feed.html.twig',
'notes' => $notes, 'notes' => $notes,
'page_title' => 'Reverse favourites timeline.', 'page_title' => 'Reverse favourites timeline.',
]; ];
} }
public function reverseFavouritesByActorNickname(Request $request, string $nickname) public function reverseFavouritesByActorNickname(Request $request, string $nickname)
{ {
$user = DB::findOneBy('local_user', ['nickname' => $nickname]); $user = DB::findOneBy('local_user', ['nickname' => $nickname]);

View File

@ -25,8 +25,6 @@ namespace Plugin\Favourite;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\Form;
use function App\Core\I18n\_m;
use App\Core\Modules\NoteHandlerPlugin; use App\Core\Modules\NoteHandlerPlugin;
use App\Core\Router\RouteLoader; use App\Core\Router\RouteLoader;
use App\Core\Router\Router; use App\Core\Router\Router;
@ -36,8 +34,6 @@ use App\Util\Exception\InvalidFormException;
use App\Util\Exception\NoSuchNoteException; use App\Util\Exception\NoSuchNoteException;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
use App\Util\Nickname; 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; use Symfony\Component\HttpFoundation\Request;
class Favourite extends NoteHandlerPlugin class Favourite extends NoteHandlerPlugin
@ -58,61 +54,24 @@ class Favourite extends NoteHandlerPlugin
return Event::next; 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()]; $opts = ['note_id' => $note->getId(), 'actor_id' => $user->getId()];
$is_set = DB::find('favourite', $opts) !== null; $is_favourite = 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']],
]);
// Form handler // Generating URL for favourite action route
$ret = self::noteActionHandle( $args = ['id' => $note->getId()];
$request, $type = Router::ABSOLUTE_PATH;
$form_fav, $favourite_action_url = $is_favourite ?
$note, Router::url('note_remove_favourite', $args, $type) :
"favourite-{$note->getId()}", Router::url('note_add_favourite', $args, $type);
/**
* 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();
}
}
// Prevent accidental refreshes from resubmitting the form $extra_classes = $is_favourite ? "note-actions-set" : "note-actions-unset";
throw new RedirectException(); $favourite_action = [
"url" => $favourite_action_url,
"classes" => "button-container favourite-button-container $extra_classes"
];
return Event::stop; $actions[] = $favourite_action;
},
);
if ($ret !== null) {
return $ret;
}
$actions[] = $form_fav->createView();
return Event::next; return Event::next;
} }
@ -125,8 +84,15 @@ class Favourite extends NoteHandlerPlugin
public function onAddRoute(RouteLoader $r): bool 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_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']); $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_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']); $r->connect(id: 'actor_reverse_favourites_nickname', uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/reverse_favourites', target: [Controller\Favourite::class, 'reverseFavouritesByActorNickname']);
return Event::next; return Event::next;

View File

@ -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() }}
<div class="page">
<div class="main">
{% include '/cards/note/view.html.twig' with {'note': note} only %}
{{ form(add_favourite) }}
</div>
</div>
{% endblock body %}

View File

@ -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() }}
<div class="page">
<div class="main">
{% include '/cards/note/view.html.twig' with {'note': note} only %}
{{ form(remove_favourite) }}
</div>
</div>
{% endblock body %}

View File

@ -35,13 +35,14 @@ use Symfony\Component\HttpFoundation\Request;
class Repeat extends NoteHandlerPlugin class Repeat extends NoteHandlerPlugin
{ {
// TODO: Refactoring to link instead of a form
/** /**
* HTML rendering event that adds the repeat form as a note * HTML rendering event that adds the repeat form as a note
* action, if a user is logged in * action, if a user is logged in
* *
* @throws RedirectException * @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) { if (($user = Common::user()) === null) {
return Event::next; return Event::next;
@ -94,5 +95,5 @@ class Repeat extends NoteHandlerPlugin
} }
$actions[] = $form_repeat->createView(); $actions[] = $form_repeat->createView();
return Event::next; return Event::next;
} }*/
} }

View File

@ -44,13 +44,14 @@ class Reply extends NoteHandlerPlugin
return Event::next; return Event::next;
} }
// TODO: Refactoring to link instead of a form
/** /**
* HTML rendering event that adds the reply form as a note action, * HTML rendering event that adds the reply form as a note action,
* if a user is logged in * if a user is logged in
* *
* @throws RedirectException * @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) { if (($user = Common::user()) === null) {
return Event::next; return Event::next;
@ -97,5 +98,5 @@ class Reply extends NoteHandlerPlugin
} }
$actions[] = $form->createView(); $actions[] = $form->createView();
return Event::next; return Event::next;
} }*/
} }

View File

@ -210,7 +210,6 @@
-o-mask-image: url("../../icons/delete.svg"); -o-mask-image: url("../../icons/delete.svg");
-moz-mask-image: url("../../icons/delete.svg"); -moz-mask-image: url("../../icons/delete.svg");
mask-image: url("../../icons/delete.svg"); mask-image: url("../../icons/delete.svg");
background: #FF634766
} }
.note-content { .note-content {

View File

@ -17,7 +17,7 @@
{% if app.user %} {% if app.user %}
<div class="note-actions"> <div class="note-actions">
{% for current_action in get_note_actions(note) %} {% for current_action in get_note_actions(note) %}
{{ form(current_action) }} <a class="{{ current_action["classes"] }}" href="{{ current_action["url"] }}"></a>
{% endfor %} {% endfor %}
</div> </div>
{% endif %} {% endif %}

View File

@ -21,5 +21,4 @@
{% endif %} {% endif %}
</div> </div>
</main> </main>
{% endblock body %} {% endblock body %}