From 4a14e970f0158f7de1aef8bc6ef1e6c403f28b35 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Thu, 10 Dec 2020 22:58:13 -0700 Subject: [PATCH] Update Profile model, improve counter caching --- app/Http/Controllers/AccountController.php | 3 + app/Http/Controllers/Api/ApiV1Controller.php | 4 ++ app/Http/Controllers/FollowerController.php | 4 ++ app/Profile.php | 62 +++++++++++++------- app/Util/ActivityPub/Inbox.php | 12 +++- 5 files changed, 62 insertions(+), 23 deletions(-) diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index b7af749f3..d0c3d4563 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -376,6 +376,9 @@ class AccountController extends Controller break; } + Cache::forget('profile:follower_count:'.$pid); + Cache::forget('profile:following_count:'.$pid); + return response()->json(['msg' => 'success'], 200); } diff --git a/app/Http/Controllers/Api/ApiV1Controller.php b/app/Http/Controllers/Api/ApiV1Controller.php index 0243939b7..1e7ef4846 100644 --- a/app/Http/Controllers/Api/ApiV1Controller.php +++ b/app/Http/Controllers/Api/ApiV1Controller.php @@ -468,6 +468,10 @@ class ApiV1Controller extends Controller Cache::forget('api:local:exp:rec:'.$user->profile_id); Cache::forget('user:account:id:'.$target->user_id); Cache::forget('user:account:id:'.$user->id); + Cache::forget('profile:follower_count:'.$target->id); + Cache::forget('profile:follower_count:'.$user->profile_id); + Cache::forget('profile:following_count:'.$target->id); + Cache::forget('profile:following_count:'.$user->profile_id); $resource = new Fractal\Resource\Item($target, new RelationshipTransformer()); $res = $this->fractal->createData($resource)->toArray(); diff --git a/app/Http/Controllers/FollowerController.php b/app/Http/Controllers/FollowerController.php index 06267dd91..abc729197 100644 --- a/app/Http/Controllers/FollowerController.php +++ b/app/Http/Controllers/FollowerController.php @@ -115,6 +115,10 @@ class FollowerController extends Controller Cache::forget('px:profile:followers-v1.3:'.$target->id); Cache::forget('px:profile:following-v1.3:'.$user->id); Cache::forget('px:profile:following-v1.3:'.$target->id); + Cache::forget('profile:follower_count:'.$target->id); + Cache::forget('profile:follower_count:'.$user->id); + Cache::forget('profile:following_count:'.$target->id); + Cache::forget('profile:following_count:'.$user->id); return $target->url(); } diff --git a/app/Profile.php b/app/Profile.php index 7c8325ad6..5b854dfcc 100644 --- a/app/Profile.php +++ b/app/Profile.php @@ -64,22 +64,52 @@ class Profile extends Model public function followingCount($short = false) { - $count = $this->following()->count(); - if ($short) { - return PrettyNumber::convert($count); - } else { + $count = Cache::remember('profile:following_count:'.$this->id, now()->addMonths(1), function() { + $count = $this->following_count; + if($count) { + return $count; + } + $count = $this->following()->count(); + $this->following_count = $count; + $this->save(); return $count; - } + }); + + return $short ? PrettyNumber::convert($count) : $count; } public function followerCount($short = false) { - $count = $this->followers()->count(); - if ($short) { - return PrettyNumber::convert($count); - } else { + $count = Cache::remember('profile:follower_count:'.$this->id, now()->addMonths(1), function() { + $count = $this->followers_count; + if($count) { + return $count; + } + $count = $this->followers()->count(); + $this->followers_count = $count; + $this->save(); return $count; - } + }); + return $short ? PrettyNumber::convert($count) : $count; + } + + public function statusCount() + { + return Cache::remember('profile:status_count:'.$this->id, now()->addMonths(1), function() { + $count = $this->status_count; + if($count) { + return $count; + } + $count = $this->statuses() + ->getQuery() + ->whereHas('media') + ->whereNull('in_reply_to_id') + ->whereNull('reblog_of_id') + ->count(); + $this->status_count = $count; + $this->save(); + return $count; + }); } public function following() @@ -148,18 +178,6 @@ class Profile extends Model return $url; } - public function statusCount() - { - return Cache::remember('profile:status_count:'.$this->id, now()->addMonths(1), function() { - return $this->statuses() - ->getQuery() - ->whereHas('media') - ->whereNull('in_reply_to_id') - ->whereNull('reblog_of_id') - ->count(); - }); - } - // deprecated public function recommendFollowers() { diff --git a/app/Util/ActivityPub/Inbox.php b/app/Util/ActivityPub/Inbox.php index c671f093c..4c47b4af6 100644 --- a/app/Util/ActivityPub/Inbox.php +++ b/app/Util/ActivityPub/Inbox.php @@ -145,7 +145,7 @@ class Inbox return; } $to = $activity['to']; - $cc = $activity['cc']; + $cc = isset($activity['cc']) ? $activity['cc'] : []; if(count($to) == 1 && count($cc) == 0 && parse_url($to[0], PHP_URL_HOST) == config('pixelfed.domain.app') @@ -342,6 +342,12 @@ class Inbox 'follower_id' => $actor->id, 'following_id' => $target->id ]); + + Cache::forget('profile:follower_count:'.$target->id); + Cache::forget('profile:follower_count:'.$actor->id); + Cache::forget('profile:following_count:'.$target->id); + Cache::forget('profile:following_count:'.$actor->id); + } else { $follower = new Follower; $follower->profile_id = $actor->id; @@ -365,6 +371,10 @@ class Inbox ] ]; Helpers::sendSignedObject($target, $actor->inbox_url, $accept); + Cache::forget('profile:follower_count:'.$target->id); + Cache::forget('profile:follower_count:'.$actor->id); + Cache::forget('profile:following_count:'.$target->id); + Cache::forget('profile:following_count:'.$actor->id); } }