[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
$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([
['submit_fav', SubmitType::class,
['submit_favourite', SubmitType::class,
[
'label' => ' ',
'attr' => [
@ -85,16 +85,14 @@ class Favourite extends NoteHandlerPlugin
*
* @throws RedirectException Always thrown in order to prevent accidental form re-submit from browser
*/ function ($note, $data) use ($opts, $request) {
$fave = DB::find('favourite', $opts);
if ($data["favourite-{$note->getId()}"] === '0' && $fave === null) {
$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' && $fave !== null) {
DB::remove($fave);
} else if ($data["favourite-{$note->getId()}"] === '1' && $favourite_note !== null) {
DB::remove($favourite_note);
DB::flush();
}
}
// Prevent accidental refreshes from resubmitting the form
throw new RedirectException();
@ -102,7 +100,7 @@ class Favourite extends NoteHandlerPlugin
return Event::stop;
});
if ($ret != null) {
if ($ret !== null) {
return $ret;
}

View File

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

View File

@ -21,14 +21,14 @@
{% if have_user %}
{% for current_action in get_note_actions(note) %}
{{ 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">
{{ form_widget(current_action.submit_fav) }}
{{ form_widget(current_action.submit_favourite) }}
</span>
{% 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">
{{ form_widget(current_action.repeat) }}
{{ form_widget(current_action.submit_repeat) }}
</span>
{% endif %}
{% if current_action.reply is defined %}