[PLUGIN][ActivityPub] Implement Group Outbox

Fix various minor issues
This commit is contained in:
Diogo Peralta Cordeiro 2022-02-11 00:22:22 +00:00
parent fb3e900b28
commit 17733f32d6
No known key found for this signature in database
GPG Key ID: 18D2D35001FBFAB0
6 changed files with 38 additions and 10 deletions

View File

@ -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

View File

@ -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.']);
}
}
}

View File

@ -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);
}

View File

@ -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 [

View File

@ -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

View File

@ -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