From 14a1367a8fd8fea8569cf5010138c9e0290a9e9f Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sun, 25 Jul 2021 03:17:49 -0600 Subject: [PATCH] Federate Media Licenses --- app/Services/MediaService.php | 21 +++++++++++++++++++ .../ActivityPub/StatusTransformer.php | 11 ++-------- app/Util/ActivityPub/Helpers.php | 5 +++++ 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/app/Services/MediaService.php b/app/Services/MediaService.php index f1230c7d8..5945e27cb 100644 --- a/app/Services/MediaService.php +++ b/app/Services/MediaService.php @@ -10,6 +10,7 @@ use League\Fractal; use League\Fractal\Serializer\ArraySerializer; use League\Fractal\Pagination\IlluminatePaginatorAdapter; use App\Transformer\Api\MediaTransformer; +use App\Util\Media\License; class MediaService { @@ -37,4 +38,24 @@ class MediaService { return Cache::forget(self::CACHE_KEY . $statusId); } + + public static function activitypub($statusId) + { + $status = self::get($statusId); + if(!$status) { + return []; + } + + return collect($status)->map(function($s) use($license) { + $license = isset($s['license']) && $s['license']['title'] ? $s['license']['title'] : null; + return [ + 'type' => 'Document', + 'mediaType' => $s['mime'], + 'url' => $s['url'], + 'name' => $s['description'], + 'blurhash' => $s['blurhash'], + 'license' => $license + ]; + }); + } } diff --git a/app/Transformer/ActivityPub/StatusTransformer.php b/app/Transformer/ActivityPub/StatusTransformer.php index 8368b64a8..f5d5ea531 100644 --- a/app/Transformer/ActivityPub/StatusTransformer.php +++ b/app/Transformer/ActivityPub/StatusTransformer.php @@ -4,6 +4,7 @@ namespace App\Transformer\ActivityPub; use App\Status; use League\Fractal; +use App\Services\MediaService; class StatusTransformer extends Fractal\TransformerAbstract { @@ -45,15 +46,7 @@ class StatusTransformer extends Fractal\TransformerAbstract 'sensitive' => (bool) $status->is_nsfw, 'atomUri' => $status->url(), 'inReplyToAtomUri' => null, - 'attachment' => $status->media->map(function ($media) { - return [ - 'type' => 'Document', - 'mediaType' => $media->mime, - 'url' => $media->url(), - 'name' => $media->caption, - 'blurhash' => $media->blurhash - ]; - }), + 'attachment' => MediaService::activitypub($status->id), 'tag' => [], 'location' => $status->place_id ? [ 'type' => 'Place', diff --git a/app/Util/ActivityPub/Helpers.php b/app/Util/ActivityPub/Helpers.php index f2e1adec5..bc2dd57b2 100644 --- a/app/Util/ActivityPub/Helpers.php +++ b/app/Util/ActivityPub/Helpers.php @@ -32,6 +32,7 @@ use App\Services\MediaPathService; use App\Services\MediaStorageService; use App\Jobs\MediaPipeline\MediaStoragePipeline; use App\Jobs\AvatarPipeline\RemoteAvatarFetch; +use App\Util\Media\License; class Helpers { @@ -428,6 +429,7 @@ class Helpers { $type = $media['mediaType']; $url = $media['url']; $blurhash = isset($media['blurhash']) ? $media['blurhash'] : null; + $license = isset($media['license']) ? License::nameToId($media['license']) : null; $valid = self::validateUrl($url); if(in_array($type, $allowed) == false || $valid == false) { continue; @@ -441,6 +443,9 @@ class Helpers { $media->user_id = null; $media->media_path = $url; $media->remote_url = $url; + if($license) { + $media->license = $license; + } $media->mime = $type; $media->version = 3; $media->save();