mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-22 06:21:27 +00:00
Update NotificationService, use zrevrangebyscore for api
This commit is contained in:
parent
76b6ec2aa6
commit
d43e6d8d07
2 changed files with 71 additions and 24 deletions
|
@ -1283,7 +1283,6 @@ class ApiV1Controller extends Controller
|
|||
|
||||
$pid = $request->user()->profile_id;
|
||||
$limit = $request->input('limit', 20);
|
||||
$timeago = now()->subMonths(6);
|
||||
|
||||
$since = $request->input('since_id');
|
||||
$min = $request->input('min_id');
|
||||
|
@ -1293,27 +1292,20 @@ class ApiV1Controller extends Controller
|
|||
$min = 1;
|
||||
}
|
||||
|
||||
$dir = $since ? '>' : ($min ? '>=' : '<');
|
||||
$id = $since ?? $min ?? $max;
|
||||
$maxId = null;
|
||||
$minId = null;
|
||||
|
||||
$notifications = Notification::whereProfileId($pid)
|
||||
->where('id', $dir, $id)
|
||||
->whereDate('created_at', '>', $timeago)
|
||||
->orderByDesc('id')
|
||||
->limit($limit)
|
||||
->get();
|
||||
|
||||
$minId = $notifications->min('id');
|
||||
$maxId = $notifications->max('id');
|
||||
|
||||
$resource = new Fractal\Resource\Collection(
|
||||
$notifications,
|
||||
new NotificationTransformer()
|
||||
);
|
||||
|
||||
$res = $this->fractal
|
||||
->createData($resource)
|
||||
->toArray();
|
||||
if($max) {
|
||||
$res = NotificationService::getMax($pid, $max, $limit);
|
||||
$ids = NotificationService::getRankedMaxId($pid, $max, $limit);
|
||||
$maxId = max($ids);
|
||||
$minId = min($ids);
|
||||
} else {
|
||||
$res = NotificationService::getMin($pid, $min ?? $since, $limit);
|
||||
$ids = NotificationService::getRankedMinId($pid, $min ?? $since, $limit);
|
||||
$maxId = max($ids);
|
||||
$minId = min($ids);
|
||||
}
|
||||
|
||||
$baseUrl = config('app.url') . '/api/v1/notifications?';
|
||||
|
||||
|
|
|
@ -46,6 +46,60 @@ class NotificationService {
|
|||
return $ids;
|
||||
}
|
||||
|
||||
public static function getMax($id = false, $start, $limit = 10)
|
||||
{
|
||||
$ids = self::getRankedMaxId($id, $start, $limit);
|
||||
|
||||
if(empty($ids)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$res = collect([]);
|
||||
foreach($ids as $id) {
|
||||
$res->push(self::getNotification($id));
|
||||
}
|
||||
return $res->toArray();
|
||||
}
|
||||
|
||||
public static function getMin($id = false, $start, $limit = 10)
|
||||
{
|
||||
$ids = self::getRankedMinId($id, $start, $limit);
|
||||
|
||||
if(empty($ids)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$res = collect([]);
|
||||
foreach($ids as $id) {
|
||||
$res->push(self::getNotification($id));
|
||||
}
|
||||
return $res->toArray();
|
||||
}
|
||||
|
||||
public static function getRankedMaxId($id = false, $start = null, $limit = 10)
|
||||
{
|
||||
if(!$start || !$id) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, $start, '-inf', [
|
||||
'withscores' => true,
|
||||
'limit' => [1, $limit]
|
||||
]));
|
||||
}
|
||||
|
||||
public static function getRankedMinId($id = false, $end = null, $limit = 10)
|
||||
{
|
||||
if(!$end || !$id) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, '+inf', $end, [
|
||||
'withscores' => true,
|
||||
'limit' => [0, $limit]
|
||||
]));
|
||||
}
|
||||
|
||||
public static function set($id, $val)
|
||||
{
|
||||
return Redis::zadd(self::CACHE_KEY . $id, $val, $val);
|
||||
|
@ -53,6 +107,7 @@ class NotificationService {
|
|||
|
||||
public static function del($id, $val)
|
||||
{
|
||||
Cache::forget('service:notification:' . $val);
|
||||
return Redis::zrem(self::CACHE_KEY . $id, $val);
|
||||
}
|
||||
|
||||
|
@ -73,7 +128,7 @@ class NotificationService {
|
|||
|
||||
public static function getNotification($id)
|
||||
{
|
||||
return Cache::remember('service:notification:'.$id, now()->addMonths(3), function() use($id) {
|
||||
return Cache::remember('service:notification:'.$id, now()->addDays(3), function() use($id) {
|
||||
$n = Notification::with('item')->findOrFail($id);
|
||||
$fractal = new Fractal\Manager();
|
||||
$fractal->setSerializer(new ArraySerializer());
|
||||
|
@ -84,7 +139,7 @@ class NotificationService {
|
|||
|
||||
public static function setNotification(Notification $notification)
|
||||
{
|
||||
return Cache::remember('service:notification:'.$notification->id, now()->addMonths(3), function() use($notification) {
|
||||
return Cache::remember('service:notification:'.$notification->id, now()->addDays(3), function() use($notification) {
|
||||
$fractal = new Fractal\Manager();
|
||||
$fractal->setSerializer(new ArraySerializer());
|
||||
$resource = new Fractal\Resource\Item($notification, new NotificationTransformer());
|
||||
|
@ -106,4 +161,4 @@ class NotificationService {
|
|||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue