mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-22 14:31:26 +00:00
Add new api transformers
This commit is contained in:
parent
8e27eb4b4d
commit
9b130cfe9b
6 changed files with 241 additions and 0 deletions
42
app/Transformer/Api/Mastodon/v1/AccountTransformer.php
Normal file
42
app/Transformer/Api/Mastodon/v1/AccountTransformer.php
Normal 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,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
17
app/Transformer/Api/Mastodon/v1/HashtagTransformer.php
Normal file
17
app/Transformer/Api/Mastodon/v1/HashtagTransformer.php
Normal 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(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
23
app/Transformer/Api/Mastodon/v1/MediaTransformer.php
Normal file
23
app/Transformer/Api/Mastodon/v1/MediaTransformer.php
Normal 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
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
19
app/Transformer/Api/Mastodon/v1/MentionTransformer.php
Normal file
19
app/Transformer/Api/Mastodon/v1/MentionTransformer.php
Normal 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,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
59
app/Transformer/Api/Mastodon/v1/NotificationTransformer.php
Normal file
59
app/Transformer/Api/Mastodon/v1/NotificationTransformer.php
Normal 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];
|
||||||
|
}
|
||||||
|
}
|
81
app/Transformer/Api/Mastodon/v1/StatusTransformer.php
Normal file
81
app/Transformer/Api/Mastodon/v1/StatusTransformer.php
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue