Update StatusTransformer

pull/2895/head
Daniel Supernault 4 years ago
parent 09d5198c55
commit 1054b025b1
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7

@ -5,81 +5,58 @@ namespace App\Transformer\Api\Mastodon\v1;
use App\Status; use App\Status;
use League\Fractal; use League\Fractal;
use Cache; use Cache;
use App\Services\MediaService;
use App\Services\ProfileService;
use App\Services\StatusHashtagService;
class StatusTransformer extends Fractal\TransformerAbstract class StatusTransformer extends Fractal\TransformerAbstract
{ {
protected $defaultIncludes = [ protected $defaultIncludes = [
'account', 'mentions',
'media_attachments', ];
'mentions',
'tags', public function transform(Status $status)
]; {
return [
public function transform(Status $status) 'id' => (string) $status->id,
{ 'created_at' => $status->created_at->toJSON(),
return [ 'in_reply_to_id' => $status->in_reply_to_id ? (string) $status->in_reply_to_id : null,
'id' => (string) $status->id, 'in_reply_to_account_id' => $status->in_reply_to_profile_id,
'created_at' => $status->created_at->toJSON(), 'sensitive' => (bool) $status->is_nsfw,
'in_reply_to_id' => $status->in_reply_to_id ? (string) $status->in_reply_to_id : null, 'spoiler_text' => $status->cw_summary ?? '',
'in_reply_to_account_id' => $status->in_reply_to_profile_id, 'visibility' => $status->visibility ?? $status->scope,
'sensitive' => (bool) $status->is_nsfw, 'language' => 'en',
'spoiler_text' => $status->cw_summary ?? '', 'uri' => $status->url(),
'visibility' => $status->visibility ?? $status->scope, 'url' => $status->url(),
'language' => 'en', 'replies_count' => 0,
'uri' => $status->url(), 'reblogs_count' => $status->reblogs_count ?? 0,
'url' => $status->url(), 'favourites_count' => $status->likes_count ?? 0,
'replies_count' => 0, 'reblogged' => $status->shared(),
'reblogs_count' => $status->reblogs_count ?? 0, 'favourited' => $status->liked(),
'favourites_count' => $status->likes_count ?? 0, 'muted' => false,
'reblogged' => $status->shared(), 'bookmarked' => false,
'favourited' => $status->liked(), 'pinned' => false,
'muted' => false, 'content' => $status->rendered ?? $status->caption ?? '',
'bookmarked' => false, 'reblog' => null,
'pinned' => false, 'application' => [
'content' => $status->rendered ?? $status->caption ?? '', 'name' => 'web',
'reblog' => null, 'website' => null
'application' => [ ],
'name' => 'web', 'mentions' => [],
'website' => null 'tags' => [],
], 'emojis' => [],
'mentions' => [], 'card' => null,
'tags' => [], 'poll' => null,
'emojis' => [], 'media_attachments' => MediaService::get($status->id),
'card' => null, 'account' => ProfileService::get($status->profile_id),
'poll' => null, 'tags' => StatusHashtagService::statusTags($status->id)
]; ];
} }
public function includeAccount(Status $status) public function includeMentions(Status $status)
{ {
$account = $status->profile; $mentions = $status->mentions;
return $this->item($account, new AccountTransformer()); return $this->collection($mentions, new MentionTransformer());
} }
public function includeMediaAttachments(Status $status)
{
return Cache::remember('mastoapi:status:transformer:media:attachments:'.$status->id, now()->addDays(14), function() use($status) {
if(in_array($status->type, ['photo', 'video', 'photo:album', 'loop', 'photo:video:album'])) {
$media = $status->media()->orderBy('order')->get();
return $this->collection($media, new MediaTransformer());
} else {
return $this->collection([], new MediaTransformer());
}
});
}
public function includeMentions(Status $status)
{
$mentions = $status->mentions;
return $this->collection($mentions, new MentionTransformer());
}
public function includeTags(Status $status)
{
$hashtags = $status->hashtags;
return $this->collection($hashtags, new HashtagTransformer());
}
} }
Loading…
Cancel
Save