diff --git a/packages/backend/src/core/QueryService.ts b/packages/backend/src/core/QueryService.ts index 12efe18f3c..ac608071f4 100644 --- a/packages/backend/src/core/QueryService.ts +++ b/packages/backend/src/core/QueryService.ts @@ -449,27 +449,33 @@ export class QueryService { /** * Adds OR condition that followerProp (user ID) is following followeeProp (user ID). * Both props should be expressions, not raw values. + * If withReplies is set to a boolean, then this method will only count followings with the matching withReplies value. */ @bindThis - public orFollowingUser(q: Q, followerProp: string, followeeProp: string): Q { - return this.addFollowingUser(q, followerProp, followeeProp, 'orWhere'); + public orFollowingUser(q: Q, followerProp: string, followeeProp: string, withReplies?: boolean): Q { + return this.addFollowingUser(q, followerProp, followeeProp, 'orWhere', withReplies); } /** * Adds AND condition that followerProp (user ID) is following followeeProp (user ID). * Both props should be expressions, not raw values. + * If withReplies is set to a boolean, then this method will only count followings with the matching withReplies value. */ @bindThis - public andFollowingUser(q: Q, followerProp: string, followeeProp: string): Q { - return this.addFollowingUser(q, followerProp, followeeProp, 'andWhere'); + public andFollowingUser(q: Q, followerProp: string, followeeProp: string, withReplies?: boolean): Q { + return this.addFollowingUser(q, followerProp, followeeProp, 'andWhere', withReplies); } - private addFollowingUser(q: Q, followerProp: string, followeeProp: string, join: 'andWhere' | 'orWhere'): Q { + private addFollowingUser(q: Q, followerProp: string, followeeProp: string, join: 'andWhere' | 'orWhere', withReplies?: boolean): Q { const followingQuery = this.followingsRepository.createQueryBuilder('following') .select('1') .andWhere(`following.followerId = ${followerProp}`) .andWhere(`following.followeeId = ${followeeProp}`); + if (withReplies !== undefined) { + followingQuery.andWhere('following.withReplies = :withReplies', { withReplies }); + } + return q[join](`EXISTS (${followingQuery.getQuery()})`, followingQuery.getParameters()); };