CreateNote.php 4.9 KB

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