2018-07-12 16:42:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Transformer\Api;
|
|
|
|
|
|
|
|
use App\Profile;
|
2024-03-02 11:21:04 +00:00
|
|
|
use App\Services\AccountService;
|
|
|
|
use App\Services\PronounService;
|
2022-12-07 06:41:05 +00:00
|
|
|
use App\User;
|
2024-02-29 11:59:13 +00:00
|
|
|
use App\UserSetting;
|
2024-03-02 11:21:04 +00:00
|
|
|
use Cache;
|
2018-07-12 16:42:17 +00:00
|
|
|
use League\Fractal;
|
|
|
|
|
|
|
|
class AccountTransformer extends Fractal\TransformerAbstract
|
|
|
|
{
|
2024-03-02 11:21:04 +00:00
|
|
|
protected $defaultIncludes = [
|
|
|
|
// 'relationship',
|
|
|
|
];
|
|
|
|
|
|
|
|
public function transform(Profile $profile)
|
|
|
|
{
|
|
|
|
if (! $profile) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$adminIds = Cache::remember('pf:admin-ids', 604800, function () {
|
|
|
|
return User::whereIsAdmin(true)->pluck('profile_id')->toArray();
|
|
|
|
});
|
|
|
|
|
|
|
|
$local = $profile->private_key != null;
|
|
|
|
$local = $profile->user_id && $profile->private_key != null;
|
|
|
|
$hideFollowing = false;
|
|
|
|
$hideFollowers = false;
|
|
|
|
if ($local) {
|
|
|
|
$hideFollowing = Cache::remember('pf:acct-trans:hideFollowing:'.$profile->id, 2592000, function () use ($profile) {
|
|
|
|
$settings = UserSetting::whereUserId($profile->user_id)->first();
|
|
|
|
if (! $settings) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $settings->show_profile_following_count == false;
|
|
|
|
});
|
|
|
|
$hideFollowers = Cache::remember('pf:acct-trans:hideFollowers:'.$profile->id, 2592000, function () use ($profile) {
|
|
|
|
$settings = UserSetting::whereUserId($profile->user_id)->first();
|
|
|
|
if (! $settings) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-02 00:26:54 +00:00
|
|
|
|
2024-03-02 11:21:04 +00:00
|
|
|
return $settings->show_profile_follower_count == false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
$is_admin = ! $local ? false : in_array($profile->id, $adminIds);
|
|
|
|
$acct = $local ? $profile->username : substr($profile->username, 1);
|
|
|
|
$username = $local ? $profile->username : explode('@', $acct)[0];
|
|
|
|
$res = [
|
|
|
|
'id' => (string) $profile->id,
|
|
|
|
'username' => $username,
|
|
|
|
'acct' => $acct,
|
|
|
|
'display_name' => $profile->name,
|
|
|
|
'discoverable' => true,
|
|
|
|
'locked' => (bool) $profile->is_private,
|
|
|
|
'followers_count' => $hideFollowers ? 0 : (int) $profile->followers_count,
|
|
|
|
'following_count' => $hideFollowing ? 0 : (int) $profile->following_count,
|
|
|
|
'statuses_count' => (int) $profile->status_count,
|
|
|
|
'note' => $profile->bio ?? '',
|
|
|
|
'note_text' => $profile->bio ? strip_tags($profile->bio) : null,
|
|
|
|
'url' => $profile->url(),
|
|
|
|
'avatar' => $profile->avatarUrl(),
|
|
|
|
'website' => $profile->website,
|
|
|
|
'local' => (bool) $local,
|
|
|
|
'is_admin' => (bool) $is_admin,
|
|
|
|
'created_at' => $profile->created_at->toJSON(),
|
|
|
|
'header_bg' => $profile->header_bg,
|
|
|
|
'last_fetched_at' => optional($profile->last_fetched_at)->toJSON(),
|
|
|
|
'pronouns' => PronounService::get($profile->id),
|
|
|
|
'location' => $profile->location,
|
|
|
|
];
|
2022-12-07 06:41:05 +00:00
|
|
|
|
2024-03-02 11:21:04 +00:00
|
|
|
if ($profile->moved_to_profile_id) {
|
|
|
|
$mt = AccountService::getMastodon($profile->moved_to_profile_id, true);
|
|
|
|
if ($mt) {
|
|
|
|
$res['moved'] = $mt;
|
|
|
|
}
|
|
|
|
}
|
2022-12-07 06:41:05 +00:00
|
|
|
|
2024-03-02 11:21:04 +00:00
|
|
|
return $res;
|
|
|
|
}
|
2019-09-02 00:26:54 +00:00
|
|
|
|
2024-03-02 11:21:04 +00:00
|
|
|
protected function includeRelationship(Profile $profile)
|
|
|
|
{
|
|
|
|
return $this->item($profile, new RelationshipTransformer());
|
|
|
|
}
|
2018-08-28 03:07:36 +00:00
|
|
|
}
|