StatusStatelessTransformer.php 3.2 KB

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