AccountTransformer.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Transformer\Api;
  3. use Auth;
  4. use Cache;
  5. use App\Profile;
  6. use App\User;
  7. use League\Fractal;
  8. use App\Services\PronounService;
  9. class AccountTransformer extends Fractal\TransformerAbstract
  10. {
  11. protected $defaultIncludes = [
  12. // 'relationship',
  13. ];
  14. public function transform(Profile $profile)
  15. {
  16. if(!$profile) {
  17. return [];
  18. }
  19. $adminIds = Cache::remember('pf:admin-ids', 604800, function() {
  20. return User::whereIsAdmin(true)->pluck('profile_id')->toArray();
  21. });
  22. $local = $profile->private_key != null;
  23. $is_admin = !$local ? false : in_array($profile->id, $adminIds);
  24. $acct = $local ? $profile->username : substr($profile->username, 1);
  25. $username = $local ? $profile->username : explode('@', $acct)[0];
  26. return [
  27. 'id' => (string) $profile->id,
  28. 'username' => $username,
  29. 'acct' => $acct,
  30. 'display_name' => $profile->name,
  31. 'discoverable' => true,
  32. 'locked' => (bool) $profile->is_private,
  33. 'followers_count' => (int) $profile->followers_count,
  34. 'following_count' => (int) $profile->following_count,
  35. 'statuses_count' => (int) $profile->status_count,
  36. 'note' => $profile->bio ?? '',
  37. 'note_text' => $profile->bio ? strip_tags($profile->bio) : null,
  38. 'url' => $profile->url(),
  39. 'avatar' => $profile->avatarUrl(),
  40. 'website' => $profile->website,
  41. 'local' => (bool) $local,
  42. 'is_admin' => (bool) $is_admin,
  43. 'created_at' => $profile->created_at->toJSON(),
  44. 'header_bg' => $profile->header_bg,
  45. 'last_fetched_at' => optional($profile->last_fetched_at)->toJSON(),
  46. 'pronouns' => PronounService::get($profile->id),
  47. 'location' => $profile->location
  48. ];
  49. }
  50. protected function includeRelationship(Profile $profile)
  51. {
  52. return $this->item($profile, new RelationshipTransformer());
  53. }
  54. }