1
0
Fork 1
mirror of https://github.com/pixelfed/pixelfed.git synced 2024-12-29 11:15:09 +00:00
pixelfed/app/Services/CollectionService.php

149 lines
3.7 KiB
PHP
Raw Normal View History

2022-03-09 03:56:08 +00:00
<?php
namespace App\Services;
use App\Collection;
use App\CollectionItem;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
class CollectionService
{
const CACHE_KEY = 'pf:services:collections-v1:';
2022-03-09 03:56:08 +00:00
public static function getItems($id, $start = 0, $stop = 10)
{
if(self::count($id)) {
return Redis::zrangebyscore(self::CACHE_KEY . 'items:' . $id, $start, $stop);
}
return self::coldBootItems($id);
}
public static function addItem($id, $sid, $score)
{
2022-10-03 00:55:16 +00:00
return Redis::zadd(self::CACHE_KEY . 'items:' . $id, $score, $sid);
2022-03-09 03:56:08 +00:00
}
public static function removeItem($id, $sid)
{
2022-10-03 00:55:16 +00:00
return Redis::zrem(self::CACHE_KEY . 'items:' . $id, $sid);
2022-03-09 03:56:08 +00:00
}
public static function clearItems($id)
{
2022-10-03 00:55:16 +00:00
return Redis::del(self::CACHE_KEY . 'items:' . $id);
2022-03-09 03:56:08 +00:00
}
public static function coldBootItems($id)
{
2022-10-03 00:55:16 +00:00
return CollectionItem::whereCollectionId($id)
->orderBy('order')
->get()
->each(function($item) use ($id) {
return self::addItem($id, $item->object_id, $item->order);
})
->map(function($item) {
return (string) $item->object_id;
})
->values()
->toArray();
2022-03-09 03:56:08 +00:00
}
public static function count($id)
{
$count = Redis::zcard(self::CACHE_KEY . 'items:' . $id);
if(!$count) {
self::coldBootItems($id);
$count = Redis::zcard(self::CACHE_KEY . 'items:' . $id);
}
return $count;
}
public static function getCollection($id)
{
$collection = Cache::remember(self::CACHE_KEY . 'get:' . $id, 86400, function() use($id) {
$collection = Collection::find($id);
if(!$collection) {
return false;
}
$account = AccountService::get($collection->profile_id);
if(!$account) {
return false;
}
return [
'id' => (string) $collection->id,
'pid' => (string) $collection->profile_id,
'visibility' => $collection->visibility,
'title' => $collection->title,
'description' => $collection->description,
2022-10-01 05:48:11 +00:00
'thumb' => url('/storage/no-preview.png'),
2022-03-09 03:56:08 +00:00
'url' => $collection->url(),
'updated_at' => $collection->updated_at,
'published_at' => $collection->published_at,
2022-03-09 03:56:08 +00:00
];
});
if($collection) {
2022-10-01 05:48:11 +00:00
$account = AccountService::get($collection['pid']);
if(!$account) {
return false;
}
$collection['avatar'] = $account['avatar'];
$collection['username'] = $account['username'];
$collection['thumb'] = self::getThumb($id);
2022-03-09 03:56:08 +00:00
$collection['post_count'] = self::count($id);
}
return $collection;
}
public static function setCollection($id, $collection)
{
$account = AccountService::get($collection->profile_id);
if(!$account) {
return false;
}
$res = [
'id' => (string) $collection->id,
'pid' => (string) $collection->profile_id,
'visibility' => $collection->visibility,
'title' => $collection->title,
'description' => $collection->description,
'thumb' => self::getThumb($id),
'url' => $collection->url(),
'updated_at' => $collection->updated_at,
'published_at' => $collection->published_at,
2022-03-09 03:56:08 +00:00
];
Cache::put(self::CACHE_KEY . 'get:' . $id, $res, 86400);
2022-10-01 11:36:07 +00:00
$res['avatar'] = $account['avatar'];
$res['username'] = $account['username'];
2022-03-09 03:56:08 +00:00
$res['post_count'] = self::count($id);
return $res;
}
public static function deleteCollection($id)
{
Redis::del(self::CACHE_KEY . 'items:' . $id);
Cache::forget(self::CACHE_KEY . 'get:' . $id);
}
public static function getThumb($id)
{
$item = self::getItems($id, 0, 1);
if(!$item || empty($item)) {
2022-10-01 05:48:11 +00:00
return url('/storage/no-preview.png');
2022-03-09 03:56:08 +00:00
}
$status = StatusService::get($item[0]);
if(!$status) {
2022-10-01 05:48:11 +00:00
return url('/storage/no-preview.png');
2022-03-09 03:56:08 +00:00
}
if(!isset($status['media_attachments']) || empty($status['media_attachments'])) {
2022-10-01 05:48:11 +00:00
return url('/storage/no-preview.png');
2022-03-09 03:56:08 +00:00
}
return $status['media_attachments'][0]['url'];
}
}