CreateNote.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Transformer\ActivityPub\Verb;
  3. use App\Status;
  4. use League\Fractal;
  5. use App\Models\CustomEmoji;
  6. use Illuminate\Support\Str;
  7. class CreateNote extends Fractal\TransformerAbstract
  8. {
  9. public function transform(Status $status)
  10. {
  11. $mentions = $status->mentions->map(function ($mention) {
  12. $webfinger = $mention->emailUrl();
  13. $name = Str::startsWith($webfinger, '@') ?
  14. $webfinger :
  15. '@' . $webfinger;
  16. return [
  17. 'type' => 'Mention',
  18. 'href' => $mention->permalink(),
  19. 'name' => $name
  20. ];
  21. })->toArray();
  22. if($status->in_reply_to_id != null) {
  23. $parent = $status->parent()->profile;
  24. if($parent) {
  25. $webfinger = $parent->emailUrl();
  26. $name = Str::startsWith($webfinger, '@') ?
  27. $webfinger :
  28. '@' . $webfinger;
  29. $reply = [
  30. 'type' => 'Mention',
  31. 'href' => $parent->permalink(),
  32. 'name' => $name
  33. ];
  34. $mentions = array_merge($reply, $mentions);
  35. }
  36. }
  37. $hashtags = $status->hashtags->map(function ($hashtag) {
  38. return [
  39. 'type' => 'Hashtag',
  40. 'href' => $hashtag->url(),
  41. 'name' => "#{$hashtag->name}",
  42. ];
  43. })->toArray();
  44. $emojis = CustomEmoji::scan($status->caption, true) ?? [];
  45. $emoji = array_merge($emojis, $mentions);
  46. $tags = array_merge($emoji, $hashtags);
  47. return [
  48. '@context' => [
  49. 'https://www.w3.org/ns/activitystreams',
  50. 'https://w3id.org/security/v1',
  51. [
  52. 'sc' => 'http://schema.org#',
  53. 'Hashtag' => 'as:Hashtag',
  54. 'sensitive' => 'as:sensitive',
  55. 'commentsEnabled' => 'sc:Boolean',
  56. 'capabilities' => [
  57. 'announce' => ['@type' => '@id'],
  58. 'like' => ['@type' => '@id'],
  59. 'reply' => ['@type' => '@id']
  60. ],
  61. 'toot' => 'http://joinmastodon.org/ns#',
  62. 'Emoji' => 'toot:Emoji'
  63. ]
  64. ],
  65. 'id' => $status->permalink(),
  66. 'type' => 'Create',
  67. 'actor' => $status->profile->permalink(),
  68. 'published' => $status->created_at->toAtomString(),
  69. 'to' => $status->scopeToAudience('to'),
  70. 'cc' => $status->scopeToAudience('cc'),
  71. 'object' => [
  72. 'id' => $status->url(),
  73. 'type' => 'Note',
  74. 'summary' => null,
  75. 'content' => $status->rendered ?? $status->caption,
  76. 'inReplyTo' => $status->in_reply_to_id ? $status->parent()->url() : null,
  77. 'published' => $status->created_at->toAtomString(),
  78. 'url' => $status->url(),
  79. 'attributedTo' => $status->profile->permalink(),
  80. 'to' => $status->scopeToAudience('to'),
  81. 'cc' => $status->scopeToAudience('cc'),
  82. 'sensitive' => (bool) $status->is_nsfw,
  83. 'attachment' => $status->media()->orderBy('order')->get()->map(function ($media) {
  84. return [
  85. 'type' => $media->activityVerb(),
  86. 'mediaType' => $media->mime,
  87. 'url' => $media->url(),
  88. 'name' => $media->caption,
  89. ];
  90. })->toArray(),
  91. 'tag' => $tags,
  92. 'commentsEnabled' => (bool) !$status->comments_disabled,
  93. 'capabilities' => [
  94. 'announce' => 'https://www.w3.org/ns/activitystreams#Public',
  95. 'like' => 'https://www.w3.org/ns/activitystreams#Public',
  96. 'reply' => $status->comments_disabled == true ? null : 'https://www.w3.org/ns/activitystreams#Public'
  97. ],
  98. 'location' => $status->place_id ? [
  99. 'type' => 'Place',
  100. 'name' => $status->place->name,
  101. 'longitude' => $status->place->long,
  102. 'latitude' => $status->place->lat,
  103. 'country' => $status->place->country
  104. ] : null,
  105. ]
  106. ];
  107. }
  108. }