From 005c8cffae0416b41952392f43440e11a9a7fd33 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Thu, 12 Jul 2018 10:42:17 -0600 Subject: [PATCH] Add API Transformers --- app/Transformer/Api/AccountTransformer.php | 33 +++++++++ .../Api/ApplicationTransformer.php | 16 +++++ app/Transformer/Api/HashtagTransformer.php | 18 +++++ app/Transformer/Api/MediaTransformer.php | 24 +++++++ app/Transformer/Api/MentionTransformer.php | 19 +++++ app/Transformer/Api/StatusTransformer.php | 69 +++++++++++++++++++ 6 files changed, 179 insertions(+) create mode 100644 app/Transformer/Api/AccountTransformer.php create mode 100644 app/Transformer/Api/ApplicationTransformer.php create mode 100644 app/Transformer/Api/HashtagTransformer.php create mode 100644 app/Transformer/Api/MediaTransformer.php create mode 100644 app/Transformer/Api/MentionTransformer.php create mode 100644 app/Transformer/Api/StatusTransformer.php diff --git a/app/Transformer/Api/AccountTransformer.php b/app/Transformer/Api/AccountTransformer.php new file mode 100644 index 000000000..1f95c8136 --- /dev/null +++ b/app/Transformer/Api/AccountTransformer.php @@ -0,0 +1,33 @@ + $profile->id, + 'username' => $profile->username, + 'acct' => $profile->username, + 'display_name' => $profile->name, + 'locked' => (bool) $profile->is_private, + 'created_at' => $profile->created_at->format('c'), + 'followers_count' => $profile->followerCount(), + 'following_count' => $profile->followingCount(), + 'statuses_count' => $profile->statusCount(), + 'note' => $profile->bio, + 'url' => $profile->url(), + 'avatar' => $profile->avatarUrl(), + 'avatar_static' => $profile->avatarUrl(), + 'header' => '', + 'header_static' => '', + 'moved' => null, + 'fields' => null, + 'bot' => null + ]; + } +} \ No newline at end of file diff --git a/app/Transformer/Api/ApplicationTransformer.php b/app/Transformer/Api/ApplicationTransformer.php new file mode 100644 index 000000000..a0fefcaa5 --- /dev/null +++ b/app/Transformer/Api/ApplicationTransformer.php @@ -0,0 +1,16 @@ + '', + 'website' => null + ]; + } +} \ No newline at end of file diff --git a/app/Transformer/Api/HashtagTransformer.php b/app/Transformer/Api/HashtagTransformer.php new file mode 100644 index 000000000..417cc9850 --- /dev/null +++ b/app/Transformer/Api/HashtagTransformer.php @@ -0,0 +1,18 @@ + $hashtag->name, + 'url' => $hashtag->url(), + ]; + } +} \ No newline at end of file diff --git a/app/Transformer/Api/MediaTransformer.php b/app/Transformer/Api/MediaTransformer.php new file mode 100644 index 000000000..959bae65b --- /dev/null +++ b/app/Transformer/Api/MediaTransformer.php @@ -0,0 +1,24 @@ + $media->id, + 'type' => 'image', + 'url' => $media->url(), + 'remote_url' => null, + 'preview_url' => $media->thumbnailUrl(), + 'text_url' => null, + 'meta' => null, + 'description' => null + ]; + } +} \ No newline at end of file diff --git a/app/Transformer/Api/MentionTransformer.php b/app/Transformer/Api/MentionTransformer.php new file mode 100644 index 000000000..1d0580afe --- /dev/null +++ b/app/Transformer/Api/MentionTransformer.php @@ -0,0 +1,19 @@ + $profile->id, + 'url' => $profile->url(), + 'username' => $profile->username, + 'acct' => $profile->username, + ]; + } +} \ No newline at end of file diff --git a/app/Transformer/Api/StatusTransformer.php b/app/Transformer/Api/StatusTransformer.php new file mode 100644 index 000000000..ad5129a91 --- /dev/null +++ b/app/Transformer/Api/StatusTransformer.php @@ -0,0 +1,69 @@ + $status->id, + 'uri' => $status->url(), + 'url' => $status->url(), + 'in_reply_to_id' => $status->in_reply_to_id, + 'in_reply_to_account_id' => $status->in_reply_to_profile_id, + + // TODO: fixme + 'reblog' => null, + + 'content' => "

$status->rendered

", + 'created_at' => $status->created_at->format('c'), + 'emojis' => [], + 'reblogs_count' => $status->shares()->count(), + 'favourites_count' => $status->likes()->count(), + 'reblogged' => $status->shared(), + 'favourited' => $status->liked(), + 'muted' => null, + 'sensitive' => (bool) $status->is_nsfw, + 'spoiler_text' => '', + 'visibility' => $status->visibility, + 'application' => null, + 'language' => null, + 'pinned' => null + ]; + } + + public function includeAccount(Status $status) + { + $account = $status->profile; + return $this->item($account, new AccountTransformer); + } + + public function includeMentions(Status $status) + { + $mentions = $status->mentions; + return $this->collection($mentions, new MentionTransformer); + } + + public function includeMediaAttachments(Status $status) + { + $media = $status->media; + return $this->collection($media, new MediaTransformer); + } + + public function includeTags(Status $status) + { + $tags = $status->hashtags; + return $this->collection($tags, new HashtagTransformer); + } +} \ No newline at end of file