AccountTransformer.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Transformer\Api;
  3. use App\Profile;
  4. use App\Services\AccountService;
  5. use App\Services\PronounService;
  6. use App\User;
  7. use App\UserSetting;
  8. use Cache;
  9. use League\Fractal;
  10. class AccountTransformer extends Fractal\TransformerAbstract
  11. {
  12. protected $defaultIncludes = [
  13. // 'relationship',
  14. ];
  15. public function transform(Profile $profile)
  16. {
  17. if (! $profile) {
  18. return [];
  19. }
  20. $adminIds = Cache::remember('pf:admin-ids', 604800, function () {
  21. return User::whereIsAdmin(true)->pluck('profile_id')->toArray();
  22. });
  23. $local = $profile->private_key != null;
  24. $local = $profile->user_id && $profile->private_key != null;
  25. $hideFollowing = false;
  26. $hideFollowers = false;
  27. if ($local) {
  28. $hideFollowing = Cache::remember('pf:acct-trans:hideFollowing:'.$profile->id, 2592000, function () use ($profile) {
  29. $settings = UserSetting::whereUserId($profile->user_id)->first();
  30. if (! $settings) {
  31. return false;
  32. }
  33. return $settings->show_profile_following_count == false;
  34. });
  35. $hideFollowers = Cache::remember('pf:acct-trans:hideFollowers:'.$profile->id, 2592000, function () use ($profile) {
  36. $settings = UserSetting::whereUserId($profile->user_id)->first();
  37. if (! $settings) {
  38. return false;
  39. }
  40. return $settings->show_profile_follower_count == false;
  41. });
  42. }
  43. $is_admin = ! $local ? false : in_array($profile->id, $adminIds);
  44. $acct = $local ? $profile->username : substr($profile->username, 1);
  45. $username = $local ? $profile->username : explode('@', $acct)[0];
  46. $res = [
  47. 'id' => (string) $profile->id,
  48. 'username' => $username,
  49. 'acct' => $acct,
  50. 'display_name' => $profile->name,
  51. 'discoverable' => true,
  52. 'locked' => (bool) $profile->is_private,
  53. 'followers_count' => $hideFollowers ? 0 : (int) $profile->followers_count,
  54. 'following_count' => $hideFollowing ? 0 : (int) $profile->following_count,
  55. 'statuses_count' => (int) $profile->status_count,
  56. 'note' => $profile->bio ?? '',
  57. 'note_text' => $profile->bio ? strip_tags($profile->bio) : null,
  58. 'url' => $profile->url(),
  59. 'avatar' => $profile->avatarUrl(),
  60. 'website' => $profile->website,
  61. 'local' => (bool) $local,
  62. 'is_admin' => (bool) $is_admin,
  63. 'created_at' => $profile->created_at->toJSON(),
  64. 'header_bg' => $profile->header_bg,
  65. 'last_fetched_at' => optional($profile->last_fetched_at)->toJSON(),
  66. 'pronouns' => PronounService::get($profile->id),
  67. 'location' => $profile->location,
  68. ];
  69. if ($profile->moved_to_profile_id) {
  70. $mt = AccountService::getMastodon($profile->moved_to_profile_id, true);
  71. if ($mt) {
  72. $res['moved'] = $mt;
  73. }
  74. }
  75. return $res;
  76. }
  77. protected function includeRelationship(Profile $profile)
  78. {
  79. return $this->item($profile, new RelationshipTransformer());
  80. }
  81. }