AccountWithStatusesTransformer.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Transformer\Api;
  3. use Auth;
  4. use App\Profile;
  5. use League\Fractal;
  6. class AccountWithStatusesTransformer extends Fractal\TransformerAbstract
  7. {
  8. protected $defaultIncludes = [
  9. // 'relationship',
  10. 'posts',
  11. ];
  12. public function transform(Profile $profile)
  13. {
  14. $local = $profile->domain == null;
  15. $is_admin = !$local ? false : $profile->user->is_admin;
  16. $acct = $local ? $profile->username : substr($profile->username, 1);
  17. $username = $local ? $profile->username : explode('@', $acct)[0];
  18. return [
  19. 'id' => (string) $profile->id,
  20. 'username' => $username,
  21. 'acct' => $acct,
  22. 'display_name' => $profile->name,
  23. 'locked' => (bool) $profile->is_private,
  24. 'followers_count' => $profile->followerCount(),
  25. 'following_count' => $profile->followingCount(),
  26. 'statuses_count' => (int) $profile->statusCount(),
  27. 'note' => $profile->bio ?? '',
  28. 'url' => $profile->url(),
  29. 'avatar' => $profile->avatarUrl(),
  30. 'website' => $profile->website,
  31. 'local' => (bool) $local,
  32. 'is_admin' => (bool) $is_admin,
  33. 'created_at' => $profile->created_at->timestamp
  34. ];
  35. }
  36. protected function includePosts(Profile $profile)
  37. {
  38. $posts = $profile
  39. ->statuses()
  40. ->whereIsNsfw(false)
  41. ->whereType('photo')
  42. ->whereScope('public')
  43. ->whereNull('in_reply_to_id')
  44. ->whereNull('reblog_of_id')
  45. ->latest()
  46. ->take(5)
  47. ->get();
  48. return $this->collection($posts, new StatusStatelessTransformer());
  49. }
  50. }