StatusTransformer.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Transformer\ActivityPub;
  3. use App\Services\MediaService;
  4. use App\Services\StatusService;
  5. use App\Status;
  6. use App\Util\Lexer\Autolink;
  7. use League\Fractal;
  8. class StatusTransformer extends Fractal\TransformerAbstract
  9. {
  10. public function transform(Status $status)
  11. {
  12. $content = $status->caption ? nl2br(Autolink::create()->autolink($status->caption)) : '';
  13. $inReplyTo = null;
  14. if ($status->in_reply_to_id) {
  15. $reply = StatusService::get($status->in_reply_to_id, true);
  16. if ($reply && isset($reply['url'])) {
  17. $inReplyTo = $reply['url'];
  18. }
  19. }
  20. return [
  21. '@context' => [
  22. 'https://www.w3.org/ns/activitystreams',
  23. 'https://w3id.org/security/v1',
  24. [
  25. 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
  26. 'featured' => [
  27. 'https://pixelfed.org/ns#featured' => ['@type' => '@id'],
  28. ],
  29. ],
  30. ],
  31. 'id' => $status->url(),
  32. 'type' => 'Note',
  33. 'summary' => null,
  34. 'content' => $content,
  35. 'inReplyTo' => $inReplyTo,
  36. 'published' => $status->created_at->toAtomString(),
  37. 'url' => $status->url(),
  38. 'attributedTo' => $status->profile->permalink(),
  39. 'to' => [
  40. 'https://www.w3.org/ns/activitystreams#Public',
  41. ],
  42. 'cc' => [
  43. $status->profile->permalink('/followers'),
  44. ],
  45. 'sensitive' => (bool) $status->is_nsfw,
  46. 'attachment' => MediaService::activitypub($status->id),
  47. 'tag' => [],
  48. 'location' => $status->place_id ? [
  49. 'type' => 'Place',
  50. 'name' => $status->place->name,
  51. 'longitude' => $status->place->long,
  52. 'latitude' => $status->place->lat,
  53. 'country' => $status->place->country,
  54. ] : null,
  55. ];
  56. }
  57. }