Question.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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://www.w3.org/ns/activitystreams',
  32. 'https://w3id.org/security/v1',
  33. [
  34. 'sc' => 'http://schema.org#',
  35. 'Hashtag' => 'as:Hashtag',
  36. 'sensitive' => 'as:sensitive',
  37. 'commentsEnabled' => 'sc:Boolean',
  38. 'capabilities' => [
  39. 'announce' => ['@type' => '@id'],
  40. 'like' => ['@type' => '@id'],
  41. 'reply' => ['@type' => '@id']
  42. ]
  43. ]
  44. ],
  45. 'id' => $status->url(),
  46. 'type' => 'Question',
  47. 'summary' => null,
  48. 'content' => $status->rendered ?? $status->caption,
  49. 'inReplyTo' => $status->in_reply_to_id ? $status->parent()->url() : null,
  50. 'published' => $status->created_at->toAtomString(),
  51. 'url' => $status->url(),
  52. 'attributedTo' => $status->profile->permalink(),
  53. 'to' => $status->scopeToAudience('to'),
  54. 'cc' => $status->scopeToAudience('cc'),
  55. 'sensitive' => (bool) $status->is_nsfw,
  56. 'attachment' => [],
  57. 'tag' => $tags,
  58. 'commentsEnabled' => (bool) !$status->comments_disabled,
  59. 'capabilities' => [
  60. 'announce' => 'https://www.w3.org/ns/activitystreams#Public',
  61. 'like' => 'https://www.w3.org/ns/activitystreams#Public',
  62. 'reply' => $status->comments_disabled == true ? null : 'https://www.w3.org/ns/activitystreams#Public'
  63. ],
  64. 'location' => $status->place_id ? [
  65. 'type' => 'Place',
  66. 'name' => $status->place->name,
  67. 'longitude' => $status->place->long,
  68. 'latitude' => $status->place->lat,
  69. 'country' => $status->place->country
  70. ] : null,
  71. 'endTime' => $status->poll->expires_at->toAtomString(),
  72. 'oneOf' => collect($status->poll->poll_options)->map(function($option, $index) use($status) {
  73. return [
  74. 'type' => 'Note',
  75. 'name' => $option,
  76. 'replies' => [
  77. 'type' => 'Collection',
  78. 'totalItems' => $status->poll->cached_tallies[$index]
  79. ]
  80. ];
  81. })
  82. ];
  83. }
  84. }