diff --git a/CHANGELOG.md b/CHANGELOG.md index eb207ae36..74053c4d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ ### Changed - Updated EmailService, added new domains [#1690](https://github.com/pixelfed/pixelfed/pull/1690) - Updated quill.js to v1.3.7 [#1692](https://github.com/pixelfed/pixelfed/pull/1690) +- Cache ProfileController [#1700](https://github.com/pixelfed/pixelfed/pull/1700) ## Deprecated - Remove deprecated profile following/followers [#1697](https://github.com/pixelfed/pixelfed/pull/1697) diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index cbc120ced..59fc4f451 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -20,80 +20,93 @@ class ProfileController extends Controller { public function show(Request $request, $username) { - $user = Profile::whereUsername($username)->firstOrFail(); - if($user->domain) { - return redirect($user->remote_url); - } - if($user->status != null) { - return $this->accountCheck($user); - } else { - return $this->buildProfile($request, $user); + $user = Profile::whereNull('domain') + ->whereNull('status') + ->whereUsername($username) + ->firstOrFail(); + if($request->wantsJson() && config('federation.activitypub.enabled')) { + return $this->showActivityPub($request, $user); } + return $this->buildProfile($request, $user); } - // TODO: refactor this mess protected function buildProfile(Request $request, $user) { $username = $user->username; $loggedIn = Auth::check(); $isPrivate = false; $isBlocked = false; + if(!$loggedIn) { + $key = 'profile:settings:' . $user->id; + $ttl = now()->addHours(6); + $settings = Cache::remember($key, $ttl, function() use($user) { + return $user->user->settings; + }); - if($user->status != null) { - return ProfileController::accountCheck($user); - } + if ($user->is_private == true) { + abort(404); + } - if ($user->remote_url) { - $settings = new \StdClass; - $settings->crawlable = false; - $settings->show_profile_follower_count = true; - $settings->show_profile_following_count = true; + $owner = false; + $is_following = false; + + $is_admin = $user->user->is_admin; + $profile = $user; + $settings = [ + 'crawlable' => $settings->crawlable, + 'following' => [ + 'count' => $settings->show_profile_following_count, + 'list' => $settings->show_profile_following + ], + 'followers' => [ + 'count' => $settings->show_profile_follower_count, + 'list' => $settings->show_profile_followers + ] + ]; + return view('profile.show', compact('profile', 'settings')); } else { - $settings = $user->user->settings; - } + $key = 'profile:settings:' . $user->id; + $ttl = now()->addHours(6); + $settings = Cache::remember($key, $ttl, function() use($user) { + return $user->user->settings; + }); - if ($request->wantsJson() && config('federation.activitypub.enabled')) { - return $this->showActivityPub($request, $user); - } + if ($user->is_private == true) { + $isPrivate = $this->privateProfileCheck($user, $loggedIn); + } - if ($user->is_private == true) { - $isPrivate = $this->privateProfileCheck($user, $loggedIn); - } - - if ($loggedIn == true) { $isBlocked = $this->blockedProfileCheck($user); + + $owner = $loggedIn && Auth::id() === $user->user_id; + $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false; + + if ($isPrivate == true || $isBlocked == true) { + $requested = Auth::check() ? FollowRequest::whereFollowerId(Auth::user()->profile_id) + ->whereFollowingId($user->id) + ->exists() : false; + return view('profile.private', compact('user', 'is_following', 'requested')); + } + + $is_admin = is_null($user->domain) ? $user->user->is_admin : false; + $profile = $user; + $settings = [ + 'crawlable' => $settings->crawlable, + 'following' => [ + 'count' => $settings->show_profile_following_count, + 'list' => $settings->show_profile_following + ], + 'followers' => [ + 'count' => $settings->show_profile_follower_count, + 'list' => $settings->show_profile_followers + ] + ]; + return view('profile.show', compact('profile', 'settings')); } - - $owner = $loggedIn && Auth::id() === $user->user_id; - $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false; - - if ($isPrivate == true || $isBlocked == true) { - $requested = Auth::check() ? FollowRequest::whereFollowerId(Auth::user()->profile_id) - ->whereFollowingId($user->id) - ->exists() : false; - return view('profile.private', compact('user', 'is_following', 'requested')); - } - - $is_admin = is_null($user->domain) ? $user->user->is_admin : false; - $profile = $user; - $settings = [ - 'crawlable' => $settings->crawlable, - 'following' => [ - 'count' => $settings->show_profile_following_count, - 'list' => $settings->show_profile_following - ], - 'followers' => [ - 'count' => $settings->show_profile_follower_count, - 'list' => $settings->show_profile_followers - ] - ]; - return view('profile.show', compact('user', 'profile', 'settings', 'owner', 'is_following', 'is_admin')); } public function permalinkRedirect(Request $request, $username) { - $user = Profile::whereUsername($username)->firstOrFail(); - $settings = User::whereUsername($username)->firstOrFail()->settings; + $user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail(); if ($request->wantsJson() && config('federation.activitypub.enabled')) { return $this->showActivityPub($request, $user); @@ -136,34 +149,19 @@ class ProfileController extends Controller return false; } - public static function accountCheck(Profile $profile) - { - switch ($profile->status) { - case 'disabled': - case 'suspended': - case 'delete': - return view('profile.disabled'); - break; - - default: - # code... - break; - } - - return abort(404); - } - public function showActivityPub(Request $request, $user) { abort_if(!config('federation.activitypub.enabled'), 404); - - if($user->status != null) { - return ProfileController::accountCheck($user); - } - $fractal = new Fractal\Manager(); - $resource = new Fractal\Resource\Item($user, new ProfileTransformer); - $res = $fractal->createData($resource)->toArray(); - return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json'); + abort_if($user->domain, 404); + $key = 'profile:ap:' . $user->id; + $ttl = now()->addHours(6); + + return Cache::remember($key, $ttl, function() use($user) { + $fractal = new Fractal\Manager(); + $resource = new Fractal\Resource\Item($user, new ProfileTransformer); + $res = $fractal->createData($resource)->toArray(); + return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json'); + }); } public function showAtomFeed(Request $request, $user) diff --git a/app/Http/Controllers/Settings/PrivacySettings.php b/app/Http/Controllers/Settings/PrivacySettings.php index 99f7d4100..754258fc7 100644 --- a/app/Http/Controllers/Settings/PrivacySettings.php +++ b/app/Http/Controllers/Settings/PrivacySettings.php @@ -29,14 +29,14 @@ trait PrivacySettings public function privacyStore(Request $request) { - $settings = Auth::user()->settings; - $profile = Auth::user()->profile; + $settings = $request->user()->settings; + $profile = $request->user()->profile; $fields = [ 'is_private', 'crawlable', 'show_profile_follower_count', 'show_profile_following_count', - ]; + ]; foreach ($fields as $field) { $form = $request->input($field); if ($field == 'is_private') { @@ -65,7 +65,7 @@ trait PrivacySettings } $settings->save(); } - + Cache::forget('profile:settings:' . $profile->id); return redirect(route('settings.privacy'))->with('status', 'Settings successfully updated!'); }