[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.

This commit is contained in:
Eliseu Amaro 2021-10-04 17:11:44 +01:00
parent 70d1521a2b
commit 1705b543d3
3 changed files with 106 additions and 0 deletions

View File

@ -0,0 +1,97 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
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 <mail@eliseuama.ro>
* @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;
}
}

View File

@ -143,6 +143,14 @@
mask-image: url("../../icons/repeat.svg") !important; 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 { .note-actions-unset {
background: var(--bg2) !important; background: var(--bg2) !important;
} }

View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="100%" height="100%"><path fill-rule="evenodd" d="M2.343 13.657A8 8 0 1113.657 2.343 8 8 0 012.343 13.657zM6.03 4.97a.75.75 0 00-1.06 1.06L6.94 8 4.97 9.97a.75.75 0 101.06 1.06L8 9.06l1.97 1.97a.75.75 0 101.06-1.06L9.06 8l1.97-1.97a.75.75 0 10-1.06-1.06L8 6.94 6.03 4.97z"></path></svg>

After

Width:  |  Height:  |  Size: 352 B