diff --git a/packages/backend/src/server/api/endpoints/users/stats.ts b/packages/backend/src/server/api/endpoints/users/stats.ts index 970308f78..7197219fb 100644 --- a/packages/backend/src/server/api/endpoints/users/stats.ts +++ b/packages/backend/src/server/api/endpoints/users/stats.ts @@ -46,27 +46,27 @@ export const meta = { }, localFollowingCount: { type: 'integer', - optional: false, nullable: false, + optional: true, nullable: false, }, remoteFollowingCount: { type: 'integer', - optional: false, nullable: false, + optional: true, nullable: false, }, localFollowersCount: { type: 'integer', - optional: false, nullable: false, + optional: true, nullable: false, }, remoteFollowersCount: { type: 'integer', - optional: false, nullable: false, + optional: true, nullable: false, }, followingCount: { type: 'integer', - optional: false, nullable: false, + optional: true, nullable: false, }, followersCount: { type: 'integer', - optional: false, nullable: false, + optional: true, nullable: false, }, sentReactionsCount: { type: 'integer', @@ -110,7 +110,7 @@ export const paramDef = { } as const; // eslint-disable-next-line import/no-default-export -export default define(meta, paramDef, async (ps) => { +export default define(meta, paramDef, async (ps, me) => { const user = await Users.findOneBy({ id: ps.userId }); if (user == null) { throw new ApiError('NO_SUCH_USER'); @@ -141,22 +141,6 @@ export default define(meta, paramDef, async (ps) => { .innerJoin('vote.note', 'note') .where('note.userId = :userId', { userId: user.id }) .getCount(), - localFollowingCount: Followings.createQueryBuilder('following') - .where('following.followerId = :userId', { userId: user.id }) - .andWhere('following.followeeHost IS NULL') - .getCount(), - remoteFollowingCount: Followings.createQueryBuilder('following') - .where('following.followerId = :userId', { userId: user.id }) - .andWhere('following.followeeHost IS NOT NULL') - .getCount(), - localFollowersCount: Followings.createQueryBuilder('following') - .where('following.followeeId = :userId', { userId: user.id }) - .andWhere('following.followerHost IS NULL') - .getCount(), - remoteFollowersCount: Followings.createQueryBuilder('following') - .where('following.followeeId = :userId', { userId: user.id }) - .andWhere('following.followerHost IS NOT NULL') - .getCount(), sentReactionsCount: NoteReactions.createQueryBuilder('reaction') .where('reaction.userId = :userId', { userId: user.id }) .getCount(), @@ -180,8 +164,32 @@ export default define(meta, paramDef, async (ps) => { driveUsage: DriveFiles.calcDriveUsageOf(user.id), }); - result.followingCount = result.localFollowingCount + result.remoteFollowingCount; - result.followersCount = result.localFollowersCount + result.remoteFollowersCount; + const ffVisible = await Users.areFollowersVisibleTo(user, me); + if (ffVisible) { + const follows = await awaitAll({ + localFollowingCount: Followings.createQueryBuilder('following') + .where('following.followerId = :userId', { userId: user.id }) + .andWhere('following.followeeHost IS NULL') + .getCount(), + remoteFollowingCount: Followings.createQueryBuilder('following') + .where('following.followerId = :userId', { userId: user.id }) + .andWhere('following.followeeHost IS NOT NULL') + .getCount(), + localFollowersCount: Followings.createQueryBuilder('following') + .where('following.followeeId = :userId', { userId: user.id }) + .andWhere('following.followerHost IS NULL') + .getCount(), + remoteFollowersCount: Followings.createQueryBuilder('following') + .where('following.followeeId = :userId', { userId: user.id }) + .andWhere('following.followerHost IS NOT NULL') + .getCount(), + }); + + Object.assign(result, follows); + + result.followingCount = result.localFollowingCount + result.remoteFollowingCount; + result.followersCount = result.localFollowersCount + result.remoteFollowersCount; + } return result; });