From 811caaadf92580ff66d4eaad86d6cef0d42b3b37 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Wed, 18 Aug 2021 19:14:24 +0100 Subject: [PATCH] [MODULES][PLUGINS] Move noteActionHandle utility to NoteHandlerPlugin which plugins which handle actions on notes should extend --- plugins/Favourite/Favourite.php | 4 +- plugins/Poll/Poll.php | 7 ++- plugins/Repeat/Repeat.php | 4 +- plugins/Reply/Reply.php | 4 +- src/Core/Modules/Module.php | 58 ------------------- src/Core/Modules/NoteHandlerPlugin.php | 79 ++++++++++++++++++++++++++ 6 files changed, 89 insertions(+), 67 deletions(-) create mode 100644 src/Core/Modules/NoteHandlerPlugin.php diff --git a/plugins/Favourite/Favourite.php b/plugins/Favourite/Favourite.php index 09a2df28b8..51569ca6fc 100644 --- a/plugins/Favourite/Favourite.php +++ b/plugins/Favourite/Favourite.php @@ -24,7 +24,7 @@ namespace Plugin\Favourite; use App\Core\DB\DB; use App\Core\Event; use App\Core\Form; -use App\Core\Modules\Plugin; +use App\Core\Modules\NoteHandlerPlugin; use App\Core\Router\RouteLoader; use App\Entity\Note; use App\Util\Common; @@ -35,7 +35,7 @@ use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\HttpFoundation\Request; -class Favourite extends Plugin +class Favourite extends NoteHandlerPlugin { /** * HTML rendering event that adds the favourite form as a note diff --git a/plugins/Poll/Poll.php b/plugins/Poll/Poll.php index 97ed900439..c71148097d 100644 --- a/plugins/Poll/Poll.php +++ b/plugins/Poll/Poll.php @@ -24,7 +24,7 @@ use App\Core\DB\DB; use App\Core\Event; use App\Core\Form; use function App\Core\I18n\_m; -use App\Core\Modules\Plugin; +use App\Core\Modules\NoteHandlerPlugin; use App\Core\Router\RouteLoader; use App\Entity\Note; use App\Entity\PollResponse; @@ -45,10 +45,11 @@ use Symfony\Component\HttpFoundation\Request; * @category Poll * * @author Daniel Brandao - * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org + * @author Hugo Sales + * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ -class Poll extends Plugin +class Poll extends NoteHandlerPlugin { /** * Map URLs to actions diff --git a/plugins/Repeat/Repeat.php b/plugins/Repeat/Repeat.php index b3a1436b6a..07cebcbce2 100644 --- a/plugins/Repeat/Repeat.php +++ b/plugins/Repeat/Repeat.php @@ -22,7 +22,7 @@ namespace Plugin\Repeat; use App\Core\DB\DB; use App\Core\Event; use App\Core\Form; -use App\Core\Modules\Plugin; +use App\Core\Modules\NoteHandlerPlugin; use App\Entity\Note; use App\Util\Common; use App\Util\Exception\NotFoundException; @@ -30,7 +30,7 @@ use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\HttpFoundation\Request; -class Repeat extends Plugin +class Repeat extends NoteHandlerPlugin { /** * HTML rendering event that adds the repeat form as a note diff --git a/plugins/Reply/Reply.php b/plugins/Reply/Reply.php index 44f9e6f536..011d783aa3 100644 --- a/plugins/Reply/Reply.php +++ b/plugins/Reply/Reply.php @@ -25,7 +25,7 @@ use App\Core\DB\DB; use App\Core\Event; use App\Core\Form; use function App\Core\I18n\_m; -use App\Core\Modules\Plugin; +use App\Core\Modules\NoteHandlerPlugin; use App\Entity\Note; use App\Util\Common; use App\Util\Exceptiion\InvalidFormException; @@ -37,7 +37,7 @@ use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\HttpFoundation\Request; -class Reply extends Plugin +class Reply extends NoteHandlerPlugin { public function onAddRoute($r) { diff --git a/src/Core/Modules/Module.php b/src/Core/Modules/Module.php index 82b14310fa..5ff6d1fed6 100644 --- a/src/Core/Modules/Module.php +++ b/src/Core/Modules/Module.php @@ -19,14 +19,7 @@ namespace App\Core\Modules; -use App\Core\Event; -use App\Core\Log; -use App\Entity\Note; use App\Util\Common; -use App\Util\Exception\InvalidFormException; -use App\Util\Exception\NoSuchNoteException; -use Symfony\Component\Form\Form; -use Symfony\Component\HttpFoundation\Request; /** * Base class for all GNU social modules (plugins and components) @@ -60,55 +53,4 @@ abstract class Module } return $obj; } - - /** - * Handle the $form submission for the note action for note if - * $note->getId() == $data['note_id'] - * - * This function is called when a user interacts with a note, such as through favouriting or commenting - * - * @codeCoverageIgnore - * - * @param Request $request - * @param Form $form - * @param Note $note - * @param string $form_name - * @param callable $handle - * - * @throws InvalidFormException - * @throws NoSuchNoteException - * - * @return bool|void - */ - 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::warning('Suspicious activity: user ' . $user->getNickname() . - ' tried to interact with 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(); - } - } - } - } - } - } } diff --git a/src/Core/Modules/NoteHandlerPlugin.php b/src/Core/Modules/NoteHandlerPlugin.php new file mode 100644 index 0000000000..adb5905cab --- /dev/null +++ b/src/Core/Modules/NoteHandlerPlugin.php @@ -0,0 +1,79 @@ +. +// }}} + +namespace App\Core\Modules; + +use App\Entity\Note; +use App\Util\Common; +use Symfony\Component\Form\Form; +use Symfony\Component\HttpFoundation\Request; + +class NoteHandlerPlugin extends Plugin +{ + /** + * Handle the $form submission for the note action for note if + * $note->getId() == $data['note_id'] + * + * This function is called when a user interacts with a note, such as through favouriting or commenting + * + * @codeCoverageIgnore + * + * @param Request $request + * @param Form $form + * @param Note $note + * @param string $form_name + * @param callable $handle + * + * @throws InvalidFormException + * @throws NoSuchNoteException + * + * @return bool|void + */ + 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::warning('Suspicious activity: user ' . $user->getNickname() . + ' tried to interact with 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(); + } + } + } + } + } + } +}