diff --git a/app/Services/MediaService.php b/app/Services/MediaService.php index 42f188eba..6960af5f1 100644 --- a/app/Services/MediaService.php +++ b/app/Services/MediaService.php @@ -18,26 +18,48 @@ class MediaService public static function get($statusId) { - $status = Status::find($statusId); - if(!$status) { - 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 Cache::remember(self::CACHE_KEY.$statusId, 86400, function() use($statusId) { + $media = Media::whereStatusId($statusId)->orderBy('order')->get(); + if(!$media) { 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->setSerializer(new ArraySerializer()); - $resource = new Fractal\Resource\Collection($media, new MediaTransformer()); - return $fractal->createData($resource)->toArray(); - } - return []; + $fractal = new Fractal\Manager(); + $fractal->setSerializer(new ArraySerializer()); + $resource = new Fractal\Resource\Collection($media, new MediaTransformer()); + return $fractal->createData($resource)->toArray(); }); } + 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) { return Cache::forget(self::CACHE_KEY . $statusId); diff --git a/app/Services/StatusService.php b/app/Services/StatusService.php index d272e478a..3532c8733 100644 --- a/app/Services/StatusService.php +++ b/app/Services/StatusService.php @@ -71,6 +71,7 @@ class StatusService $status['account']['note_text'], $status['account']['pronouns'], $status['account']['website'], + $status['media_attachments'], ); $status['account']['avatar_static'] = $status['account']['avatar']; $status['account']['bot'] = false; @@ -80,6 +81,8 @@ class StatusService $status['account']['header_static'] = url('/storage/headers/missing.png'); $status['account']['last_status_at'] = null; + $status['media_attachments'] = array_values(MediaService::getMastodon($status['id'])); + return $status; }