StatusTransformer.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Transformer\Api;
  3. use App\Models\CustomEmoji;
  4. use App\Services\BookmarkService;
  5. use App\Services\HashidService;
  6. use App\Services\LikeService;
  7. use App\Services\MediaService;
  8. use App\Services\MediaTagService;
  9. use App\Services\PollService;
  10. use App\Services\ProfileService;
  11. use App\Services\StatusHashtagService;
  12. use App\Services\StatusLabelService;
  13. use App\Services\StatusMentionService;
  14. use App\Services\StatusService;
  15. use App\Status;
  16. use App\Util\Lexer\Autolink;
  17. use League\Fractal;
  18. class StatusTransformer extends Fractal\TransformerAbstract
  19. {
  20. public function transform(Status $status)
  21. {
  22. $pid = request()->user()->profile_id;
  23. $taggedPeople = MediaTagService::get($status->id);
  24. $poll = $status->type === 'poll' ? PollService::get($status->id, $pid) : null;
  25. $content = $status->caption ? nl2br(Autolink::create()->autolink($status->caption)) : '';
  26. return [
  27. '_v' => 1,
  28. 'id' => (string) $status->id,
  29. 'shortcode' => HashidService::encode($status->id),
  30. 'uri' => $status->url(),
  31. 'url' => $status->url(),
  32. 'in_reply_to_id' => $status->in_reply_to_id ? (string) $status->in_reply_to_id : null,
  33. 'in_reply_to_account_id' => $status->in_reply_to_profile_id ? (string) $status->in_reply_to_profile_id : null,
  34. 'reblog' => $status->reblog_of_id ? StatusService::get($status->reblog_of_id) : null,
  35. 'content' => $content,
  36. 'content_text' => $status->caption,
  37. 'created_at' => str_replace('+00:00', 'Z', $status->created_at->format(DATE_RFC3339_EXTENDED)),
  38. 'emojis' => CustomEmoji::scan($status->caption),
  39. 'reblogs_count' => 0,
  40. 'favourites_count' => $status->likes_count ?? 0,
  41. 'reblogged' => $status->shared(),
  42. 'favourited' => $status->liked(),
  43. 'muted' => null,
  44. 'sensitive' => (bool) $status->is_nsfw,
  45. 'spoiler_text' => $status->cw_summary ?? '',
  46. 'visibility' => $status->scope ?? $status->visibility,
  47. 'application' => [
  48. 'name' => 'web',
  49. 'website' => null,
  50. ],
  51. 'language' => null,
  52. 'mentions' => StatusMentionService::get($status->id),
  53. 'pf_type' => $status->type ?? $status->setType(),
  54. 'reply_count' => (int) $status->reply_count,
  55. 'comments_disabled' => (bool) $status->comments_disabled,
  56. 'thread' => false,
  57. 'replies' => [],
  58. 'parent' => [],
  59. 'place' => $status->place,
  60. 'local' => (bool) $status->local,
  61. 'taggedPeople' => $taggedPeople,
  62. 'label' => StatusLabelService::get($status),
  63. 'liked_by' => LikeService::likedBy($status),
  64. 'media_attachments' => MediaService::get($status->id),
  65. 'account' => ProfileService::get($status->profile_id, true),
  66. 'tags' => StatusHashtagService::statusTags($status->id),
  67. 'poll' => $poll,
  68. 'bookmarked' => BookmarkService::get($pid, $status->id),
  69. 'edited_at' => $status->edited_at ? str_replace('+00:00', 'Z', $status->edited_at->format(DATE_RFC3339_EXTENDED)) : null,
  70. 'pinned' => (bool) $status->pinned_order,
  71. ];
  72. }
  73. }