[NoteAction] Refactor duplicated code out to base class
This commit is contained in:
parent
a6c24393b5
commit
147ff89e74
|
@ -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,33 +42,20 @@ 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')) {
|
$fave = DB::find('favourite', $opts);
|
||||||
$form->handleRequest($request);
|
if (!$data['is_set'] && ($fave == null)) {
|
||||||
if ($form->isSubmitted()) {
|
DB::persist(Fave::create($opts));
|
||||||
$data = $form->getData();
|
DB::flush();
|
||||||
// Loose comparison
|
} else {
|
||||||
if ($data['note_id'] != $note->getId()) {
|
DB::remove($fave);
|
||||||
return Event::next;
|
DB::flush();
|
||||||
}
|
|
||||||
|
|
||||||
$fave = DB::find('favourite', $opts);
|
|
||||||
if ($form->isValid()) {
|
|
||||||
// Loose comparison
|
|
||||||
if (!$data['is_set'] && ($fave == null)) {
|
|
||||||
DB::persist(Fave::create($opts));
|
|
||||||
DB::flush();
|
|
||||||
} else {
|
|
||||||
DB::remove($fave);
|
|
||||||
DB::flush();
|
|
||||||
}
|
|
||||||
return Event::stop;
|
|
||||||
} else {
|
|
||||||
throw new InvalidFormException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return Event::stop;
|
||||||
|
});
|
||||||
|
if ($ret != null) {
|
||||||
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
$actions[] = $form->createView();
|
$actions[] = $form->createView();
|
||||||
return Event::next;
|
return Event::next;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
{
|
{
|
||||||
$is_set = false;
|
$user = Common::user();
|
||||||
$form = Form::create([
|
$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;
|
||||||
|
}
|
||||||
|
$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())) {
|
DB::flush();
|
||||||
// ^ Ensure user isn't trying to trip us up
|
} else {
|
||||||
Log::error('Suspicious activity: user ' . $user->getNickname() .
|
DB::remove($note);
|
||||||
' tried to repeat note ' . $note->getId() .
|
DB::flush();
|
||||||
', 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();
|
|
||||||
} else {
|
|
||||||
DB::remove(DB::findOneBy('note', ['gsactor_id' => $user->getId(), 'repeat_of' => $note->getId()]));
|
|
||||||
DB::flush();
|
|
||||||
}
|
|
||||||
return Event::stop;
|
|
||||||
} else {
|
|
||||||
throw new InvalidFormException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return Event::stop;
|
||||||
|
});
|
||||||
|
if ($ret != null) {
|
||||||
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
$actions[] = $form->createView();
|
$actions[] = $form->createView();
|
||||||
return Event::next;
|
return Event::next;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,42 +44,29 @@ 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')) {
|
if ($data['content'] !== null) {
|
||||||
$form->handleRequest($request);
|
// JS submitted
|
||||||
if ($form->isSubmitted()) {
|
// TODO DO THE THING
|
||||||
$data = $form->getData();
|
} else {
|
||||||
// Loose comparison
|
// JS disabled, redirect
|
||||||
if ($data['note_id'] != $note->getId()) {
|
throw new RedirectException('note_reply', ['reply_to' => $note->getId()]);
|
||||||
return Event::next;
|
|
||||||
} else {
|
|
||||||
if ($form->isValid()) {
|
|
||||||
if ($data['content'] !== null) {
|
|
||||||
// JS submitted
|
|
||||||
// TODO DO THE THING
|
|
||||||
} else {
|
|
||||||
// JS disabled, redirect
|
|
||||||
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();
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user