Note.php 2.9 KB

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