refactor: use awaitAll to reduce duplication (#8791)
* refactor: use awaitAll to reduce duplication * fix lint * fix typo
This commit is contained in:
parent
aea2f01ef7
commit
0fa2a52fac
1 changed files with 25 additions and 61 deletions
|
@ -1,6 +1,7 @@
|
||||||
import define from '../../define.js';
|
import define from '../../define.js';
|
||||||
import { ApiError } from '../../error.js';
|
import { ApiError } from '../../error.js';
|
||||||
import { DriveFiles, Followings, NoteFavorites, NoteReactions, Notes, PageLikes, PollVotes, Users } from '@/models/index.js';
|
import { DriveFiles, Followings, NoteFavorites, NoteReactions, Notes, PageLikes, PollVotes, Users } from '@/models/index.js';
|
||||||
|
import { awaitAll } from '@/prelude/await-all.js';
|
||||||
|
|
||||||
export const meta = {
|
export const meta = {
|
||||||
tags: ['users'],
|
tags: ['users'],
|
||||||
|
@ -31,109 +32,72 @@ export default define(meta, paramDef, async (ps, me) => {
|
||||||
throw new ApiError(meta.errors.noSuchUser);
|
throw new ApiError(meta.errors.noSuchUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
const [
|
const result = await awaitAll({
|
||||||
notesCount,
|
notesCount: Notes.createQueryBuilder('note')
|
||||||
repliesCount,
|
|
||||||
renotesCount,
|
|
||||||
repliedCount,
|
|
||||||
renotedCount,
|
|
||||||
pollVotesCount,
|
|
||||||
pollVotedCount,
|
|
||||||
localFollowingCount,
|
|
||||||
remoteFollowingCount,
|
|
||||||
localFollowersCount,
|
|
||||||
remoteFollowersCount,
|
|
||||||
sentReactionsCount,
|
|
||||||
receivedReactionsCount,
|
|
||||||
noteFavoritesCount,
|
|
||||||
pageLikesCount,
|
|
||||||
pageLikedCount,
|
|
||||||
driveFilesCount,
|
|
||||||
driveUsage,
|
|
||||||
] = await Promise.all([
|
|
||||||
Notes.createQueryBuilder('note')
|
|
||||||
.where('note.userId = :userId', { userId: user.id })
|
.where('note.userId = :userId', { userId: user.id })
|
||||||
.getCount(),
|
.getCount(),
|
||||||
Notes.createQueryBuilder('note')
|
repliesCount: Notes.createQueryBuilder('note')
|
||||||
.where('note.userId = :userId', { userId: user.id })
|
.where('note.userId = :userId', { userId: user.id })
|
||||||
.andWhere('note.replyId IS NOT NULL')
|
.andWhere('note.replyId IS NOT NULL')
|
||||||
.getCount(),
|
.getCount(),
|
||||||
Notes.createQueryBuilder('note')
|
renotesCount: Notes.createQueryBuilder('note')
|
||||||
.where('note.userId = :userId', { userId: user.id })
|
.where('note.userId = :userId', { userId: user.id })
|
||||||
.andWhere('note.renoteId IS NOT NULL')
|
.andWhere('note.renoteId IS NOT NULL')
|
||||||
.getCount(),
|
.getCount(),
|
||||||
Notes.createQueryBuilder('note')
|
repliedCount: Notes.createQueryBuilder('note')
|
||||||
.where('note.replyUserId = :userId', { userId: user.id })
|
.where('note.replyUserId = :userId', { userId: user.id })
|
||||||
.getCount(),
|
.getCount(),
|
||||||
Notes.createQueryBuilder('note')
|
renotedCount: Notes.createQueryBuilder('note')
|
||||||
.where('note.renoteUserId = :userId', { userId: user.id })
|
.where('note.renoteUserId = :userId', { userId: user.id })
|
||||||
.getCount(),
|
.getCount(),
|
||||||
PollVotes.createQueryBuilder('vote')
|
pollVotesCount: PollVotes.createQueryBuilder('vote')
|
||||||
.where('vote.userId = :userId', { userId: user.id })
|
.where('vote.userId = :userId', { userId: user.id })
|
||||||
.getCount(),
|
.getCount(),
|
||||||
PollVotes.createQueryBuilder('vote')
|
pollVotedCount: PollVotes.createQueryBuilder('vote')
|
||||||
.innerJoin('vote.note', 'note')
|
.innerJoin('vote.note', 'note')
|
||||||
.where('note.userId = :userId', { userId: user.id })
|
.where('note.userId = :userId', { userId: user.id })
|
||||||
.getCount(),
|
.getCount(),
|
||||||
Followings.createQueryBuilder('following')
|
localFollowingCount: Followings.createQueryBuilder('following')
|
||||||
.where('following.followerId = :userId', { userId: user.id })
|
.where('following.followerId = :userId', { userId: user.id })
|
||||||
.andWhere('following.followeeHost IS NULL')
|
.andWhere('following.followeeHost IS NULL')
|
||||||
.getCount(),
|
.getCount(),
|
||||||
Followings.createQueryBuilder('following')
|
remoteFollowingCount: Followings.createQueryBuilder('following')
|
||||||
.where('following.followerId = :userId', { userId: user.id })
|
.where('following.followerId = :userId', { userId: user.id })
|
||||||
.andWhere('following.followeeHost IS NOT NULL')
|
.andWhere('following.followeeHost IS NOT NULL')
|
||||||
.getCount(),
|
.getCount(),
|
||||||
Followings.createQueryBuilder('following')
|
localFollowersCount: Followings.createQueryBuilder('following')
|
||||||
.where('following.followeeId = :userId', { userId: user.id })
|
.where('following.followeeId = :userId', { userId: user.id })
|
||||||
.andWhere('following.followerHost IS NULL')
|
.andWhere('following.followerHost IS NULL')
|
||||||
.getCount(),
|
.getCount(),
|
||||||
Followings.createQueryBuilder('following')
|
remoteFollowersCount: Followings.createQueryBuilder('following')
|
||||||
.where('following.followeeId = :userId', { userId: user.id })
|
.where('following.followeeId = :userId', { userId: user.id })
|
||||||
.andWhere('following.followerHost IS NOT NULL')
|
.andWhere('following.followerHost IS NOT NULL')
|
||||||
.getCount(),
|
.getCount(),
|
||||||
NoteReactions.createQueryBuilder('reaction')
|
sentReactionsCount: NoteReactions.createQueryBuilder('reaction')
|
||||||
.where('reaction.userId = :userId', { userId: user.id })
|
.where('reaction.userId = :userId', { userId: user.id })
|
||||||
.getCount(),
|
.getCount(),
|
||||||
NoteReactions.createQueryBuilder('reaction')
|
receivedReactionsCount: NoteReactions.createQueryBuilder('reaction')
|
||||||
.innerJoin('reaction.note', 'note')
|
.innerJoin('reaction.note', 'note')
|
||||||
.where('note.userId = :userId', { userId: user.id })
|
.where('note.userId = :userId', { userId: user.id })
|
||||||
.getCount(),
|
.getCount(),
|
||||||
NoteFavorites.createQueryBuilder('favorite')
|
noteFavoritesCount: NoteFavorites.createQueryBuilder('favorite')
|
||||||
.where('favorite.userId = :userId', { userId: user.id })
|
.where('favorite.userId = :userId', { userId: user.id })
|
||||||
.getCount(),
|
.getCount(),
|
||||||
PageLikes.createQueryBuilder('like')
|
pageLikesCount: PageLikes.createQueryBuilder('like')
|
||||||
.where('like.userId = :userId', { userId: user.id })
|
.where('like.userId = :userId', { userId: user.id })
|
||||||
.getCount(),
|
.getCount(),
|
||||||
PageLikes.createQueryBuilder('like')
|
pageLikedCount: PageLikes.createQueryBuilder('like')
|
||||||
.innerJoin('like.page', 'page')
|
.innerJoin('like.page', 'page')
|
||||||
.where('page.userId = :userId', { userId: user.id })
|
.where('page.userId = :userId', { userId: user.id })
|
||||||
.getCount(),
|
.getCount(),
|
||||||
DriveFiles.createQueryBuilder('file')
|
driveFilesCount: DriveFiles.createQueryBuilder('file')
|
||||||
.where('file.userId = :userId', { userId: user.id })
|
.where('file.userId = :userId', { userId: user.id })
|
||||||
.getCount(),
|
.getCount(),
|
||||||
DriveFiles.calcDriveUsageOf(user),
|
driveUsage: DriveFiles.calcDriveUsageOf(user),
|
||||||
]);
|
});
|
||||||
|
|
||||||
return {
|
result.followingCount = result.localFollowingCount + result.remoteFollowingCount;
|
||||||
notesCount,
|
result.followersCount = result.localFollowersCount + result.remoteFollowersCount;
|
||||||
repliesCount,
|
|
||||||
renotesCount,
|
return result;
|
||||||
repliedCount,
|
|
||||||
renotedCount,
|
|
||||||
pollVotesCount,
|
|
||||||
pollVotedCount,
|
|
||||||
localFollowingCount,
|
|
||||||
remoteFollowingCount,
|
|
||||||
localFollowersCount,
|
|
||||||
remoteFollowersCount,
|
|
||||||
followingCount: localFollowingCount + remoteFollowingCount,
|
|
||||||
followersCount: localFollowersCount + remoteFollowersCount,
|
|
||||||
sentReactionsCount,
|
|
||||||
receivedReactionsCount,
|
|
||||||
noteFavoritesCount,
|
|
||||||
pageLikesCount,
|
|
||||||
pageLikedCount,
|
|
||||||
driveFilesCount,
|
|
||||||
driveUsage,
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue