Skip to content

Commit

Permalink
Update Profile model, improve counter caching
Browse files Browse the repository at this point in the history
  • Loading branch information
dansup committed Dec 11, 2020
1 parent b4573a8 commit 4a14e97
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 23 deletions.
3 changes: 3 additions & 0 deletions app/Http/Controllers/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@ public function followRequestHandle(Request $request)
break;
}

Cache::forget('profile:follower_count:'.$pid);
Cache::forget('profile:following_count:'.$pid);

return response()->json(['msg' => 'success'], 200);
}

Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/Api/ApiV1Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,10 @@ public function accountFollowById(Request $request, $id)
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();
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/FollowerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ protected function handleFollowRequest($item, $force)
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();
}
Expand Down
62 changes: 40 additions & 22 deletions app/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,52 @@ public function statuses()

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()
Expand Down Expand Up @@ -148,18 +178,6 @@ public function avatarUrl()
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()
{
Expand Down
12 changes: 11 additions & 1 deletion app/Util/ActivityPub/Inbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function handleCreateActivity()
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')
Expand Down Expand Up @@ -342,6 +342,12 @@ public function handleFollowActivity()
'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;
Expand All @@ -365,6 +371,10 @@ public function handleFollowActivity()
]
];
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);
}
}

Expand Down

0 comments on commit 4a14e97

Please sign in to comment.