StatusTransformer.php 3.1 KB

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