StatusTransformer.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Transformer\Api;
  3. use App\Status;
  4. use League\Fractal;
  5. use Cache;
  6. use App\Services\HashidService;
  7. use App\Services\MediaTagService;
  8. use App\Services\StatusLabelService;
  9. use Illuminate\Support\Str;
  10. class StatusTransformer extends Fractal\TransformerAbstract
  11. {
  12. protected $defaultIncludes = [
  13. 'account',
  14. 'media_attachments',
  15. ];
  16. public function transform(Status $status)
  17. {
  18. $taggedPeople = MediaTagService::get($status->id);
  19. return [
  20. 'id' => (string) $status->id,
  21. 'shortcode' => HashidService::encode($status->id),
  22. 'uri' => $status->url(),
  23. 'url' => $status->url(),
  24. 'in_reply_to_id' => (string) $status->in_reply_to_id,
  25. 'in_reply_to_account_id' => (string) $status->in_reply_to_profile_id,
  26. 'reblog' => null,
  27. 'content' => $status->rendered ?? $status->caption,
  28. 'content_text' => $status->caption,
  29. 'created_at' => $status->created_at->format('c'),
  30. 'emojis' => [],
  31. 'reblogs_count' => $status->reblogs_count ?? 0,
  32. 'favourites_count' => $status->likes_count ?? 0,
  33. 'reblogged' => $status->shared(),
  34. 'favourited' => $status->liked(),
  35. 'muted' => null,
  36. 'sensitive' => (bool) $status->is_nsfw,
  37. 'spoiler_text' => $status->cw_summary ?? '',
  38. 'visibility' => $status->visibility ?? $status->scope,
  39. 'application' => [
  40. 'name' => 'web',
  41. 'website' => null
  42. ],
  43. 'language' => null,
  44. 'pinned' => null,
  45. 'mentions' => [],
  46. 'tags' => [],
  47. 'pf_type' => $status->type ?? $status->setType(),
  48. 'reply_count' => (int) $status->reply_count,
  49. 'comments_disabled' => $status->comments_disabled ? true : false,
  50. 'thread' => false,
  51. 'replies' => [],
  52. 'parent' => [],
  53. 'place' => $status->place,
  54. 'local' => (bool) $status->local,
  55. 'taggedPeople' => $taggedPeople,
  56. 'label' => StatusLabelService::get($status)
  57. ];
  58. }
  59. public function includeAccount(Status $status)
  60. {
  61. $account = $status->profile;
  62. return $this->item($account, new AccountTransformer());
  63. }
  64. public function includeMediaAttachments(Status $status)
  65. {
  66. return Cache::remember('status:transformer:media:attachments:'.$status->id, now()->addMinutes(14), function() use($status) {
  67. if(in_array($status->type, ['photo', 'video', 'video:album', 'photo:album', 'loop', 'photo:video:album'])) {
  68. $media = $status->media()->orderBy('order')->get();
  69. return $this->collection($media, new MediaTransformer());
  70. }
  71. });
  72. }
  73. }