1
0
Fork 0

Add new api transformers

This commit is contained in:
Daniel Supernault 2019-09-26 01:30:15 -06:00
parent 8e27eb4b4d
commit 9b130cfe9b
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7
6 changed files with 241 additions and 0 deletions

View File

@ -0,0 +1,42 @@
<?php
namespace App\Transformer\Api\Mastodon\v1;
use App\Profile;
use League\Fractal;
class AccountTransformer extends Fractal\TransformerAbstract
{
public function transform(Profile $profile)
{
$local = $profile->domain == null;
$is_admin = !$local ? false : $profile->user->is_admin;
$acct = $local ? $profile->username : substr($profile->username, 1);
$username = $local ? $profile->username : explode('@', $acct)[0];
return [
'id' => (string) $profile->id,
'username' => $username,
'acct' => $acct,
'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' => (int) $profile->statusCount(),
'note' => $profile->bio ?? '',
'url' => $profile->url(),
'avatar' => $profile->avatarUrl(),
'avatar_static' => $profile->avatarUrl(),
'header' => '',
'header_static' => '',
'header_bg' => $profile->header_bg,
'emojis' => [],
'moved' => null,
'fields' => [],
'bot' => false,
'website' => $profile->website,
'software' => 'pixelfed',
'is_admin' => (bool) $is_admin,
];
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace App\Transformer\Api\Mastodon\v1;
use App\Hashtag;
use League\Fractal;
class HashtagTransformer extends Fractal\TransformerAbstract
{
public function transform(Hashtag $hashtag)
{
return [
'name' => $hashtag->name,
'url' => $hashtag->url(),
];
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace App\Transformer\Api\Mastodon\v1;
use App\Media;
use League\Fractal;
class MediaTransformer extends Fractal\TransformerAbstract
{
public function transform(Media $media)
{
return [
'id' => (string) $media->id,
'type' => lcfirst($media->activityVerb()),
'url' => $media->url(),
'remote_url' => null,
'preview_url' => $media->thumbnailUrl(),
'text_url' => null,
'meta' => null,
'description' => $media->caption
];
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace App\Transformer\Api\Mastodon\v1;
use App\Profile;
use League\Fractal;
class MentionTransformer extends Fractal\TransformerAbstract
{
public function transform(Profile $profile)
{
return [
'id' => (string) $profile->id,
'url' => $profile->url(),
'username' => $profile->username,
'acct' => $profile->username,
];
}
}

View File

@ -0,0 +1,59 @@
<?php
namespace App\Transformer\Api\Mastodon\v1;
use App\{
Notification,
Status
};
use League\Fractal;
class NotificationTransformer extends Fractal\TransformerAbstract
{
protected $defaultIncludes = [
'account',
'status',
];
public function transform(Notification $notification)
{
return [
'id' => (string) $notification->id,
'type' => $this->replaceTypeVerb($notification->action),
'created_at' => (string) $notification->created_at->format('c'),
];
}
public function includeAccount(Notification $notification)
{
return $this->item($notification->actor, new AccountTransformer());
}
public function includeStatus(Notification $notification)
{
$item = $notification;
if($item->item_id && $item->item_type == 'App\Status') {
$status = Status::with('media')->find($item->item_id);
if($status) {
return $this->item($status, new StatusTransformer());
} else {
return null;
}
} else {
return null;
}
}
public function replaceTypeVerb($verb)
{
$verbs = [
'follow' => 'follow',
'mention' => 'mention',
'reblog' => 'share',
'share' => 'share',
'like' => 'favourite',
'comment' => 'comment',
];
return $verbs[$verb];
}
}

View File

@ -0,0 +1,81 @@
<?php
namespace App\Transformer\Api\Mastodon\v1;
use App\Status;
use League\Fractal;
use Cache;
class StatusTransformer extends Fractal\TransformerAbstract
{
protected $defaultIncludes = [
'account',
'media_attachments',
'mentions',
'tags',
];
public function transform(Status $status)
{
return [
'id' => (string) $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,
'reblog' => null,
'content' => $status->rendered ?? $status->caption,
'created_at' => $status->created_at->format('c'),
'emojis' => [],
'reblogs_count' => $status->reblogs_count != 0 ? $status->reblogs_count: $status->shares()->count(),
'favourites_count' => $status->likes_count != 0 ? $status->likes_count: $status->likes()->count(),
'reblogged' => null,
'favourited' => null,
'muted' => null,
'sensitive' => (bool) $status->is_nsfw,
'spoiler_text' => $status->cw_summary ?? '',
'visibility' => $status->visibility ?? $status->scope,
'application' => [
'name' => 'web',
'website' => null
],
'language' => null,
'pinned' => null,
'mentions' => [],
'parent' => [],
'place' => $status->place
];
}
public function includeAccount(Status $status)
{
$account = $status->profile;
return $this->item($account, new AccountTransformer());
}
public function includeMediaAttachments(Status $status)
{
return Cache::remember('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());
}
});
}
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());
}
}