2021-10-05 01:11:44 +09:00
|
|
|
<?php
|
|
|
|
|
2021-12-29 22:54:09 +09:00
|
|
|
declare(strict_types = 1);
|
2021-10-10 17:26:18 +09:00
|
|
|
|
2021-10-05 01:11:44 +09: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;
|
|
|
|
|
2021-12-29 07:43:04 +09:00
|
|
|
use ActivityPhp\Type\AbstractObject;
|
2022-01-09 02:15:00 +09:00
|
|
|
use App\Core\Cache;
|
2022-03-27 23:19:09 +09:00
|
|
|
use App\Core\DB;
|
2021-10-05 01:11:44 +09:00
|
|
|
use App\Core\Event;
|
2021-12-29 22:54:09 +09:00
|
|
|
use function App\Core\I18n\_m;
|
2021-10-05 01:11:44 +09:00
|
|
|
use App\Core\Modules\NoteHandlerPlugin;
|
2022-03-28 00:43:59 +09:00
|
|
|
use App\Core\Router;
|
2021-12-28 07:33:10 +09:00
|
|
|
use App\Entity\Activity;
|
|
|
|
use App\Entity\Actor;
|
2021-10-05 01:11:44 +09:00
|
|
|
use App\Entity\Note;
|
2021-12-28 23:40:34 +09:00
|
|
|
use App\Util\Common;
|
2021-12-28 15:18:59 +09:00
|
|
|
use App\Util\Exception\ClientException;
|
2021-12-29 01:07:35 +09:00
|
|
|
use DateTime;
|
2021-12-29 07:43:04 +09:00
|
|
|
use Plugin\ActivityPub\Entity\ActivitypubActivity;
|
2021-10-05 01:11:44 +09:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete note plugin main class.
|
2021-12-29 07:43:04 +09:00
|
|
|
* Adds "delete this note" action to respective note if the user logged in is
|
|
|
|
* the author.
|
2021-10-05 01:11:44 +09:00
|
|
|
*
|
2021-12-29 07:43:04 +09:00
|
|
|
* @package GNUsocial
|
|
|
|
* @category DeleteNote
|
2021-10-05 01:11:44 +09:00
|
|
|
*
|
|
|
|
* @author Eliseu Amaro <mail@eliseuama.ro>
|
2022-03-28 00:43:59 +09:00
|
|
|
* @copyright 2021-2022 Free Software Foundation, Inc http://www.fsf.org
|
2021-10-05 01:11:44 +09:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
class DeleteNote extends NoteHandlerPlugin
|
|
|
|
{
|
2022-01-09 02:15:00 +09:00
|
|
|
public static function cacheKeys(int|Note $note_id): array
|
|
|
|
{
|
|
|
|
$note_id = \is_int($note_id) ? $note_id : $note_id->getId();
|
|
|
|
return [
|
|
|
|
'activity' => "deleted-note-activity-{$note_id}",
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-12-29 07:43:04 +09:00
|
|
|
/**
|
|
|
|
* **Checks actor permissions for the DeleteNote action, deletes given Note
|
|
|
|
* and creates respective Activity and Notification**
|
|
|
|
*
|
|
|
|
* Ensures the given Actor has sufficient permissions to perform the
|
|
|
|
* deletion.
|
|
|
|
* If it does, **Undertaker** will carry on, spelling doom for
|
|
|
|
* the given Note and **everything related to it**
|
|
|
|
* - Replies and Conversation **are unaffected**, except for the fact that
|
|
|
|
* this Note no longer exists, of course
|
|
|
|
* - Replies to this Note **will** remain on the same Conversation, and can
|
|
|
|
* **still be seen** on that Conversation (potentially separated from a
|
|
|
|
* parent, this Note)
|
|
|
|
*
|
|
|
|
* Replies shouldn't be taken out of context in any additional way, and
|
|
|
|
* **Undertaker** only calls the methods necessary to accomplish the
|
|
|
|
* deletion of this Note. Not any other as collateral damage.
|
|
|
|
*
|
|
|
|
* Creates the **_delete_ (verb)** Activity, performed on the given **Note
|
|
|
|
* (object)**, by the given **Actor (subject)**. Launches the
|
|
|
|
* NewNotification Event, stating who dared to call Undertaker.
|
|
|
|
*
|
|
|
|
* @throws \App\Util\Exception\ClientException
|
|
|
|
* @throws \App\Util\Exception\ServerException
|
|
|
|
*/
|
2021-12-28 15:18:59 +09:00
|
|
|
private static function undertaker(Actor $actor, Note $note): Activity
|
2021-12-04 20:59:45 +09:00
|
|
|
{
|
2021-12-28 23:40:34 +09:00
|
|
|
// Check permissions
|
2022-02-14 13:35:13 +09:00
|
|
|
if (!$actor->canModerate($note->getActor())) {
|
2021-12-28 15:18:59 +09:00
|
|
|
throw new ClientException(_m('You don\'t have permissions to delete this note.'), 401);
|
2021-12-28 07:33:10 +09:00
|
|
|
}
|
|
|
|
|
2021-12-28 15:18:59 +09:00
|
|
|
// Undertaker believes the actor can terminate this note
|
|
|
|
$activity = $note->delete(actor: $actor, source: 'web');
|
2022-01-09 02:15:00 +09:00
|
|
|
Cache::delete(self::cacheKeys($note)['activity']);
|
2021-12-28 07:33:10 +09:00
|
|
|
|
|
|
|
// Undertaker successful
|
2022-03-14 03:23:19 +09:00
|
|
|
Event::handle('NewNotification', [$actor, $activity, $note->getAttentionTargets(), _m('{actor_id} deleted note {note_id}.', ['actor_id' => $actor->getId(), 'note_id' => $activity->getObjectId()])]);
|
2021-12-28 15:18:59 +09:00
|
|
|
return $activity;
|
2021-12-04 20:59:45 +09:00
|
|
|
}
|
|
|
|
|
2021-12-29 07:43:04 +09:00
|
|
|
/**
|
|
|
|
* Delegates **DeleteNote::undertaker** to delete the Note provided
|
|
|
|
*
|
|
|
|
* Checks whether the Note has already been deleted, only passing on the
|
|
|
|
* responsibility to undertaker if the Note wasn't.
|
|
|
|
*
|
|
|
|
* @throws \App\Util\Exception\ClientException
|
|
|
|
* @throws \App\Util\Exception\DuplicateFoundException
|
|
|
|
* @throws \App\Util\Exception\NotFoundException
|
|
|
|
* @throws \App\Util\Exception\ServerException
|
|
|
|
*/
|
2021-12-29 01:07:35 +09:00
|
|
|
public static function deleteNote(Note|int $note, Actor|int $actor, string $source = 'web'): ?Activity
|
2021-12-28 07:33:10 +09:00
|
|
|
{
|
2021-12-29 22:54:09 +09:00
|
|
|
$actor = \is_int($actor) ? Actor::getById($actor) : $actor;
|
|
|
|
$note = \is_int($note) ? Note::getById($note) : $note;
|
2022-01-09 02:15:00 +09:00
|
|
|
// Try to find if note was already deleted
|
|
|
|
if (\is_null(
|
|
|
|
Cache::get(
|
|
|
|
self::cacheKeys($note)['activity'],
|
2022-03-14 03:23:19 +09:00
|
|
|
fn () => DB::findOneBy(Activity::class, ['verb' => 'delete', 'object_type' => Note::schemaName(), 'object_id' => $note->getId()], return_null: true),
|
2022-01-09 02:15:00 +09:00
|
|
|
),
|
|
|
|
)) {
|
2021-12-28 07:33:10 +09:00
|
|
|
// If none found, then undertaker has a job to do
|
2021-12-29 01:07:35 +09:00
|
|
|
return self::undertaker($actor, $note);
|
2021-12-28 15:18:59 +09:00
|
|
|
} else {
|
|
|
|
return null;
|
2021-12-28 07:33:10 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 07:43:04 +09:00
|
|
|
/**
|
|
|
|
* Adds and connects the _delete_note_action_ route to
|
|
|
|
* Controller\DeleteNote::class
|
|
|
|
*
|
|
|
|
* @return bool Event hook
|
|
|
|
*/
|
2022-03-28 00:43:59 +09:00
|
|
|
public function onAddRoute(Router $r)
|
2021-12-04 20:59:45 +09:00
|
|
|
{
|
2021-12-28 07:33:10 +09: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 20:59:45 +09:00
|
|
|
}
|
|
|
|
|
2021-12-29 07:43:04 +09:00
|
|
|
/**
|
|
|
|
* **Catches AddExtraNoteActions Event**
|
|
|
|
*
|
|
|
|
* Adds an anchor link to the route _delete_note_action_ in the **Note card
|
|
|
|
* template**. More specifically, in the **note_actions block**.
|
|
|
|
*
|
|
|
|
* @throws \App\Util\Exception\DuplicateFoundException
|
|
|
|
* @throws \App\Util\Exception\NotFoundException
|
|
|
|
* @throws \App\Util\Exception\ServerException
|
2021-12-29 22:54:09 +09:00
|
|
|
*
|
|
|
|
* @return bool Event hook
|
2021-12-29 07:43:04 +09:00
|
|
|
*/
|
2021-12-28 07:33:10 +09:00
|
|
|
public function onAddExtraNoteActions(Request $request, Note $note, array &$actions)
|
2021-10-05 01:11:44 +09:00
|
|
|
{
|
2021-12-29 22:54:09 +09:00
|
|
|
if (\is_null($actor = Common::actor())) {
|
2021-12-28 23:40:34 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
2022-01-09 02:15:00 +09:00
|
|
|
if (
|
|
|
|
// Only add action if note wasn't already deleted!
|
|
|
|
\is_null(Cache::get(
|
|
|
|
self::cacheKeys($note)['activity'],
|
2022-03-14 03:23:19 +09:00
|
|
|
fn () => DB::findOneBy(Activity::class, ['verb' => 'delete', 'object_type' => Note::schemaName(), 'object_id' => $note->getId()], return_null: true),
|
2022-01-09 02:15:00 +09:00
|
|
|
))
|
2021-12-29 07:43:04 +09:00
|
|
|
// And has permissions
|
2022-02-14 13:35:13 +09:00
|
|
|
&& $actor->canModerate($note->getActor())) {
|
2021-12-28 15:18:59 +09:00
|
|
|
$delete_action_url = Router::url('delete_note_action', ['note_id' => $note->getId()]);
|
2021-12-29 22:54:09 +09:00
|
|
|
$query_string = $request->getQueryString();
|
2022-03-07 08:06:40 +09:00
|
|
|
if (\is_null($query_string)) {
|
|
|
|
return Event::next;
|
|
|
|
}
|
2021-12-28 07:33:10 +09:00
|
|
|
$delete_action_url .= '?from=' . mb_substr($query_string, 2);
|
|
|
|
$actions[] = [
|
2021-12-29 22:54:09 +09:00
|
|
|
'title' => _m('Delete note'),
|
2021-12-28 07:33:10 +09:00
|
|
|
'classes' => '',
|
2021-12-29 22:54:09 +09:00
|
|
|
'url' => $delete_action_url,
|
2021-12-28 07:33:10 +09:00
|
|
|
];
|
2021-10-05 01:11:44 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return Event::next;
|
2021-12-28 07:33:10 +09:00
|
|
|
}
|
2021-12-29 01:07:35 +09:00
|
|
|
|
2021-12-29 07:52:15 +09:00
|
|
|
// ActivityPub handling and processing for Delete note is below
|
2021-12-29 01:07:35 +09:00
|
|
|
|
2021-12-29 07:52:15 +09:00
|
|
|
/**
|
|
|
|
* ActivityPub Inbox handler for Delete activities
|
|
|
|
*
|
|
|
|
* @param Actor $actor Actor who authored the activity
|
|
|
|
* @param \ActivityPhp\Type\AbstractObject $type_activity Activity Streams 2.0 Activity
|
|
|
|
* @param mixed $type_object Activity's Object
|
|
|
|
* @param null|\Plugin\ActivityPub\Entity\ActivitypubActivity $ap_act Resulting ActivitypubActivity
|
|
|
|
*
|
|
|
|
* @return bool Returns `Event::stop` if handled, `Event::next` otherwise
|
|
|
|
*/
|
2021-12-29 22:54:09 +09:00
|
|
|
private function activitypub_handler(Actor $actor, AbstractObject $type_activity, mixed $type_object, ?ActivitypubActivity &$ap_act): bool
|
2021-12-29 01:07:35 +09:00
|
|
|
{
|
|
|
|
if ($type_activity->get('type') !== 'Delete'
|
|
|
|
|| !($type_object instanceof Note)) {
|
2022-03-10 09:40:54 +09:00
|
|
|
// Don't care about AbstractObject because we only want to delete objects we already have
|
2021-12-29 01:07:35 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
|
|
|
$activity = self::deleteNote($type_object, $actor, source: 'ActivityPub');
|
2021-12-29 07:52:15 +09:00
|
|
|
if (!\is_null($activity)) {
|
2021-12-29 01:07:35 +09:00
|
|
|
// Store ActivityPub Activity
|
2021-12-29 07:52:15 +09:00
|
|
|
$ap_act = \Plugin\ActivityPub\Entity\ActivitypubActivity::create([
|
|
|
|
'activity_id' => $activity->getId(),
|
2021-12-29 01:07:35 +09:00
|
|
|
'activity_uri' => $type_activity->get('id'),
|
2021-12-29 07:52:15 +09:00
|
|
|
'created' => new DateTime($type_activity->get('published') ?? 'now'),
|
|
|
|
'modified' => new DateTime(),
|
2021-12-29 01:07:35 +09:00
|
|
|
]);
|
|
|
|
DB::persist($ap_act);
|
|
|
|
}
|
|
|
|
return Event::stop;
|
|
|
|
}
|
|
|
|
|
2021-12-29 07:52:15 +09:00
|
|
|
/**
|
|
|
|
* Convert an Activity Streams 2.0 Delete into the appropriate Delete entities
|
|
|
|
*
|
|
|
|
* @param Actor $actor Actor who authored the activity
|
|
|
|
* @param \ActivityPhp\Type\AbstractObject $type_activity Activity Streams 2.0 Activity
|
|
|
|
* @param \ActivityPhp\Type\AbstractObject $type_object Activity Streams 2.0 Object
|
|
|
|
* @param null|\Plugin\ActivityPub\Entity\ActivitypubActivity $ap_act Resulting ActivitypubActivity
|
|
|
|
*
|
|
|
|
* @return bool Returns `Event::stop` if handled, `Event::next` otherwise
|
|
|
|
*/
|
2021-12-29 22:54:09 +09:00
|
|
|
public function onNewActivityPubActivity(Actor $actor, AbstractObject $type_activity, AbstractObject $type_object, ?ActivitypubActivity &$ap_act): bool
|
2021-12-29 01:07:35 +09:00
|
|
|
{
|
|
|
|
return $this->activitypub_handler($actor, $type_activity, $type_object, $ap_act);
|
|
|
|
}
|
|
|
|
|
2021-12-29 07:52:15 +09:00
|
|
|
/**
|
|
|
|
* Convert an Activity Streams 2.0 formatted activity with a known object into Entities
|
|
|
|
*
|
|
|
|
* @param Actor $actor Actor who authored the activity
|
|
|
|
* @param \ActivityPhp\Type\AbstractObject $type_activity Activity Streams 2.0 Activity
|
|
|
|
* @param mixed $type_object Object
|
|
|
|
* @param null|\Plugin\ActivityPub\Entity\ActivitypubActivity $ap_act Resulting ActivitypubActivity
|
|
|
|
*
|
|
|
|
* @return bool Returns `Event::stop` if handled, `Event::next` otherwise
|
|
|
|
*/
|
2021-12-29 22:54:09 +09:00
|
|
|
public function onNewActivityPubActivityWithObject(Actor $actor, AbstractObject $type_activity, mixed $type_object, ?ActivitypubActivity &$ap_act): bool
|
2021-12-29 01:07:35 +09:00
|
|
|
{
|
|
|
|
return $this->activitypub_handler($actor, $type_activity, $type_object, $ap_act);
|
|
|
|
}
|
|
|
|
|
2021-12-29 07:52:15 +09:00
|
|
|
/**
|
|
|
|
* Translate GNU social internal verb 'delete' to Activity Streams 2.0 'Delete'
|
|
|
|
*
|
|
|
|
* @param string $verb GNU social's internal verb
|
|
|
|
* @param null|string $gs_verb_to_activity_stream_two_verb Resulting Activity Streams 2.0 verb
|
|
|
|
*
|
|
|
|
* @return bool Returns `Event::stop` if handled, `Event::next` otherwise
|
|
|
|
*/
|
2021-12-29 01:07:35 +09:00
|
|
|
public function onGSVerbToActivityStreamsTwoActivityType(string $verb, ?string &$gs_verb_to_activity_stream_two_verb): bool
|
|
|
|
{
|
|
|
|
if ($verb === 'delete') {
|
|
|
|
$gs_verb_to_activity_stream_two_verb = 'Delete';
|
|
|
|
return Event::stop;
|
|
|
|
}
|
|
|
|
return Event::next;
|
|
|
|
}
|
2021-10-05 01:11:44 +09:00
|
|
|
}
|