StatusStatelessTransformer.php 3.4 KB

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