forked from GNUsocial/gnu-social
		
	[NoteAction] Refactor duplicated code out to base class
This commit is contained in:
		@@ -26,7 +26,6 @@ use App\Core\Module;
 | 
			
		||||
use App\Entity\Favourite as Fave;
 | 
			
		||||
use App\Entity\Note;
 | 
			
		||||
use App\Util\Common;
 | 
			
		||||
use App\Util\Exceptiion\InvalidFormException;
 | 
			
		||||
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
 | 
			
		||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
 | 
			
		||||
use Symfony\Component\HttpFoundation\Request;
 | 
			
		||||
@@ -43,33 +42,20 @@ class Favourite extends Module
 | 
			
		||||
            ['note_id',   HiddenType::class, ['data' => $note->getId()]],
 | 
			
		||||
            ['favourite', SubmitType::class, ['label' => ' ']],
 | 
			
		||||
        ]);
 | 
			
		||||
 | 
			
		||||
        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);
 | 
			
		||||
                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();
 | 
			
		||||
                }
 | 
			
		||||
        $ret = self::noteActionHandle($request, $form, $note, 'favourite', function ($note, $data) use ($opts) {
 | 
			
		||||
            $fave = DB::find('favourite', $opts);
 | 
			
		||||
            if (!$data['is_set'] && ($fave == null)) {
 | 
			
		||||
                DB::persist(Fave::create($opts));
 | 
			
		||||
                DB::flush();
 | 
			
		||||
            } else {
 | 
			
		||||
                DB::remove($fave);
 | 
			
		||||
                DB::flush();
 | 
			
		||||
            }
 | 
			
		||||
            return Event::stop;
 | 
			
		||||
        });
 | 
			
		||||
        if ($ret != null) {
 | 
			
		||||
            return $ret;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $actions[] = $form->createView();
 | 
			
		||||
        return Event::next;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -25,7 +25,7 @@ use App\Core\Form;
 | 
			
		||||
use App\Core\Module;
 | 
			
		||||
use App\Entity\Note;
 | 
			
		||||
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\SubmitType;
 | 
			
		||||
use Symfony\Component\HttpFoundation\Request;
 | 
			
		||||
@@ -34,45 +34,38 @@ class Repeat extends Module
 | 
			
		||||
{
 | 
			
		||||
    public function onAddNoteActions(Request $request, Note $note, array &$actions)
 | 
			
		||||
    {
 | 
			
		||||
        $is_set = false;
 | 
			
		||||
        $form   = Form::create([
 | 
			
		||||
        $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;
 | 
			
		||||
        }
 | 
			
		||||
        $form = Form::create([
 | 
			
		||||
            ['is_set',  HiddenType::class, ['data' => $is_set ? '1' : '0']],
 | 
			
		||||
            ['note_id', HiddenType::class, ['data' => $note->getId()]],
 | 
			
		||||
            ['repeat',  SubmitType::class, ['label' => ' ']],
 | 
			
		||||
        ]);
 | 
			
		||||
 | 
			
		||||
        if ('POST' === $request->getMethod() && $request->request->has('repeat')) {
 | 
			
		||||
            $form->handleRequest($request);
 | 
			
		||||
            if ($form->isSubmitted()) {
 | 
			
		||||
                $data = $form->getData();
 | 
			
		||||
                if ($data['note_id'] != $note . getId()) {
 | 
			
		||||
                    // ^ Loose comparison
 | 
			
		||||
                    return Event::next;
 | 
			
		||||
                } 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();
 | 
			
		||||
                            } else {
 | 
			
		||||
                                DB::remove(DB::findOneBy('note', ['gsactor_id' => $user->getId(), 'repeat_of' => $note->getId()]));
 | 
			
		||||
                                DB::flush();
 | 
			
		||||
                            }
 | 
			
		||||
                            return Event::stop;
 | 
			
		||||
                        } else {
 | 
			
		||||
                            throw new InvalidFormException();
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
        $ret = self::noteActionHandle($request, $form, $note, 'repeat', function ($note, $data, $user) use ($opts) {
 | 
			
		||||
            $note = DB::findOneBy('note', $opts);
 | 
			
		||||
            if (!$data['is_set'] && $note == null) {
 | 
			
		||||
                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();
 | 
			
		||||
            }
 | 
			
		||||
            return Event::stop;
 | 
			
		||||
        });
 | 
			
		||||
        if ($ret != null) {
 | 
			
		||||
            return $ret;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $actions[] = $form->createView();
 | 
			
		||||
        return Event::next;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -22,14 +22,17 @@ namespace Plugin\Reply;
 | 
			
		||||
use App\Core\DB\DB;
 | 
			
		||||
use App\Core\Event;
 | 
			
		||||
use App\Core\Form;
 | 
			
		||||
use function App\Core\I18n\_m;
 | 
			
		||||
use App\Core\Module;
 | 
			
		||||
use App\Entity\Note;
 | 
			
		||||
use App\Util\Common;
 | 
			
		||||
use App\Util\Exceptiion\InvalidFormException;
 | 
			
		||||
use App\Util\Exception\RedirectException;
 | 
			
		||||
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\SubmitType;
 | 
			
		||||
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
 | 
			
		||||
use Symfony\Component\HttpFoundation\Request;
 | 
			
		||||
 | 
			
		||||
class Reply extends Module
 | 
			
		||||
@@ -41,42 +44,29 @@ class Reply extends Module
 | 
			
		||||
 | 
			
		||||
    public function onAddNoteActions(Request $request, Note $note, array &$actions)
 | 
			
		||||
    {
 | 
			
		||||
        $is_set = false;
 | 
			
		||||
        $form   = Form::create([
 | 
			
		||||
        $form = Form::create([
 | 
			
		||||
            ['content',     HiddenType::class, ['label' => ' ', 'required' => false]],
 | 
			
		||||
            ['attachments', HiddenType::class, ['label' => ' ', 'required' => false]],
 | 
			
		||||
            ['note_id',     HiddenType::class, ['data' => $note->getId()]],
 | 
			
		||||
            ['reply',       SubmitType::class, ['label' => ' ']],
 | 
			
		||||
        ]);
 | 
			
		||||
 | 
			
		||||
        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) {
 | 
			
		||||
                            // JS submitted
 | 
			
		||||
                        // TODO DO THE THING
 | 
			
		||||
                        } else {
 | 
			
		||||
                            // JS disabled, redirect
 | 
			
		||||
                            throw new RedirectException('note_reply', ['reply_to' => $note->getId()]);
 | 
			
		||||
                        }
 | 
			
		||||
                    } else {
 | 
			
		||||
                        throw new InvalidFormException();
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
        $ret = self::noteActionHandle($request, $form, $note, 'reply', function ($note, $data) {
 | 
			
		||||
            if ($data['content'] !== null) {
 | 
			
		||||
                // JS submitted
 | 
			
		||||
                    // TODO DO THE THING
 | 
			
		||||
            } else {
 | 
			
		||||
                // JS disabled, redirect
 | 
			
		||||
                throw new RedirectException('note_reply', ['reply_to' => $note->getId()]);
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        if ($ret != null) {
 | 
			
		||||
            return $ret;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $actions[] = $form->createView();
 | 
			
		||||
        return Event::next;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function reply(Request $request, string $reply_to)
 | 
			
		||||
    public function replyController(Request $request, string $reply_to)
 | 
			
		||||
    {
 | 
			
		||||
        $user     = Common::ensureLoggedIn();
 | 
			
		||||
        $actor_id = $user->getId();
 | 
			
		||||
 
 | 
			
		||||
@@ -19,6 +19,10 @@
 | 
			
		||||
 | 
			
		||||
namespace App\Core;
 | 
			
		||||
 | 
			
		||||
use App\Entity\Note;
 | 
			
		||||
use Symfony\Component\Form\Form;
 | 
			
		||||
use Symfony\Component\HttpFoundation\Request;
 | 
			
		||||
 | 
			
		||||
class Module
 | 
			
		||||
{
 | 
			
		||||
    public static function __set_state($state)
 | 
			
		||||
@@ -30,4 +34,36 @@ class Module
 | 
			
		||||
        }
 | 
			
		||||
        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();
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user