From 1705b543d304a8b784436ae781d17fcbcd2db9e2 Mon Sep 17 00:00:00 2001 From: eli Date: Mon, 4 Oct 2021 17:11:44 +0100 Subject: [PATCH] [PLUGIN] Delete note action added. If the author of a note is logged in, the action will be added to that note. Once the author presses it, the note will be deleted from the DB. --- plugins/DeleteNote/DeleteNote.php | 97 +++++++++++++++++++ .../assets/default_theme/css/pages/feeds.css | 8 ++ public/assets/default_theme/icons/delete.svg | 1 + 3 files changed, 106 insertions(+) create mode 100644 plugins/DeleteNote/DeleteNote.php create mode 100644 public/assets/default_theme/icons/delete.svg diff --git a/plugins/DeleteNote/DeleteNote.php b/plugins/DeleteNote/DeleteNote.php new file mode 100644 index 0000000000..35731c4316 --- /dev/null +++ b/plugins/DeleteNote/DeleteNote.php @@ -0,0 +1,97 @@ +. +// }}} + +namespace Plugin\DeleteNote; + +use App\Core\DB\DB; +use App\Core\Event; +use App\Core\Form; +use function App\Core\I18n\_m; +use App\Core\Modules\NoteHandlerPlugin; +use App\Entity\Note; +use App\Util\Common; +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; + +/** + * Delete note plugin main class. + * Adds "delete this note" action to respective note if the user logged in is the author. + * + * @package GNUsocial + * @category ProfileColor + * + * @author Eliseu Amaro + * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later + */ +class DeleteNote 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) { + return Event::next; + } + $user_id = $user->getId(); + $note_actor_id = $note->getActor()->getId(); + if ($user_id !== $note_actor_id) { + return Event::next; + } + + $note_id = $note->getId(); + $form_delete = Form::create([ + ['submit_delete', SubmitType::class, + [ + 'label' => ' ', + 'attr' => [ + 'class' => 'button-container delete-button-container', + 'title' => _m('Delete this note.'), + ], + ], + ], + ['note_id', HiddenType::class, ['data' => $note_id]], + ["delete-{$note_id}", HiddenType::class, []], + ]); + + // Handle form + $ret = self::noteActionHandle( + $request, $form_delete, $note, "delete-{$note_id}", function ($note, $note_id) { + DB::remove(DB::findOneBy('note', ['id' => $note_id])); + DB::flush(); + + // Prevent accidental refreshes from resubmitting the form + throw new RedirectException(); + + return Event::stop; + }); + + if ($ret !== null) { + return $ret; + } + $actions[] = $form_delete->createView(); + return Event::next; + } +} diff --git a/public/assets/default_theme/css/pages/feeds.css b/public/assets/default_theme/css/pages/feeds.css index 90f5b2fef6..13d79d17c0 100644 --- a/public/assets/default_theme/css/pages/feeds.css +++ b/public/assets/default_theme/css/pages/feeds.css @@ -143,6 +143,14 @@ mask-image: url("../../icons/repeat.svg") !important; } +.delete-button-container { + -webkit-mask-image: url("../../icons/delete.svg") !important; + -o-mask-image: url("../../icons/delete.svg") !important; + -moz-mask-image: url("../../icons/delete.svg") !important; + mask-image: url("../../icons/delete.svg") !important; + background: #FF634766 !important; +} + .note-actions-unset { background: var(--bg2) !important; } diff --git a/public/assets/default_theme/icons/delete.svg b/public/assets/default_theme/icons/delete.svg new file mode 100644 index 0000000000..299864d4b5 --- /dev/null +++ b/public/assets/default_theme/icons/delete.svg @@ -0,0 +1 @@ + \ No newline at end of file