[NoteAction] Refactor duplicated code out to base class

This commit is contained in:
Hugo Sales 2020-09-10 22:28:11 +00:00 committed by Hugo Sales
parent a6c24393b5
commit 147ff89e74
No known key found for this signature in database
GPG Key ID: 7D0C7EAFC9D835A0
4 changed files with 90 additions and 85 deletions

View File

@ -26,7 +26,6 @@ use App\Core\Module;
use App\Entity\Favourite as Fave; use App\Entity\Favourite as Fave;
use App\Entity\Note; use App\Entity\Note;
use App\Util\Common; use App\Util\Common;
use App\Util\Exceptiion\InvalidFormException;
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;
@ -43,19 +42,8 @@ class Favourite extends Module
['note_id', HiddenType::class, ['data' => $note->getId()]], ['note_id', HiddenType::class, ['data' => $note->getId()]],
['favourite', SubmitType::class, ['label' => ' ']], ['favourite', SubmitType::class, ['label' => ' ']],
]); ]);
$ret = self::noteActionHandle($request, $form, $note, 'favourite', function ($note, $data) use ($opts) {
if ('POST' === $request->getMethod() && $request->request->has('favourite')) {
$form->handleRequest($request);
if ($form->isSubmitted()) {
$data = $form->getData();
// Loose comparison
if ($data['note_id'] != $note->getId()) {
return Event::next;
}
$fave = DB::find('favourite', $opts); $fave = DB::find('favourite', $opts);
if ($form->isValid()) {
// Loose comparison
if (!$data['is_set'] && ($fave == null)) { if (!$data['is_set'] && ($fave == null)) {
DB::persist(Fave::create($opts)); DB::persist(Fave::create($opts));
DB::flush(); DB::flush();
@ -64,12 +52,10 @@ class Favourite extends Module
DB::flush(); DB::flush();
} }
return Event::stop; return Event::stop;
} else { });
throw new InvalidFormException(); if ($ret != null) {
return $ret;
} }
}
}
$actions[] = $form->createView(); $actions[] = $form->createView();
return Event::next; return Event::next;
} }

View File

@ -25,7 +25,7 @@ use App\Core\Form;
use App\Core\Module; use App\Core\Module;
use App\Entity\Note; use App\Entity\Note;
use App\Util\Common; use App\Util\Common;
use App\Util\Exceptiion\InvalidFormException; use App\Util\Exception\NotFoundException;
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;
@ -34,45 +34,38 @@ class Repeat extends Module
{ {
public function onAddNoteActions(Request $request, Note $note, array &$actions) public function onAddNoteActions(Request $request, Note $note, array &$actions)
{ {
$user = Common::user();
$opts = ['gsactor_id' => $user->getId(), 'repeat_of' => $note->getId()];
try {
$is_set = DB::findOneBy('note', $opts) != null;
} catch (NotFoundException $e) {
// Not found
$is_set = false; $is_set = false;
}
$form = Form::create([ $form = Form::create([
['is_set', HiddenType::class, ['data' => $is_set ? '1' : '0']], ['is_set', HiddenType::class, ['data' => $is_set ? '1' : '0']],
['note_id', HiddenType::class, ['data' => $note->getId()]], ['note_id', HiddenType::class, ['data' => $note->getId()]],
['repeat', SubmitType::class, ['label' => ' ']], ['repeat', SubmitType::class, ['label' => ' ']],
]); ]);
$ret = self::noteActionHandle($request, $form, $note, 'repeat', function ($note, $data, $user) use ($opts) {
if ('POST' === $request->getMethod() && $request->request->has('repeat')) { $note = DB::findOneBy('note', $opts);
$form->handleRequest($request); if (!$data['is_set'] && $note == null) {
if ($form->isSubmitted()) { DB::persist(Note::create([
$data = $form->getData(); 'gsactor_id' => $user->getId(),
if ($data['note_id'] != $note . getId()) { 'repeat_of' => $note->getId(),
// ^ Loose comparison 'content' => $note->getContent(),
return Event::next; 'is_local' => true,
} else { ]));
if (!$note->isVisibleTo(Common::user())) {
// ^ Ensure user isn't trying to trip us up
Log::error('Suspicious activity: user ' . $user->getNickname() .
' tried to repeat note ' . $note->getId() .
', but they shouldn\'t have access to it');
throw new NoSuchNoteException();
} else {
if ($form->isValid()) {
if (!$data['is_set']) {
DB::persist(Note::create(['gsactor_id' => $user->getId(), 'repeat_of' => $note->getId(), 'content' => $note->getContent(), 'is_local' => true]));
DB::flush(); DB::flush();
} else { } else {
DB::remove(DB::findOneBy('note', ['gsactor_id' => $user->getId(), 'repeat_of' => $note->getId()])); DB::remove($note);
DB::flush(); DB::flush();
} }
return Event::stop; return Event::stop;
} else { });
throw new InvalidFormException(); if ($ret != null) {
return $ret;
} }
}
}
}
}
$actions[] = $form->createView(); $actions[] = $form->createView();
return Event::next; return Event::next;
} }

View File

@ -22,14 +22,17 @@ namespace Plugin\Reply;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\Form; use App\Core\Form;
use function App\Core\I18n\_m;
use App\Core\Module; use App\Core\Module;
use App\Entity\Note; use App\Entity\Note;
use App\Util\Common; use App\Util\Common;
use App\Util\Exceptiion\InvalidFormException; use App\Util\Exceptiion\InvalidFormException;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
use Componenet\Posting; use Componenet\Posting;
use Symfony\Component\Form\Extension\Core\Type\FileType;
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\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
class Reply extends Module class Reply extends Module
@ -41,23 +44,13 @@ class Reply extends Module
public function onAddNoteActions(Request $request, Note $note, array &$actions) public function onAddNoteActions(Request $request, Note $note, array &$actions)
{ {
$is_set = false;
$form = Form::create([ $form = Form::create([
['content', HiddenType::class, ['label' => ' ', 'required' => false]], ['content', HiddenType::class, ['label' => ' ', 'required' => false]],
['attachments', HiddenType::class, ['label' => ' ', 'required' => false]], ['attachments', HiddenType::class, ['label' => ' ', 'required' => false]],
['note_id', HiddenType::class, ['data' => $note->getId()]], ['note_id', HiddenType::class, ['data' => $note->getId()]],
['reply', SubmitType::class, ['label' => ' ']], ['reply', SubmitType::class, ['label' => ' ']],
]); ]);
$ret = self::noteActionHandle($request, $form, $note, 'reply', function ($note, $data) {
if ('POST' === $request->getMethod() && $request->request->has('reply')) {
$form->handleRequest($request);
if ($form->isSubmitted()) {
$data = $form->getData();
// Loose comparison
if ($data['note_id'] != $note->getId()) {
return Event::next;
} else {
if ($form->isValid()) {
if ($data['content'] !== null) { if ($data['content'] !== null) {
// JS submitted // JS submitted
// TODO DO THE THING // TODO DO THE THING
@ -65,18 +58,15 @@ class Reply extends Module
// JS disabled, redirect // JS disabled, redirect
throw new RedirectException('note_reply', ['reply_to' => $note->getId()]); throw new RedirectException('note_reply', ['reply_to' => $note->getId()]);
} }
} else { });
throw new InvalidFormException(); if ($ret != null) {
return $ret;
} }
}
}
}
$actions[] = $form->createView(); $actions[] = $form->createView();
return Event::next; return Event::next;
} }
public function reply(Request $request, string $reply_to) public function replyController(Request $request, string $reply_to)
{ {
$user = Common::ensureLoggedIn(); $user = Common::ensureLoggedIn();
$actor_id = $user->getId(); $actor_id = $user->getId();

View File

@ -19,6 +19,10 @@
namespace App\Core; namespace App\Core;
use App\Entity\Note;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
class Module class Module
{ {
public static function __set_state($state) public static function __set_state($state)
@ -30,4 +34,36 @@ class Module
} }
return $obj; return $obj;
} }
public static function noteActionHandle(Request $request, Form $form, Note $note, string $form_name, callable $handle)
{
if ('POST' === $request->getMethod() && $request->request->has($form_name)) {
$form->handleRequest($request);
if ($form->isSubmitted()) {
$data = $form->getData();
// Loose comparison
if ($data['note_id'] != $note->getId()) {
return Event::next;
} else {
$user = Common::user();
if (!$note->isVisibleTo($user)) {
// ^ Ensure user isn't trying to trip us up
Log::error('Suspicious activity: user ' . $user->getNickname() .
' tried to repeat note ' . $note->getId() .
', but they shouldn\'t have access to it');
throw new NoSuchNoteException();
} else {
if ($form->isValid()) {
$ret = $handle($note, $data, $user);
if ($ret != null) {
return $ret;
}
} else {
throw new InvalidFormException();
}
}
}
}
}
}
} }