[Reply] Fixed reply plugin action, there was no need to query the database when handling.

This commit is contained in:
Eliseu Amaro 2021-09-05 15:56:32 +01:00 committed by Hugo Sales
parent f000532b7e
commit 85db9464ca
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
3 changed files with 45 additions and 40 deletions

View File

@ -61,9 +61,9 @@ class Favourite extends NoteHandlerPlugin
// if note is favourited, "is_set" is 1 // if note is favourited, "is_set" is 1
$opts = ['note_id' => $note->getId(), 'gsactor_id' => $user->getId()]; $opts = ['note_id' => $note->getId(), 'gsactor_id' => $user->getId()];
$is_set = DB::find('favourite', $opts) != null; $is_set = DB::find('favourite', $opts) !== null;
$form_fav = Form::create([ $form_fav = Form::create([
['submit_fav', SubmitType::class, ['submit_favourite', SubmitType::class,
[ [
'label' => ' ', 'label' => ' ',
'attr' => [ 'attr' => [
@ -71,8 +71,8 @@ class Favourite extends NoteHandlerPlugin
], ],
], ],
], ],
['note_id', HiddenType::class, ['data' => $note->getId()]], ['note_id', HiddenType::class, ['data' => $note->getId()]],
["favourite-{$note->getId()}", HiddenType::class, ['data' => $is_set ? '1' : '0']], ["favourite-{$note->getId()}", HiddenType::class, ['data' => $is_set ? '1' : '0']],
]); ]);
// Form handler // Form handler
@ -85,24 +85,22 @@ class Favourite extends NoteHandlerPlugin
* *
* @throws RedirectException Always thrown in order to prevent accidental form re-submit from browser * @throws RedirectException Always thrown in order to prevent accidental form re-submit from browser
*/ function ($note, $data) use ($opts, $request) { */ function ($note, $data) use ($opts, $request) {
$fave = DB::find('favourite', $opts); $favourite_note = DB::find('favourite', $opts);
if ($data["favourite-{$note->getId()}"] === '0' && $fave === null) { if ($data["favourite-{$note->getId()}"] === '0' && $favourite_note === null) {
DB::persist(Entity\Favourite::create($opts)); DB::persist(Entity\Favourite::create($opts));
DB::flush(); DB::flush();
} else { } else if ($data["favourite-{$note->getId()}"] === '1' && $favourite_note !== null) {
if ($data["favourite-{$note->getId()}"] === '1' && $fave !== null) { DB::remove($favourite_note);
DB::remove($fave);
DB::flush(); DB::flush();
} }
}
// Prevent accidental refreshes from resubmitting the form // Prevent accidental refreshes from resubmitting the form
throw new RedirectException(); throw new RedirectException();
return Event::stop; return Event::stop;
}); });
if ($ret != null) { if ($ret !== null) {
return $ret; return $ret;
} }

View File

@ -26,6 +26,7 @@ use App\Core\Modules\NoteHandlerPlugin;
use App\Entity\Note; use App\Entity\Note;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\NotFoundException; use App\Util\Exception\NotFoundException;
use App\Util\Exception\RedirectException;
use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -35,10 +36,11 @@ class Repeat extends NoteHandlerPlugin
/** /**
* 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
*/ */
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;
} }
@ -49,9 +51,8 @@ class Repeat extends NoteHandlerPlugin
// Not found // Not found
$is_set = false; $is_set = false;
} }
$form = Form::create([ $form_repeat = Form::create([
['note_id', HiddenType::class, ['data' => $note->getId()]], ['submit_repeat', SubmitType::class,
['repeat', SubmitType::class,
[ [
'label' => ' ', 'label' => ' ',
'attr' => [ 'attr' => [
@ -59,30 +60,36 @@ class Repeat extends NoteHandlerPlugin
], ],
], ],
], ],
['note_id', HiddenType::class, ['data' => $note->getId()]],
["repeat-{$note->getId()}", HiddenType::class, ['data' => $is_set ? '1' : '0']],
]); ]);
// Handle form // Handle form
$ret = self::noteActionHandle($request, $form, $note, 'repeat', function ($note, $data, $user) use ($opts) { $ret = self::noteActionHandle(
$note = DB::findOneBy('note', $opts); $request, $form_repeat, $note, "repeat-{$note->getId()}", function ($note, $data, $user) use ($opts) {
if (!$data['is_set'] && $note == null) {
DB::persist(Note::create([ if ($data["repeat-{$note->getId()}"] === '0') {
'gsactor_id' => $user->getId(), DB::persist(Note::create([
'repeat_of' => $note->getId(), 'gsactor_id' => $user->getId(),
'content' => $note->getContent(), 'repeat_of' => $note->getId(),
'is_local' => true, 'content' => $note->getContent(),
])); 'is_local' => true,
]));
} else {
DB::remove($note);
}
DB::flush(); DB::flush();
} else {
DB::remove($note); // Prevent accidental refreshes from resubmitting the form
DB::flush(); throw new RedirectException();
}
return Event::stop; return Event::stop;
}); });
if ($ret != null) { if ($ret !== null) {
return $ret; return $ret;
} }
$actions[] = $form->createView(); $actions[] = $form_repeat->createView();
return Event::next; return Event::next;
} }
} }

View File

@ -21,14 +21,14 @@
{% if have_user %} {% if have_user %}
{% for current_action in get_note_actions(note) %} {% for current_action in get_note_actions(note) %}
{{ form_start(current_action) }} {{ form_start(current_action) }}
{% if current_action.submit_fav is defined %} {% if current_action.submit_favourite is defined %}
<span title="Favourite this note." class="button-container favourite-button-container"> <span title="Favourite this note." class="button-container favourite-button-container">
{{ form_widget(current_action.submit_fav) }} {{ form_widget(current_action.submit_favourite) }}
</span> </span>
{% endif %} {% endif %}
{% if current_action.repeat is defined %} {% if current_action.submit_repeat is defined %}
<span title="Repeat this note." class="button-container repeat-button-container"> <span title="Repeat this note." class="button-container repeat-button-container">
{{ form_widget(current_action.repeat) }} {{ form_widget(current_action.submit_repeat) }}
</span> </span>
{% endif %} {% endif %}
{% if current_action.reply is defined %} {% if current_action.reply is defined %}