1
0
Fork 0
pixelfed/app/Services/HomeTimelineService.php

115 lines
3.4 KiB
PHP
Raw Normal View History

2023-11-11 12:46:37 +00:00
<?php
namespace App\Services;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
use App\Follower;
use App\Status;
use App\Models\UserDomainBlock;
2023-11-11 12:46:37 +00:00
class HomeTimelineService
{
const CACHE_KEY = 'pf:services:timeline:home:';
2023-11-13 05:45:09 +00:00
const FOLLOWER_FEED_POST_LIMIT = 10;
2023-11-11 12:46:37 +00:00
public static function get($id, $start = 0, $stop = 10)
{
if($stop > 100) {
$stop = 100;
}
return Redis::zrevrange(self::CACHE_KEY . $id, $start, $stop);
}
public static function getRankedMaxId($id, $start = null, $limit = 10)
{
if(!$start) {
return [];
}
return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY . $id, $start, '-inf', [
'withscores' => true,
'limit' => [1, $limit - 1]
]));
}
public static function getRankedMinId($id, $end = null, $limit = 10)
{
if(!$end) {
return [];
}
return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY . $id, '+inf', $end, [
'withscores' => true,
'limit' => [0, $limit]
]));
}
public static function add($id, $val)
{
2023-11-13 04:09:06 +00:00
if(self::count($id) >= 400) {
2023-11-11 12:46:37 +00:00
Redis::zpopmin(self::CACHE_KEY . $id);
}
return Redis::zadd(self::CACHE_KEY .$id, $val, $val);
}
public static function rem($id, $val)
{
return Redis::zrem(self::CACHE_KEY . $id, $val);
}
public static function count($id)
{
return Redis::zcard(self::CACHE_KEY . $id);
}
public static function warmCache($id, $force = false, $limit = 100, $returnIds = false)
{
if(self::count($id) == 0 || $force == true) {
Redis::del(self::CACHE_KEY . $id);
$following = Cache::remember('profile:following:'.$id, 1209600, function() use($id) {
$following = Follower::whereProfileId($id)->pluck('following_id');
return $following->push($id)->toArray();
});
$minId = SnowflakeService::byDate(now()->subMonths(6));
$filters = UserFilterService::filters($id);
if($filters && count($filters)) {
$following = array_diff($following, $filters);
}
$domainBlocks = UserDomainBlock::whereProfileId($id)->pluck('domain')->toArray();
$ids = Status::where('id', '>', $minId)
->whereIn('profile_id', $following)
2023-11-11 12:46:37 +00:00
->whereNull(['in_reply_to_id', 'reblog_of_id'])
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
->whereIn('visibility',['public', 'unlisted', 'private'])
->orderByDesc('id')
->limit($limit)
->pluck('id');
foreach($ids as $pid) {
$status = StatusService::get($pid, false);
if(!$status || !isset($status['account'], $status['url'])) {
continue;
}
if($domainBlocks && count($domainBlocks)) {
$domain = strtolower(parse_url($status['url'], PHP_URL_HOST));
if(in_array($domain, $domainBlocks)) {
continue;
}
}
2023-11-11 12:46:37 +00:00
self::add($id, $pid);
}
return $returnIds ? $ids : 1;
}
return 0;
}
}