1
0
Эх сурвалжийг харах

Update Profile model, improve counter caching

Daniel Supernault 4 жил өмнө
parent
commit
4a14e970f0

+ 3 - 0
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);
 	}
 

+ 4 - 0
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();

+ 4 - 0
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();
     }

+ 40 - 22
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()
     {

+ 11 - 1
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);
         }
     }