pixelfed/app/Services/ReblogService.php

69 lines
1.6 KiB
PHP
Raw Normal View History

<?php
namespace App\Services;
2022-03-11 06:34:34 +00:00
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
2022-03-11 06:34:34 +00:00
use App\Status;
class ReblogService
{
const CACHE_KEY = 'pf:services:reblogs:';
2022-03-11 06:34:34 +00:00
const REBLOGS_KEY = 'pf:services:reblogs:post:';
const COLDBOOT_KEY = 'pf:services:reblogs:post_:';
public static function get($profileId, $statusId)
{
if (!Redis::zcard(self::CACHE_KEY . $profileId)) {
return false;
}
return Redis::zscore(self::CACHE_KEY . $profileId, $statusId) != null;
}
public static function add($profileId, $statusId)
{
return Redis::zadd(self::CACHE_KEY . $profileId, $statusId, $statusId);
}
public static function del($profileId, $statusId)
{
return Redis::zrem(self::CACHE_KEY . $profileId, $statusId);
}
2022-03-11 06:34:34 +00:00
public static function getPostReblogs($id, $start = 0, $stop = 10)
{
if(!Redis::zcard(self::REBLOGS_KEY . $id)) {
return Cache::remember(self::COLDBOOT_KEY . $id, 86400, function() use($id) {
return Status::whereReblogOfId($id)
->pluck('id')
->each(function($reblog) use($id) {
self::addPostReblog($id, $reblog);
})
->map(function($reblog) {
return (string) $reblog;
});
});
}
return Redis::zrange(self::REBLOGS_KEY . $id, $start, $stop);
}
public static function addPostReblog($parentId, $reblogId)
{
2022-03-11 06:55:32 +00:00
$pid = intval($parentId);
$id = intval($reblogId);
if($pid && $id) {
return Redis::zadd(self::REBLOGS_KEY . $pid, $id);
}
2022-03-11 06:34:34 +00:00
}
public static function removePostReblog($parentId, $reblogId)
{
2022-03-11 06:55:32 +00:00
$pid = intval($parentId);
$id = intval($reblogId);
if($pid && $id) {
return Redis::zrem(self::REBLOGS_KEY . $pid, $id);
}
2022-03-11 06:34:34 +00:00
}
}