Update Services, fix mastoapi compat

This commit is contained in:
Daniel Supernault 2022-01-03 02:31:03 -07:00
parent 85fc9dd0a8
commit 454b4e21dd
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
2 changed files with 40 additions and 15 deletions

View file

@ -18,26 +18,48 @@ class MediaService
public static function get($statusId) public static function get($statusId)
{ {
$status = Status::find($statusId); return Cache::remember(self::CACHE_KEY.$statusId, 86400, function() use($statusId) {
if(!$status) { $media = Media::whereStatusId($statusId)->orderBy('order')->get();
if(!$media) {
return []; return [];
} }
$ttl = $status->created_at->lt(now()->subMinutes(30)) ? 129600 : 30;
return Cache::remember(self::CACHE_KEY.$statusId, $ttl, function() use($status) {
if(!$status) {
return [];
}
if(in_array($status->type, ['group:post', 'photo', 'video', 'video:album', 'photo:album', 'loop', 'photo:video:album'])) {
$media = Media::whereStatusId($status->id)->orderBy('order')->get();
$fractal = new Fractal\Manager(); $fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer()); $fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Collection($media, new MediaTransformer()); $resource = new Fractal\Resource\Collection($media, new MediaTransformer());
return $fractal->createData($resource)->toArray(); return $fractal->createData($resource)->toArray();
}
return [];
}); });
} }
public static function getMastodon($id)
{
$media = self::get($id);
if(!$media) {
return [];
}
$medias = collect($media)
->map(function($media) {
$mime = $media['mime'] ? explode('/', $media['mime']) : false;
unset(
$media['optimized_url'],
$media['license'],
$media['is_nsfw'],
$media['orientation'],
$media['filter_name'],
$media['filter_class'],
$media['mime']
);
$media['type'] = $mime ? strtolower($mime[0]) : 'unknown';
return $media;
})
->filter(function($m) {
return $m && isset($m['url']);
})
->values();
return $medias->toArray();
}
public static function del($statusId) public static function del($statusId)
{ {
return Cache::forget(self::CACHE_KEY . $statusId); return Cache::forget(self::CACHE_KEY . $statusId);

View file

@ -71,6 +71,7 @@ class StatusService
$status['account']['note_text'], $status['account']['note_text'],
$status['account']['pronouns'], $status['account']['pronouns'],
$status['account']['website'], $status['account']['website'],
$status['media_attachments'],
); );
$status['account']['avatar_static'] = $status['account']['avatar']; $status['account']['avatar_static'] = $status['account']['avatar'];
$status['account']['bot'] = false; $status['account']['bot'] = false;
@ -80,6 +81,8 @@ class StatusService
$status['account']['header_static'] = url('/storage/headers/missing.png'); $status['account']['header_static'] = url('/storage/headers/missing.png');
$status['account']['last_status_at'] = null; $status['account']['last_status_at'] = null;
$status['media_attachments'] = array_values(MediaService::getMastodon($status['id']));
return $status; return $status;
} }