2021-10-04 17:11:44 +01:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2021-10-04 17:11:44 +01:00
|
|
|
// {{{ 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 function App\Core\I18n\_m;
|
|
|
|
use App\Core\Modules\NoteHandlerPlugin;
|
2021-12-04 11:59:45 +00:00
|
|
|
use App\Core\Router\RouteLoader;
|
|
|
|
use App\Core\Router\Router;
|
2021-12-27 22:33:10 +00:00
|
|
|
use App\Entity\Activity;
|
|
|
|
use App\Entity\Actor;
|
2021-10-04 17:11:44 +01:00
|
|
|
use App\Entity\Note;
|
2021-12-28 06:18:59 +00:00
|
|
|
use App\Util\Exception\ClientException;
|
2021-10-04 17:11:44 +01:00
|
|
|
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
|
2021-12-27 22:33:10 +00:00
|
|
|
* @category DeleteNote
|
2021-10-04 17:11:44 +01:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
{
|
2021-12-28 06:18:59 +00:00
|
|
|
private static function undertaker(Actor $actor, Note $note): Activity
|
2021-12-04 11:59:45 +00:00
|
|
|
{
|
2021-12-27 22:33:10 +00:00
|
|
|
// Only let the original actor delete it
|
2021-12-28 06:18:59 +00:00
|
|
|
// TODO: Let actors of appropriate role do this as well
|
2021-12-27 22:33:10 +00:00
|
|
|
if ($note->getActor()->getId() !== $actor->getId()) {
|
2021-12-28 06:18:59 +00:00
|
|
|
throw new ClientException(_m('You don\'t have permissions to delete this note.'), 401);
|
2021-12-27 22:33:10 +00:00
|
|
|
}
|
|
|
|
|
2021-12-28 06:18:59 +00:00
|
|
|
// Undertaker believes the actor can terminate this note
|
|
|
|
$activity = $note->delete(actor: $actor, source: 'web');
|
2021-12-27 22:33:10 +00:00
|
|
|
|
|
|
|
// Undertaker successful
|
2021-12-28 06:18:59 +00:00
|
|
|
Event::handle('NewNotification', [$actor, $activity, [], "{$actor->getNickname()} deleted note {$activity->getObjectId()}"]);
|
|
|
|
return $activity;
|
2021-12-04 11:59:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 22:33:10 +00:00
|
|
|
public static function deleteNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity
|
|
|
|
{
|
|
|
|
// Try and find if note was already deleted
|
2021-12-28 06:18:59 +00:00
|
|
|
if (\is_null(DB::findOneBy(Activity::class, ['verb' => 'delete', 'object_type' => 'note', 'object_id' => $note_id], return_null: true))) {
|
2021-12-27 22:33:10 +00:00
|
|
|
// If none found, then undertaker has a job to do
|
2021-12-28 06:18:59 +00:00
|
|
|
return self::undertaker(Actor::getById($actor_id), Note::getById($note_id));
|
|
|
|
} else {
|
|
|
|
return null;
|
2021-12-27 22:33:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onAddRoute(RouteLoader $r)
|
2021-12-04 11:59:45 +00:00
|
|
|
{
|
2021-12-27 22:33:10 +00:00
|
|
|
$r->connect(id: 'delete_note_action', uri_path: '/object/note/{note_id<\d+>}/delete', target: Controller\DeleteNote::class);
|
|
|
|
|
|
|
|
return Event::next;
|
2021-12-04 11:59:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 22:33:10 +00:00
|
|
|
public function onAddExtraNoteActions(Request $request, Note $note, array &$actions)
|
2021-10-04 17:11:44 +01:00
|
|
|
{
|
2021-12-27 22:33:10 +00:00
|
|
|
// Only add action if note wasn't already deleted!
|
2021-12-28 06:18:59 +00:00
|
|
|
if (\is_null(DB::findOneBy(Activity::class, ['verb' => 'delete', 'object_type' => 'note', 'object_id' => $note->getId()], return_null: true))) {
|
|
|
|
$delete_action_url = Router::url('delete_note_action', ['note_id' => $note->getId()]);
|
2021-12-27 22:33:10 +00:00
|
|
|
$query_string = $request->getQueryString();
|
|
|
|
$delete_action_url .= '?from=' . mb_substr($query_string, 2);
|
|
|
|
$actions[] = [
|
|
|
|
'title' => _m('Delete note'),
|
|
|
|
'classes' => '',
|
|
|
|
'url' => $delete_action_url,
|
|
|
|
];
|
2021-10-04 17:11:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Event::next;
|
2021-12-27 22:33:10 +00:00
|
|
|
}
|
2021-10-04 17:11:44 +01:00
|
|
|
}
|