1
0

Question.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Transformer\ActivityPub\Verb;
  3. use App\Status;
  4. use League\Fractal;
  5. use Illuminate\Support\Str;
  6. class Question 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. $hashtags = $status->hashtags->map(function ($hashtag) {
  22. return [
  23. 'type' => 'Hashtag',
  24. 'href' => $hashtag->url(),
  25. 'name' => "#{$hashtag->name}",
  26. ];
  27. })->toArray();
  28. $tags = array_merge($mentions, $hashtags);
  29. return [
  30. '@context' => [
  31. 'https://w3id.org/security/v1',
  32. 'https://www.w3.org/ns/activitystreams',
  33. [
  34. 'Hashtag' => 'as:Hashtag',
  35. 'sensitive' => 'as:sensitive',
  36. 'schema' => 'http://schema.org/',
  37. 'pixelfed' => 'http://pixelfed.org/ns#',
  38. 'commentsEnabled' => [
  39. '@id' => 'pixelfed:commentsEnabled',
  40. '@type' => 'schema:Boolean'
  41. ],
  42. 'capabilities' => [
  43. '@id' => 'pixelfed:capabilities',
  44. '@container' => '@set'
  45. ],
  46. 'announce' => [
  47. '@id' => 'pixelfed:canAnnounce',
  48. '@type' => '@id'
  49. ],
  50. 'like' => [
  51. '@id' => 'pixelfed:canLike',
  52. '@type' => '@id'
  53. ],
  54. 'reply' => [
  55. '@id' => 'pixelfed:canReply',
  56. '@type' => '@id'
  57. ],
  58. 'toot' => 'http://joinmastodon.org/ns#',
  59. 'Emoji' => 'toot:Emoji'
  60. ]
  61. ],
  62. 'id' => $status->url(),
  63. 'type' => 'Question',
  64. 'summary' => null,
  65. 'content' => $status->rendered ?? $status->caption,
  66. 'inReplyTo' => $status->in_reply_to_id ? $status->parent()->url() : null,
  67. 'published' => $status->created_at->toAtomString(),
  68. 'url' => $status->url(),
  69. 'attributedTo' => $status->profile->permalink(),
  70. 'to' => $status->scopeToAudience('to'),
  71. 'cc' => $status->scopeToAudience('cc'),
  72. 'sensitive' => (bool) $status->is_nsfw,
  73. 'attachment' => [],
  74. 'tag' => $tags,
  75. 'commentsEnabled' => (bool) !$status->comments_disabled,
  76. 'capabilities' => [
  77. 'announce' => 'https://www.w3.org/ns/activitystreams#Public',
  78. 'like' => 'https://www.w3.org/ns/activitystreams#Public',
  79. 'reply' => $status->comments_disabled == true ? '[]' : 'https://www.w3.org/ns/activitystreams#Public'
  80. ],
  81. 'location' => $status->place_id ? [
  82. 'type' => 'Place',
  83. 'name' => $status->place->name,
  84. 'longitude' => $status->place->long,
  85. 'latitude' => $status->place->lat,
  86. 'country' => $status->place->country
  87. ] : null,
  88. 'endTime' => $status->poll->expires_at->toAtomString(),
  89. 'oneOf' => collect($status->poll->poll_options)->map(function($option, $index) use($status) {
  90. return [
  91. 'type' => 'Note',
  92. 'name' => $option,
  93. 'replies' => [
  94. 'type' => 'Collection',
  95. 'totalItems' => $status->poll->cached_tallies[$index]
  96. ]
  97. ];
  98. })
  99. ];
  100. }
  101. }