diff --git a/app/src/follow/follow.service.ts b/app/src/follow/follow.service.ts index b0c36203..631ea9b0 100644 --- a/app/src/follow/follow.service.ts +++ b/app/src/follow/follow.service.ts @@ -4,7 +4,9 @@ import { FilterQuery, Model, SortOrder } from 'mongoose'; import { UserPreview } from 'src/common/models/common.user.model'; import { QueryArgs, + QueryOneArgs, findAllAndLean, + findOneAndLean, } from 'src/database/mongoose/database.mongoose.query'; import { CursorExtractor, @@ -34,6 +36,12 @@ export class FollowService { private readonly paginationCursorService: PaginationCursorService, ) {} + async findOneAndLean( + queryOneArgs?: QueryOneArgs, + ): Promise { + return await findOneAndLean(this.followModel, queryOneArgs); + } + async findAllAndLean(queryArgs?: QueryArgs): Promise { return await findAllAndLean(this.followModel, queryArgs); } @@ -118,10 +126,12 @@ export class FollowService { aggregate.match(filter); } - const follower = await aggregate - .match({ followId: targetId }) - .sort(followSort(sortOrder)) - .limit(limit); + const follower: Pick[] = await this.findAllAndLean({ + filter: { followId: targetId }, + select: { _id: 0, userId: 1 }, + sort: followSort(sortOrder), + limit, + }); const followerUserPreview = await Promise.all( follower.map(async (follower) => { @@ -156,10 +166,14 @@ export class FollowService { const [id, _login]: FollowListCursorField = this.paginationCursorService.toFields(after, fieldExtractor); - const [followAt] = await aggregate.match({ - userId: id, - followId: targetId, - }); + const followAt: Pick | null = + await this.findOneAndLean({ + filter: { + userId: id, + followId: targetId, + }, + select: { _id: 0, followAt: 1 }, + }); if (!followAt) { return this.generateEmptyPage(); @@ -206,10 +220,12 @@ export class FollowService { aggregate.match(filter); } - const following = await aggregate - .match({ userId: targetId }) - .sort(followSort(sortOrder)) - .limit(limit); + const following: Pick[] = await this.findAllAndLean({ + filter: { userId: targetId }, + select: { _id: 0, followId: 1 }, + sort: followSort(sortOrder), + limit, + }); const followingUserPreview = await Promise.all( following.map(async (following) => { @@ -244,10 +260,14 @@ export class FollowService { const [id, _login]: FollowListCursorField = this.paginationCursorService.toFields(after, fieldExtractor); - const [followAt] = await aggregate.match({ - userId: targetId, - followId: id, - }); + const followAt: Pick | null = + await this.findOneAndLean({ + filter: { + userId: targetId, + followId: id, + }, + select: { _id: 0, followAt: 1 }, + }); if (!followAt) { return this.generateEmptyPage();