From c84cee5ae0abb24bd93c5941297e6ccd5ca03838 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 11 Apr 2020 22:12:28 -0600 Subject: [PATCH] Update ActivityPub Outbox, fixes #2100 --- app/Http/Controllers/FederationController.php | 14 +++++++-- app/Util/ActivityPub/Outbox.php | 31 +++++++++++++++---- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/FederationController.php b/app/Http/Controllers/FederationController.php index a21f556d6..8b7292c07 100644 --- a/app/Http/Controllers/FederationController.php +++ b/app/Http/Controllers/FederationController.php @@ -80,9 +80,19 @@ class FederationController extends Controller abort_if(!config('federation.activitypub.enabled'), 404); abort_if(!config('federation.activitypub.outbox'), 404); - $res = Outbox::get($username); + $profile = Profile::whereNull('domain') + ->whereNull('status') + ->whereIsPrivate(false) + ->whereUsername($username) + ->firstOrFail(); - return response(json_encode($res))->header('Content-Type', 'application/activity+json'); + $key = 'ap:outbox:latest_10:pid:' . $profile->id; + $ttl = now()->addMinutes(15); + $res = Cache::remember($key, $ttl, function() use($profile) { + return Outbox::get($profile); + }); + + return response(json_encode($res, JSON_UNESCAPED_SLASHES))->header('Content-Type', 'application/activity+json'); } public function userInbox(Request $request, $username) diff --git a/app/Util/ActivityPub/Outbox.php b/app/Util/ActivityPub/Outbox.php index b9a7d9657..630951334 100644 --- a/app/Util/ActivityPub/Outbox.php +++ b/app/Util/ActivityPub/Outbox.php @@ -3,30 +3,49 @@ namespace App\Util\ActivityPub; use App\Profile; +use App\Status; use League\Fractal; use App\Http\Controllers\ProfileController; use App\Transformer\ActivityPub\ProfileOutbox; +use App\Transformer\ActivityPub\Verb\CreateNote; class Outbox { - public static function get($username) + public static function get($profile) { abort_if(!config('federation.activitypub.enabled'), 404); abort_if(!config('federation.activitypub.outbox'), 404); - $profile = Profile::whereNull('remote_url')->whereUsername($username)->firstOrFail(); if($profile->status != null) { return ProfileController::accountCheck($profile); } + if($profile->is_private) { - return response()->json(['error'=>'403', 'msg' => 'private profile'], 403); + return ['error'=>'403', 'msg' => 'private profile']; } - $timeline = $profile->statuses()->whereVisibility('public')->orderBy('created_at', 'desc')->paginate(10); + + $timeline = $profile + ->statuses() + ->whereVisibility('public') + ->orderBy('created_at', 'desc') + ->take(10) + ->get(); + + $count = Status::whereProfileId($profile->id)->count(); + $fractal = new Fractal\Manager(); - $resource = new Fractal\Resource\Item($profile, new ProfileOutbox()); + $resource = new Fractal\Resource\Collection($timeline, new CreateNote()); $res = $fractal->createData($resource)->toArray(); - return $res['data']; + $outbox = [ + '@context' => 'https://www.w3.org/ns/activitystreams', + '_debug' => 'Outbox only supports latest 10 objects, pagination is not supported', + 'id' => $profile->permalink('/outbox'), + 'type' => 'OrderedCollection', + 'totalItems' => $count, + 'orderedItems' => $res['data'] + ]; + return $outbox; } } \ No newline at end of file