123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- namespace App\Transformer\ActivityPub\Verb;
- use App\Status;
- use League\Fractal;
- use App\Models\CustomEmoji;
- use Illuminate\Support\Str;
- class CreateNote extends Fractal\TransformerAbstract
- {
- public function transform(Status $status)
- {
- $mentions = $status->mentions->map(function ($mention) {
- $webfinger = $mention->emailUrl();
- $name = Str::startsWith($webfinger, '@') ?
- $webfinger :
- '@' . $webfinger;
- return [
- 'type' => 'Mention',
- 'href' => $mention->permalink(),
- 'name' => $name
- ];
- })->toArray();
- if($status->in_reply_to_id != null) {
- $parent = $status->parent()->profile;
- if($parent) {
- $webfinger = $parent->emailUrl();
- $name = Str::startsWith($webfinger, '@') ?
- $webfinger :
- '@' . $webfinger;
- $reply = [
- 'type' => 'Mention',
- 'href' => $parent->permalink(),
- 'name' => $name
- ];
- $mentions = array_merge($reply, $mentions);
- }
- }
- $hashtags = $status->hashtags->map(function ($hashtag) {
- return [
- 'type' => 'Hashtag',
- 'href' => $hashtag->url(),
- 'name' => "#{$hashtag->name}",
- ];
- })->toArray();
- $emojis = CustomEmoji::scan($status->caption, true) ?? [];
- $emoji = array_merge($emojis, $mentions);
- $tags = array_merge($emoji, $hashtags);
- return [
- '@context' => [
- 'https://www.w3.org/ns/activitystreams',
- 'https://w3id.org/security/v1',
- [
- 'sc' => 'http://schema.org#',
- 'Hashtag' => 'as:Hashtag',
- 'sensitive' => 'as:sensitive',
- 'commentsEnabled' => 'sc:Boolean',
- 'capabilities' => [
- 'announce' => ['@type' => '@id'],
- 'like' => ['@type' => '@id'],
- 'reply' => ['@type' => '@id']
- ],
- 'toot' => 'http://joinmastodon.org/ns#',
- 'Emoji' => 'toot:Emoji'
- ]
- ],
- 'id' => $status->permalink(),
- 'type' => 'Create',
- 'actor' => $status->profile->permalink(),
- 'published' => $status->created_at->toAtomString(),
- 'to' => $status->scopeToAudience('to'),
- 'cc' => $status->scopeToAudience('cc'),
- 'object' => [
- 'id' => $status->url(),
- 'type' => 'Note',
- 'summary' => null,
- 'content' => $status->rendered ?? $status->caption,
- 'inReplyTo' => $status->in_reply_to_id ? $status->parent()->url() : null,
- 'published' => $status->created_at->toAtomString(),
- 'url' => $status->url(),
- 'attributedTo' => $status->profile->permalink(),
- 'to' => $status->scopeToAudience('to'),
- 'cc' => $status->scopeToAudience('cc'),
- 'sensitive' => (bool) $status->is_nsfw,
- 'attachment' => $status->media()->orderBy('order')->get()->map(function ($media) {
- return [
- 'type' => $media->activityVerb(),
- 'mediaType' => $media->mime,
- 'url' => $media->url(),
- 'name' => $media->caption,
- ];
- })->toArray(),
- 'tag' => $tags,
- 'commentsEnabled' => (bool) !$status->comments_disabled,
- 'capabilities' => [
- 'announce' => 'https://www.w3.org/ns/activitystreams#Public',
- 'like' => 'https://www.w3.org/ns/activitystreams#Public',
- 'reply' => $status->comments_disabled == true ? null : 'https://www.w3.org/ns/activitystreams#Public'
- ],
- 'location' => $status->place_id ? [
- 'type' => 'Place',
- 'name' => $status->place->name,
- 'longitude' => $status->place->long,
- 'latitude' => $status->place->lat,
- 'country' => $status->place->country
- ] : null,
- ]
- ];
- }
- }
|