diff --git a/plugins/ActivityPub/Util/Model/Activity.php b/plugins/ActivityPub/Util/Model/Activity.php index 278fa73e12..ed1423546c 100644 --- a/plugins/ActivityPub/Util/Model/Activity.php +++ b/plugins/ActivityPub/Util/Model/Activity.php @@ -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'); } diff --git a/plugins/ActivityPub/Util/Model/Actor.php b/plugins/ActivityPub/Util/Model/Actor.php index c5c38907c2..c83f5c29fe 100644 --- a/plugins/ActivityPub/Util/Model/Actor.php +++ b/plugins/ActivityPub/Util/Model/Actor.php @@ -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); diff --git a/plugins/ActivityPub/Util/Model/Note.php b/plugins/ActivityPub/Util/Model/Note.php index ff1664a834..fd8c1272cf 100644 --- a/plugins/ActivityPub/Util/Model/Note.php +++ b/plugins/ActivityPub/Util/Model/Note.php @@ -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'); } diff --git a/plugins/DeleteNote/Controller/DeleteNote.php b/plugins/DeleteNote/Controller/DeleteNote.php index f8b3074e48..4f0ab14282 100644 --- a/plugins/DeleteNote/Controller/DeleteNote.php +++ b/plugins/DeleteNote/Controller/DeleteNote.php @@ -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!')); diff --git a/plugins/DeleteNote/DeleteNote.php b/plugins/DeleteNote/DeleteNote.php index de0bb8f618..2b78890c7f 100644 --- a/plugins/DeleteNote/DeleteNote.php +++ b/plugins/DeleteNote/DeleteNote.php @@ -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; + } } diff --git a/plugins/Favourite/Favourite.php b/plugins/Favourite/Favourite.php index ab1edb35b6..abbd03d897 100644 --- a/plugins/Favourite/Favourite.php +++ b/plugins/Favourite/Favourite.php @@ -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 { diff --git a/plugins/RepeatNote/RepeatNote.php b/plugins/RepeatNote/RepeatNote.php index 7af1e70ade..bc3a460f10 100644 --- a/plugins/RepeatNote/RepeatNote.php +++ b/plugins/RepeatNote/RepeatNote.php @@ -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; }