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();
return []; if(!$media) {
}
$ttl = $status->created_at->lt(now()->subMinutes(30)) ? 129600 : 30;
return Cache::remember(self::CACHE_KEY.$statusId, $ttl, function() use($status) {
if(!$status) {
return []; return [];
} }
if(in_array($status->type, ['group:post', 'photo', 'video', 'video:album', 'photo:album', 'loop', 'photo:video:album'])) { $fractal = new Fractal\Manager();
$media = Media::whereStatusId($status->id)->orderBy('order')->get(); $fractal->setSerializer(new ArraySerializer());
$fractal = new Fractal\Manager(); $resource = new Fractal\Resource\Collection($media, new MediaTransformer());
$fractal->setSerializer(new ArraySerializer()); return $fractal->createData($resource)->toArray();
$resource = new Fractal\Resource\Collection($media, new MediaTransformer());
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;
} }