From 17733f32d6440b99679870f57f82dd47b331c32c Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Fri, 11 Feb 2022 00:22:22 +0000 Subject: [PATCH] [PLUGIN][ActivityPub] Implement Group Outbox Fix various minor issues --- .../Notification/Entity/Notification.php | 13 +++++++++++-- plugins/ActivityPub/ActivityPub.php | 7 +++++++ plugins/ActivityPub/Controller/Outbox.php | 19 +++++++++++++------ src/Entity/Activity.php | 5 +++++ src/Entity/Actor.php | 2 +- src/Entity/Note.php | 2 +- 6 files changed, 38 insertions(+), 10 deletions(-) diff --git a/components/Notification/Entity/Notification.php b/components/Notification/Entity/Notification.php index 6d790069ff..10e7590716 100644 --- a/components/Notification/Entity/Notification.php +++ b/components/Notification/Entity/Notification.php @@ -121,7 +121,7 @@ class Notification extends Entity */ public static function getNotificationTargetIdsByActivity(int|Activity $activity_id): array { - $notifications = DB::findBy('notification', ['activity_id' => \is_int($activity_id) ? $activity_id : $activity_id->getId()]); + $notifications = DB::findBy(self::class, ['activity_id' => \is_int($activity_id) ? $activity_id : $activity_id->getId()]); $targets = []; foreach ($notifications as $notification) { $targets[] = $notification->getTargetId(); @@ -131,7 +131,16 @@ class Notification extends Entity public function getNotificationTargetsByActivity(int|Activity $activity_id): array { - return DB::findBy('actor', ['id' => $this->getNotificationTargetIdsByActivity($activity_id)]); + return DB::findBy(Actor::class, ['id' => $this->getNotificationTargetIdsByActivity($activity_id)]); + } + + public static function getAllActivitiesTargetedAtActor(Actor $actor): array + { + return DB::dql(<<<'EOF' + SELECT act FROM \App\Entity\Activity AS act + WHERE act.object_type = 'note' AND act.id IN + (SELECT att.activity_id FROM \Component\Notification\Entity\Notification AS att WHERE att.target_id = :id) + EOF, ['id' => $actor->getId()]); } public static function schemaDef(): array diff --git a/plugins/ActivityPub/ActivityPub.php b/plugins/ActivityPub/ActivityPub.php index f0f0b81499..1c438cacb6 100644 --- a/plugins/ActivityPub/ActivityPub.php +++ b/plugins/ActivityPub/ActivityPub.php @@ -52,6 +52,8 @@ use Component\FreeNetwork\Entity\FreeNetworkActorProtocol; use Component\FreeNetwork\Util\Discovery; use Exception; use InvalidArgumentException; +use Plugin\ActivityPub\Util\Response\ActivityResponse; +use Symfony\Component\HttpFoundation\JsonResponse; use const PHP_URL_HOST; use Plugin\ActivityPub\Controller\Inbox; use Plugin\ActivityPub\Controller\Outbox; @@ -193,6 +195,9 @@ class ActivityPub extends Plugin case 'bot_actor_view_nickname': $response = ActorResponse::handle($vars['actor']); break; + case 'activity_view': + $response = ActivityResponse::handle($vars['activity']); + break; case 'note_view': $response = NoteResponse::handle($vars['note']); break; @@ -203,6 +208,8 @@ class ActivityPub extends Plugin if (Event::handle('ActivityPubActivityStreamsTwoResponse', [$route, $vars, &$response]) !== Event::stop) { if (is_subclass_of($vars['controller'][0], OrderedCollection::class)) { $response = new TypeResponse(OrderedCollectionController::fromControllerVars($vars)['type']); + } else { + $response = new JsonResponse(['error' => 'Unknown Object cannot be represented.']); } } } diff --git a/plugins/ActivityPub/Controller/Outbox.php b/plugins/ActivityPub/Controller/Outbox.php index a9caba3b2e..d70eee7ac1 100644 --- a/plugins/ActivityPub/Controller/Outbox.php +++ b/plugins/ActivityPub/Controller/Outbox.php @@ -33,6 +33,8 @@ declare(strict_types = 1); namespace Plugin\ActivityPub\Controller; use App\Core\DB\DB; +use App\Entity\Actor; +use Component\Notification\Entity\Notification; use function App\Core\I18n\_m; use App\Core\Log; use App\Core\Router\Router; @@ -55,24 +57,29 @@ class Outbox extends OrderedCollectionController */ public function viewOutboxByActorId(Request $request, int $gsactor_id): array { - try { - $user = DB::findOneBy('local_user', ['id' => $gsactor_id]); - } catch (Exception $e) { - throw new ClientException(_m('No such actor.'), 404, $e); + $actor = Actor::getById($gsactor_id); + if (is_null($actor)) { + throw new ClientException(_m('No such actor.'), 404); + } elseif (!$actor->getIsLocal()) { + throw new ClientException(_m('We have no authority over a remote actor\'s outbox.'), 400); } $this->actor_id = $gsactor_id; Log::debug('ActivityPub Outbox: Received a GET request.'); - $activities = DB::findBy(Activity::class, ['actor_id' => $user->getId()], order_by: ['created' => 'DESC']); + if ($actor->getType() !== Actor::GROUP) { + $activities = Activity::getAllActivitiesByActor($actor); + } else { + $activities = Notification::getAllActivitiesTargetedAtActor($actor); + } foreach ($activities as $act) { $this->ordered_items[] = Router::url('activity_view', ['id' => $act->getId()], ROUTER::ABSOLUTE_URL); } $this->route = 'activitypub_actor_outbox'; - $this->route_args = ['gsactor_id' => $user->getId(), 'page' => $this->int('page') ?? 0]; + $this->route_args = ['gsactor_id' => $actor->getId(), 'page' => $this->int('page') ?? 0]; return $this->handle($request); } diff --git a/src/Entity/Activity.php b/src/Entity/Activity.php index ab9e157d7f..8582d156ae 100644 --- a/src/Entity/Activity.php +++ b/src/Entity/Activity.php @@ -187,6 +187,11 @@ class Activity extends Entity return array_unique($target_ids); } + public static function getAllActivitiesByActor(Actor $actor): array + { + return DB::findBy(self::class, ['actor_id' => $actor->getId()], order_by: ['created' => 'DESC', 'id' => 'DESC']); + } + public static function schemaDef(): array { return [ diff --git a/src/Entity/Actor.php b/src/Entity/Actor.php index 28c71e437c..b55bfa708d 100644 --- a/src/Entity/Actor.php +++ b/src/Entity/Actor.php @@ -283,7 +283,7 @@ class Actor extends Entity public static function getById(int $id): ?self { - return Cache::get(self::cacheKeys($id)['id'], fn() => DB::find('actor', ['id' => $id])); + return Cache::get(self::cacheKeys($id)['id'], fn() => DB::findOneBy(self::class, ['id' => $id])); } public static function getNicknameById(int $id): string diff --git a/src/Entity/Note.php b/src/Entity/Note.php index a16108b58c..be436af40c 100644 --- a/src/Entity/Note.php +++ b/src/Entity/Note.php @@ -293,7 +293,7 @@ class Note extends Entity public static function getAllNotesByActor(Actor $actor): array { - return DB::findBy('note', ['actor_id' => $actor->getId()], order_by: ['created' => 'DESC', 'id' => 'DESC']); + return DB::findBy(self::class, ['actor_id' => $actor->getId()], order_by: ['created' => 'DESC', 'id' => 'DESC']); } public function getAttachments(): array