Question.php 4.1 KB

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