mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-09 16:24:51 +00:00
Update LikeService, improve caching logic and add profile id to likedBy method to fix #3271
This commit is contained in:
parent
d06f513b36
commit
6af842eb3e
1 changed files with 28 additions and 35 deletions
|
@ -14,22 +14,23 @@ class LikeService {
|
||||||
public static function add($profileId, $statusId)
|
public static function add($profileId, $statusId)
|
||||||
{
|
{
|
||||||
$key = self::CACHE_KEY . $profileId . ':' . $statusId;
|
$key = self::CACHE_KEY . $profileId . ':' . $statusId;
|
||||||
$ttl = now()->addHours(2);
|
Cache::increment('pf:services:likes:count:'.$statusId);
|
||||||
return Cache::put($key, true, $ttl);
|
Cache::forget('pf:services:likes:liked_by:'.$statusId);
|
||||||
|
return Cache::put($key, true, 86400);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function remove($profileId, $statusId)
|
public static function remove($profileId, $statusId)
|
||||||
{
|
{
|
||||||
$key = self::CACHE_KEY . $profileId . ':' . $statusId;
|
$key = self::CACHE_KEY . $profileId . ':' . $statusId;
|
||||||
$ttl = now()->addHours(2);
|
Cache::decrement('pf:services:likes:count:'.$statusId);
|
||||||
return Cache::put($key, false, $ttl);
|
Cache::forget('pf:services:likes:liked_by:'.$statusId);
|
||||||
|
return Cache::put($key, false, 86400);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function liked($profileId, $statusId)
|
public static function liked($profileId, $statusId)
|
||||||
{
|
{
|
||||||
$key = self::CACHE_KEY . $profileId . ':' . $statusId;
|
$key = self::CACHE_KEY . $profileId . ':' . $statusId;
|
||||||
$ttl = now()->addMinutes(30);
|
return Cache::remember($key, 86400, function() use($profileId, $statusId) {
|
||||||
return Cache::remember($key, $ttl, function() use($profileId, $statusId) {
|
|
||||||
return Like::whereProfileId($profileId)->whereStatusId($statusId)->exists();
|
return Like::whereProfileId($profileId)->whereStatusId($statusId)->exists();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -45,44 +46,36 @@ class LikeService {
|
||||||
return $empty;
|
return $empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!$status->likes_count) {
|
$res = Cache::remember('pf:services:likes:liked_by:' . $status->id, 86400, function() use($status, $empty) {
|
||||||
return $empty;
|
$like = Like::whereStatusId($status->id)->first();
|
||||||
}
|
if(!$like) {
|
||||||
$user = request()->user();
|
return $empty;
|
||||||
|
}
|
||||||
|
$id = $like->profile_id;
|
||||||
|
$profile = ProfileService::get($id);
|
||||||
|
$profileUrl = "/i/web/profile/{$profile['id']}";
|
||||||
|
$res = [
|
||||||
|
'id' => (string) $profile['id'],
|
||||||
|
'username' => $profile['username'],
|
||||||
|
'url' => $profileUrl,
|
||||||
|
'others' => $status->likes_count >= 3,
|
||||||
|
];
|
||||||
|
return $res;
|
||||||
|
});
|
||||||
|
|
||||||
if($user) {
|
if(!isset($res['id']) || !isset($res['url'])) {
|
||||||
$like = Like::whereStatusId($status->id)
|
|
||||||
->where('profile_id', '!=', $user->profile_id)
|
|
||||||
->first();
|
|
||||||
} else {
|
|
||||||
$like = Like::whereStatusId($status->id)
|
|
||||||
->first();
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!$like) {
|
|
||||||
return $empty;
|
return $empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
$id = $like->profile_id;
|
$res['total_count'] = ($status->likes_count - 1);
|
||||||
|
$res['total_count_pretty'] = number_format($res['total_count']);
|
||||||
$profile = ProfileService::get($id);
|
|
||||||
$profileUrl = $profile['local'] ? $profile['url'] : '/i/web/profile/_/' . $profile['id'];
|
|
||||||
$res = [
|
|
||||||
'username' => $profile['username'],
|
|
||||||
'url' => $profileUrl,
|
|
||||||
'others' => $status->likes_count >= 3,
|
|
||||||
];
|
|
||||||
|
|
||||||
if(request()->user() && request()->user()->profile_id == $status->profile_id) {
|
|
||||||
$res['total_count'] = ($status->likes_count - 1);
|
|
||||||
$res['total_count_pretty'] = number_format($res['total_count']);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function count($id)
|
public static function count($id)
|
||||||
{
|
{
|
||||||
return Like::whereStatusId($id)->count();
|
return Cache::get('pf:services:likes:count:'.$id, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue