Note.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 Note 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. array_push($mentions, $reply);
  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://w3id.org/security/v1',
  50. 'https://www.w3.org/ns/activitystreams',
  51. [
  52. 'Hashtag' => 'as:Hashtag',
  53. 'sensitive' => 'as:sensitive',
  54. 'schema' => 'http://schema.org/',
  55. 'pixelfed' => 'http://pixelfed.org/ns#',
  56. 'commentsEnabled' => [
  57. '@id' => 'pixelfed:commentsEnabled',
  58. '@type' => 'schema:Boolean'
  59. ],
  60. 'capabilities' => [
  61. '@id' => 'pixelfed:capabilities',
  62. '@container' => '@set'
  63. ],
  64. 'announce' => [
  65. '@id' => 'pixelfed:canAnnounce',
  66. '@type' => '@id'
  67. ],
  68. 'like' => [
  69. '@id' => 'pixelfed:canLike',
  70. '@type' => '@id'
  71. ],
  72. 'reply' => [
  73. '@id' => 'pixelfed:canReply',
  74. '@type' => '@id'
  75. ],
  76. 'toot' => 'http://joinmastodon.org/ns#',
  77. 'Emoji' => 'toot:Emoji',
  78. 'blurhash' => 'toot:blurhash',
  79. ]
  80. ],
  81. 'id' => $status->url(),
  82. 'type' => 'Note',
  83. 'summary' => $status->is_nsfw ? $status->cw_summary : null,
  84. 'content' => $status->rendered ?? $status->caption,
  85. 'inReplyTo' => $status->in_reply_to_id ? $status->parent()->url() : null,
  86. 'published' => $status->created_at->toAtomString(),
  87. 'url' => $status->url(),
  88. 'attributedTo' => $status->profile->permalink(),
  89. 'to' => $status->scopeToAudience('to'),
  90. 'cc' => $status->scopeToAudience('cc'),
  91. 'sensitive' => (bool) $status->is_nsfw,
  92. 'attachment' => $status->media()->orderBy('order')->get()->map(function ($media) {
  93. $res = [
  94. 'type' => $media->activityVerb(),
  95. 'mediaType' => $media->mime,
  96. 'url' => $media->url(),
  97. 'name' => $media->caption,
  98. ];
  99. if($media->blurhash) {
  100. $res['blurhash'] = $media->blurhash;
  101. }
  102. if($media->width) {
  103. $res['width'] = $media->width;
  104. }
  105. if($media->height) {
  106. $res['height'] = $media->height;
  107. }
  108. return $res;
  109. })->toArray(),
  110. 'tag' => $tags,
  111. 'commentsEnabled' => (bool) !$status->comments_disabled,
  112. 'capabilities' => [
  113. 'announce' => 'https://www.w3.org/ns/activitystreams#Public',
  114. 'like' => 'https://www.w3.org/ns/activitystreams#Public',
  115. 'reply' => $status->comments_disabled == true ? '[]' : 'https://www.w3.org/ns/activitystreams#Public'
  116. ],
  117. 'location' => $status->place_id ? [
  118. 'type' => 'Place',
  119. 'name' => $status->place->name,
  120. 'longitude' => $status->place->long,
  121. 'latitude' => $status->place->lat,
  122. 'country' => $status->place->country
  123. ] : null,
  124. ];
  125. }
  126. }