UpdateNote.php 4.0 KB

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