Update ProfileController, cache actor object and atom feed

This commit is contained in:
Daniel Supernault 2022-11-17 21:28:24 -07:00
parent 55003148d4
commit 8665eab190
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -187,10 +187,12 @@ class ProfileController extends Controller
abort_if(!config_cache('federation.activitypub.enabled'), 404); abort_if(!config_cache('federation.activitypub.enabled'), 404);
abort_if($user->domain, 404); abort_if($user->domain, 404);
$fractal = new Fractal\Manager(); return Cache::remember('pf:activitypub:user-object:by-id:' . $user->id, 3600, function() use($user) {
$resource = new Fractal\Resource\Item($user, new ProfileTransformer); $fractal = new Fractal\Manager();
$res = $fractal->createData($resource)->toArray(); $resource = new Fractal\Resource\Item($user, new ProfileTransformer);
return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json'); $res = $fractal->createData($resource)->toArray();
return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
});
} }
public function showAtomFeed(Request $request, $user) public function showAtomFeed(Request $request, $user)
@ -201,36 +203,47 @@ class ProfileController extends Controller
abort_if(!$pid, 404); abort_if(!$pid, 404);
$profile = AccountService::get($pid); $profile = AccountService::get($pid, true);
abort_if(!$profile || $profile['locked'] || !$profile['local'], 404); abort_if(!$profile || $profile['locked'] || !$profile['local'], 404);
$items = DB::table('statuses') $data = Cache::remember('pf:atom:user-feed:by-id:' . $profile['id'], 86400, function() use($pid, $profile) {
->whereProfileId($pid) $items = DB::table('statuses')
->whereVisibility('public') ->whereProfileId($pid)
->whereType('photo') ->whereVisibility('public')
->orderByDesc('id') ->whereType('photo')
->take(10) ->orderByDesc('id')
->get() ->take(10)
->map(function($status) { ->get()
return StatusService::get($status->id); ->map(function($status) {
}) return StatusService::get($status->id);
->filter(function($status) { })
return $status && ->filter(function($status) {
isset($status['account']) && return $status &&
isset($status['media_attachments']) && isset($status['account']) &&
count($status['media_attachments']); isset($status['media_attachments']) &&
}) count($status['media_attachments']);
->values(); })
$permalink = config('app.url') . "/users/{$profile['username']}.atom"; ->values();
$headers = ['Content-Type' => 'application/atom+xml']; $permalink = config('app.url') . "/users/{$profile['username']}.atom";
$headers = ['Content-Type' => 'application/atom+xml'];
if($items && $items->count()) { if($items && $items->count()) {
$headers['Last-Modified'] = now()->parse($items->first()['created_at'])->toRfc7231String(); $headers['Last-Modified'] = now()->parse($items->first()['created_at'])->toRfc7231String();
} }
return compact('items', 'permalink', 'headers');
});
abort_if(!$data, 404);
return response() return response()
->view('atom.user', compact('profile', 'items', 'permalink')) ->view('atom.user',
->withHeaders($headers); [
'profile' => $profile,
'items' => $data['items'],
'permalink' => $data['permalink']
]
)
->withHeaders($data['headers']);
} }
public function meRedirect() public function meRedirect()