fix(api/users): PVランキングに誰もいない場合エラーになる問題を修正 (MisskeyIO#737)

This commit is contained in:
まっちゃとーにゅ 2024-09-17 13:21:28 +09:00 committed by GitHub
parent 8e9b6dc4d1
commit c82bf7583a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -73,10 +73,10 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
query.andWhere('user.host = :hostname', { hostname: ps.hostname.toLowerCase() }); query.andWhere('user.host = :hostname', { hostname: ps.hostname.toLowerCase() });
} }
let pvUsers: { userId: string; count: number; }[] | undefined = undefined; let pvRankedUsers: { userId: string; count: number; }[] | undefined = undefined;
if (ps.sort?.endsWith('pv')) { if (ps.sort?.endsWith('pv')) {
// 直近12時間のPVランキングを取得 // 直近12時間のPVランキングを取得
pvUsers = await this.perUserPvChart.getUsersRanking( pvRankedUsers = await this.perUserPvChart.getUsersRanking(
'hour', ps.sort.startsWith('+') ? 'DESC' : 'ASC', 'hour', ps.sort.startsWith('+') ? 'DESC' : 'ASC',
12, null, ps.limit, ps.offset, 12, null, ps.limit, ps.offset,
); );
@ -89,8 +89,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
case '-createdAt': query.orderBy('user.id', 'ASC'); break; case '-createdAt': query.orderBy('user.id', 'ASC'); break;
case '+updatedAt': query.andWhere('user.updatedAt IS NOT NULL').orderBy('user.updatedAt', 'DESC'); break; case '+updatedAt': query.andWhere('user.updatedAt IS NOT NULL').orderBy('user.updatedAt', 'DESC'); break;
case '-updatedAt': query.andWhere('user.updatedAt IS NOT NULL').orderBy('user.updatedAt', 'ASC'); break; case '-updatedAt': query.andWhere('user.updatedAt IS NOT NULL').orderBy('user.updatedAt', 'ASC'); break;
case '+pv': query.andWhere('user.id IN (:...userIds)', { userIds: pvUsers?.map(user => user.userId) ?? [] }); break; case '+pv': query.andWhere((pvRankedUsers?.length ?? 0) > 0 ? 'user.id IN (:...userIds)' : '1 = 0', { userIds: pvRankedUsers?.map(user => user.userId) ?? [] }); break;
case '-pv': query.andWhere('user.id IN (:...userIds)', { userIds: pvUsers?.map(user => user.userId) ?? [] }); break; case '-pv': query.andWhere((pvRankedUsers?.length ?? 0) > 0 ? 'user.id IN (:...userIds)' : '1 = 0', { userIds: pvRankedUsers?.map(user => user.userId) ?? [] }); break;
default: query.orderBy('user.id', 'ASC'); break; default: query.orderBy('user.id', 'ASC'); break;
} }
@ -103,14 +103,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
const users = await query.getMany(); const users = await query.getMany();
if (ps.sort === '+pv') { if (ps.sort === '+pv') {
users.sort((a, b) => { users.sort((a, b) => {
const aPv = pvUsers?.find(u => u.userId === a.id)?.count ?? 0; const aPv = pvRankedUsers?.find(u => u.userId === a.id)?.count ?? 0;
const bPv = pvUsers?.find(u => u.userId === b.id)?.count ?? 0; const bPv = pvRankedUsers?.find(u => u.userId === b.id)?.count ?? 0;
return bPv - aPv; return bPv - aPv;
}); });
} else if (ps.sort === '-pv') { } else if (ps.sort === '-pv') {
users.sort((a, b) => { users.sort((a, b) => {
const aPv = pvUsers?.find(u => u.userId === a.id)?.count ?? 0; const aPv = pvRankedUsers?.find(u => u.userId === a.id)?.count ?? 0;
const bPv = pvUsers?.find(u => u.userId === b.id)?.count ?? 0; const bPv = pvRankedUsers?.find(u => u.userId === b.id)?.count ?? 0;
return aPv - bPv; return aPv - bPv;
}); });
} }