[PLUGIN][DeleteNote] Support ActivityPub

This commit is contained in:
Diogo Peralta Cordeiro 2021-12-28 16:07:35 +00:00
parent 9585472679
commit 3e83387e98
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
7 changed files with 68 additions and 26 deletions

View File

@ -143,7 +143,7 @@ class Activity extends Model
*/
public static function toJson(mixed $object, ?int $options = null): string
{
if ($object::class !== 'App\Entity\Activity') {
if ($object::class !== GSActivity::class) {
throw new InvalidArgumentException('First argument type is Activity');
}

View File

@ -171,7 +171,7 @@ class Actor extends Model
*/
public static function toJson(mixed $object, ?int $options = null): string
{
if ($object::class !== 'App\Entity\Actor') {
if ($object::class !== GSActor::class) {
throw new InvalidArgumentException('First argument type is Actor');
}
$rsa = ActivitypubRsa::getByActor($object);

View File

@ -307,7 +307,7 @@ class Note extends Model
*/
public static function toJson(mixed $object, ?int $options = null): string
{
if ($object::class !== 'App\Entity\Note') {
if ($object::class !== GSNote::class) {
throw new InvalidArgumentException('First argument type is Note');
}

View File

@ -71,7 +71,7 @@ class DeleteNote extends Controller
$form_delete->handleRequest($request);
if ($form_delete->isSubmitted()) {
if (!\is_null(\Plugin\DeleteNote\DeleteNote::deleteNote(note_id: $note_id, actor_id: $user->getId()))) {
if (!\is_null(\Plugin\DeleteNote\DeleteNote::deleteNote(note: $note_id, actor: $user->getId()))) {
DB::flush();
} else {
throw new ClientException(_m('Note already deleted!'));

View File

@ -32,6 +32,8 @@ use App\Entity\Actor;
use App\Entity\Note;
use App\Util\Common;
use App\Util\Exception\ClientException;
use DateTime;
use Plugin\ActivityPub\ActivityPub;
use Symfony\Component\HttpFoundation\Request;
/**
@ -62,12 +64,14 @@ class DeleteNote extends NoteHandlerPlugin
return $activity;
}
public static function deleteNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity
public static function deleteNote(Note|int $note, Actor|int $actor, string $source = 'web'): ?Activity
{
$actor = \is_int($actor) ? Actor::getById($actor) : $actor;
$note = \is_int($note) ? Note::getById($note) : $note;
// Try and find if note was already deleted
if (\is_null(DB::findOneBy(Activity::class, ['verb' => 'delete', 'object_type' => 'note', 'object_id' => $note_id], return_null: true))) {
if (\is_null(DB::findOneBy(Activity::class, ['verb' => 'delete', 'object_type' => 'note', 'object_id' => $note->getId()], return_null: true))) {
// If none found, then undertaker has a job to do
return self::undertaker(Actor::getById($actor_id), Note::getById($note_id));
return self::undertaker($actor, $note);
} else {
return null;
}
@ -101,4 +105,46 @@ class DeleteNote extends NoteHandlerPlugin
return Event::next;
}
// ActivityPub
private function activitypub_handler(Actor $actor, \ActivityPhp\Type\AbstractObject $type_activity, mixed $type_object, ?\Plugin\ActivityPub\Entity\ActivitypubActivity &$ap_act): bool
{
if ($type_activity->get('type') !== 'Delete'
|| !($type_object instanceof Note)) {
return Event::next;
}
$activity = self::deleteNote($type_object, $actor, source: 'ActivityPub');
if (!\is_null($activity)) {
// Store ActivityPub Activity
$ap_act = \Plugin\ActivityPub\Entity\ActivitypubActivity::create([
'activity_id' => $activity->getId(),
'activity_uri' => $type_activity->get('id'),
'created' => new DateTime($type_activity->get('published') ?? 'now'),
'modified' => new DateTime(),
]);
DB::persist($ap_act);
}
return Event::stop;
}
public function onNewActivityPubActivity(Actor $actor, \ActivityPhp\Type\AbstractObject $type_activity, \ActivityPhp\Type\AbstractObject $type_object, ?\Plugin\ActivityPub\Entity\ActivitypubActivity &$ap_act): bool
{
return $this->activitypub_handler($actor, $type_activity, $type_object, $ap_act);
}
public function onNewActivityPubActivityWithObject(Actor $actor, \ActivityPhp\Type\AbstractObject $type_activity, mixed $type_object, ?\Plugin\ActivityPub\Entity\ActivitypubActivity &$ap_act): bool
{
return $this->activitypub_handler($actor, $type_activity, $type_object, $ap_act);
}
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;
}
}

View File

@ -206,7 +206,6 @@ class Favourite extends NoteHandlerPlugin
}
}
$activity = null;
if ($type_activity->get('type') === 'Like') {
$activity = self::favourNote($note_id, $actor->getId(), source: 'ActivityPub');
} else {

View File

@ -44,7 +44,7 @@ use Symfony\Component\HttpFoundation\Request;
class RepeatNote extends NoteHandlerPlugin
{
public static function repeatNote(Note $note, int $actor_id, string $source = 'web'): Activity
public static function repeatNote(Note $note, int $actor_id, string $source = 'web'): ?Activity
{
$repeat_entity = DB::findBy('note_repeat', [
'actor_id' => $actor_id,
@ -52,12 +52,7 @@ class RepeatNote extends NoteHandlerPlugin
])[0] ?? null;
if (!\is_null($repeat_entity)) {
return DB::findBy('activity', [
'actor_id' => $actor_id,
'verb' => 'repeat',
'object_type' => 'note',
'object_id' => $note->getId(),
], order_by: ['created' => 'DESC'])[0];
return null;
}
// If it's a repeat, the reply_to should be to the original, conversation ought to be the same
@ -109,7 +104,7 @@ class RepeatNote extends NoteHandlerPlugin
])[0] ?? null;
// Remove the clone note
DB::findBy('note', ['id' => $already_repeated->getNoteId()])[0]->delete();
DB::findBy(Note::class, ['id' => $already_repeated->getNoteId()])[0]->delete();
// Remove from the note_repeat table
DB::remove(DB::findBy('note_repeat', ['note_id' => $already_repeated->getNoteId()])[0]);
@ -311,18 +306,20 @@ class RepeatNote extends NoteHandlerPlugin
}
if ($type_activity->get('type') === 'Announce') {
$act = self::repeatNote($note ?? Note::getById($note_id), $actor->getId(), source: 'ActivityPub');
$activity = self::repeatNote($note ?? Note::getById($note_id), $actor->getId(), source: 'ActivityPub');
} else {
$act = self::unrepeatNote($note_id, $actor->getId(), source: 'ActivityPub');
$activity = self::unrepeatNote($note_id, $actor->getId(), source: 'ActivityPub');
}
if (!\is_null($activity)) {
// Store ActivityPub Activity
$ap_act = \Plugin\ActivityPub\Entity\ActivitypubActivity::create([
'activity_id' => $activity->getId(),
'activity_uri' => $type_activity->get('id'),
'created' => new DateTime($type_activity->get('published') ?? 'now'),
'modified' => new DateTime(),
]);
DB::persist($ap_act);
}
// Store ActivityPub Activity
$ap_act = \Plugin\ActivityPub\Entity\ActivitypubActivity::create([
'activity_id' => $act->getId(),
'activity_uri' => $type_activity->get('id'),
'created' => new DateTime($type_activity->get('published') ?? 'now'),
'modified' => new DateTime(),
]);
DB::persist($ap_act);
return Event::stop;
}