StatusStatelessTransformer.php 3.0 KB

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