StatusTransformer.php 3.4 KB

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