Skip to content

Commit

Permalink
Update AccountTransformer, fix follower/following count visibility bug
Browse files Browse the repository at this point in the history
  • Loading branch information
dansup committed Feb 29, 2024
1 parent 402a460 commit 542d110
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
2 changes: 2 additions & 0 deletions app/Http/Controllers/Api/ApiV1Controller.php
Expand Up @@ -409,6 +409,7 @@ public function accountUpdateCredentials(Request $request)
if($settings->show_profile_follower_count != $show_profile_follower_count) {
$settings->show_profile_follower_count = $show_profile_follower_count;
$changes = true;
Cache::forget('pf:acct-trans:hideFollowers:' . $profile->id);
}
}

Expand All @@ -417,6 +418,7 @@ public function accountUpdateCredentials(Request $request)
if($settings->show_profile_following_count != $show_profile_following_count) {
$settings->show_profile_following_count = $show_profile_following_count;
$changes = true;
Cache::forget('pf:acct-trans:hideFollowing:' . $profile->id);
}
}

Expand Down
17 changes: 10 additions & 7 deletions app/Http/Controllers/Settings/PrivacySettings.php
Expand Up @@ -84,14 +84,17 @@ public function privacyStore(Request $request)
}
$settings->save();
}
Cache::forget('profile:settings:' . $profile->id);
$pid = $profile->id;
Cache::forget('profile:settings:' . $pid);
Cache::forget('user:account:id:' . $profile->user_id);
Cache::forget('profile:follower_count:' . $profile->id);
Cache::forget('profile:following_count:' . $profile->id);
Cache::forget('profile:atom:enabled:' . $profile->id);
Cache::forget('profile:embed:' . $profile->id);
Cache::forget('pf:acct:settings:hidden-followers:' . $profile->id);
Cache::forget('pf:acct:settings:hidden-following:' . $profile->id);
Cache::forget('profile:follower_count:' . $pid);
Cache::forget('profile:following_count:' . $pid);
Cache::forget('profile:atom:enabled:' . $pid);
Cache::forget('profile:embed:' . $pid);
Cache::forget('pf:acct:settings:hidden-followers:' . $pid);
Cache::forget('pf:acct:settings:hidden-following:' . $pid);
Cache::forget('pf:acct-trans:hideFollowing:' . $pid);
Cache::forget('pf:acct-trans:hideFollowers:' . $pid);
return redirect(route('settings.privacy'))->with('status', 'Settings successfully updated!');
}

Expand Down
30 changes: 25 additions & 5 deletions app/Transformer/Api/AccountTransformer.php
Expand Up @@ -6,14 +6,15 @@
use Cache;
use App\Profile;
use App\User;
use App\UserSetting;
use League\Fractal;
use App\Services\PronounService;

class AccountTransformer extends Fractal\TransformerAbstract
{
protected $defaultIncludes = [
// 'relationship',
];
protected $defaultIncludes = [
// 'relationship',
];

public function transform(Profile $profile)
{
Expand All @@ -26,6 +27,25 @@ public function transform(Profile $profile)
});

$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;
}
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];
Expand All @@ -36,8 +56,8 @@ public function transform(Profile $profile)
'display_name' => $profile->name,
'discoverable' => true,
'locked' => (bool) $profile->is_private,
'followers_count' => (int) $profile->followers_count,
'following_count' => (int) $profile->following_count,
'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,
Expand Down

0 comments on commit 542d110

Please sign in to comment.