2021-05-01 21:51:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Util\ActivityPub\Helpers;
|
2021-07-02 07:31:45 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2021-05-01 21:51:02 +00:00
|
|
|
use Illuminate\Support\Facades\Redis;
|
|
|
|
use App\Like;
|
|
|
|
|
|
|
|
class LikeService {
|
|
|
|
|
|
|
|
const CACHE_KEY = 'pf:services:likes:ids:';
|
2022-09-21 11:34:39 +00:00
|
|
|
const CACHE_SET_KEY = 'pf:services:likes:set:';
|
2021-05-01 21:51:02 +00:00
|
|
|
|
2021-07-02 07:31:45 +00:00
|
|
|
public static function add($profileId, $statusId)
|
2021-05-01 21:51:02 +00:00
|
|
|
{
|
2021-07-02 07:31:45 +00:00
|
|
|
$key = self::CACHE_KEY . $profileId . ':' . $statusId;
|
2022-02-27 05:41:57 +00:00
|
|
|
Cache::increment('pf:services:likes:count:'.$statusId);
|
|
|
|
Cache::forget('pf:services:likes:liked_by:'.$statusId);
|
2022-09-21 11:34:39 +00:00
|
|
|
self::setAdd($profileId, $statusId);
|
2022-02-27 05:41:57 +00:00
|
|
|
return Cache::put($key, true, 86400);
|
2021-05-01 21:51:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 11:34:39 +00:00
|
|
|
public static function setAdd($profileId, $statusId)
|
|
|
|
{
|
|
|
|
if(self::setCount($profileId) > 400) {
|
|
|
|
if(config('database.redis.client') === 'phpredis') {
|
2022-09-29 10:50:10 +00:00
|
|
|
Redis::zpopmin(self::CACHE_SET_KEY . $profileId);
|
2022-09-21 11:34:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Redis::zadd(self::CACHE_SET_KEY . $profileId, $statusId, $statusId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function setCount($id)
|
|
|
|
{
|
|
|
|
return Redis::zcard(self::CACHE_SET_KEY . $id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function setRem($profileId, $val)
|
|
|
|
{
|
|
|
|
return Redis::zrem(self::CACHE_SET_KEY . $profileId, $val);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function get($profileId, $start = 0, $stop = 10)
|
|
|
|
{
|
|
|
|
if($stop > 100) {
|
|
|
|
$stop = 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Redis::zrevrange(self::CACHE_SET_KEY . $profileId, $start, $stop);
|
|
|
|
}
|
|
|
|
|
2021-07-02 07:31:45 +00:00
|
|
|
public static function remove($profileId, $statusId)
|
2021-05-01 21:51:02 +00:00
|
|
|
{
|
2021-07-02 07:31:45 +00:00
|
|
|
$key = self::CACHE_KEY . $profileId . ':' . $statusId;
|
2022-02-27 05:41:57 +00:00
|
|
|
Cache::decrement('pf:services:likes:count:'.$statusId);
|
|
|
|
Cache::forget('pf:services:likes:liked_by:'.$statusId);
|
2022-09-21 11:34:39 +00:00
|
|
|
self::setRem($profileId, $statusId);
|
2022-02-27 05:41:57 +00:00
|
|
|
return Cache::put($key, false, 86400);
|
2021-05-01 21:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function liked($profileId, $statusId)
|
|
|
|
{
|
2021-07-02 07:31:45 +00:00
|
|
|
$key = self::CACHE_KEY . $profileId . ':' . $statusId;
|
2022-02-27 05:41:57 +00:00
|
|
|
return Cache::remember($key, 86400, function() use($profileId, $statusId) {
|
2021-07-02 07:31:45 +00:00
|
|
|
return Like::whereProfileId($profileId)->whereStatusId($statusId)->exists();
|
|
|
|
});
|
2021-05-01 21:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function likedBy($status)
|
|
|
|
{
|
2021-05-03 23:55:06 +00:00
|
|
|
$empty = [
|
|
|
|
'username' => null,
|
|
|
|
'others' => false
|
|
|
|
];
|
|
|
|
|
|
|
|
if(!$status) {
|
|
|
|
return $empty;
|
|
|
|
}
|
|
|
|
|
2022-02-27 05:41:57 +00:00
|
|
|
$res = Cache::remember('pf:services:likes:liked_by:' . $status->id, 86400, function() use($status, $empty) {
|
|
|
|
$like = Like::whereStatusId($status->id)->first();
|
|
|
|
if(!$like) {
|
|
|
|
return $empty;
|
|
|
|
}
|
|
|
|
$id = $like->profile_id;
|
2023-05-09 04:47:59 +00:00
|
|
|
$profile = ProfileService::get($id, true);
|
|
|
|
if(!$profile) {
|
|
|
|
return [];
|
|
|
|
}
|
2022-02-27 05:41:57 +00:00
|
|
|
$profileUrl = "/i/web/profile/{$profile['id']}";
|
|
|
|
$res = [
|
|
|
|
'id' => (string) $profile['id'],
|
|
|
|
'username' => $profile['username'],
|
|
|
|
'url' => $profileUrl,
|
|
|
|
'others' => $status->likes_count >= 3,
|
|
|
|
];
|
|
|
|
return $res;
|
|
|
|
});
|
2021-05-03 23:55:06 +00:00
|
|
|
|
2022-02-27 05:41:57 +00:00
|
|
|
if(!isset($res['id']) || !isset($res['url'])) {
|
2021-05-03 23:55:06 +00:00
|
|
|
return $empty;
|
|
|
|
}
|
|
|
|
|
2022-02-27 05:41:57 +00:00
|
|
|
$res['total_count'] = ($status->likes_count - 1);
|
|
|
|
$res['total_count_pretty'] = number_format($res['total_count']);
|
2021-05-13 04:18:00 +00:00
|
|
|
|
|
|
|
return $res;
|
2021-05-01 21:51:02 +00:00
|
|
|
}
|
2021-08-31 06:37:40 +00:00
|
|
|
|
|
|
|
public static function count($id)
|
|
|
|
{
|
2022-02-27 05:41:57 +00:00
|
|
|
return Cache::get('pf:services:likes:count:'.$id, 0);
|
2021-08-31 06:37:40 +00:00
|
|
|
}
|
2022-02-27 05:41:57 +00:00
|
|
|
|
2021-05-01 21:51:02 +00:00
|
|
|
}
|