From cbefbd2a33eefcada4be2554d3f2560ad094b5f5 Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Mon, 2 Jun 2025 16:58:54 -0400 Subject: refactor QueryService to use EXISTS instead of IN for most queries --- packages/backend/src/core/QueryService.ts | 370 ++++++++++++++++++------------ 1 file changed, 218 insertions(+), 152 deletions(-) (limited to 'packages/backend/src/core/QueryService.ts') diff --git a/packages/backend/src/core/QueryService.ts b/packages/backend/src/core/QueryService.ts index cf2419a9eb..548887959d 100644 --- a/packages/backend/src/core/QueryService.ts +++ b/packages/backend/src/core/QueryService.ts @@ -4,13 +4,13 @@ */ import { Inject, Injectable } from '@nestjs/common'; -import { Brackets, ObjectLiteral } from 'typeorm'; +import { Brackets, Not, WhereExpressionBuilder } from 'typeorm'; import { DI } from '@/di-symbols.js'; import type { MiUser } from '@/models/User.js'; -import type { UserProfilesRepository, FollowingsRepository, ChannelFollowingsRepository, BlockingsRepository, NoteThreadMutingsRepository, MutingsRepository, RenoteMutingsRepository, MiMeta } from '@/models/_.js'; +import type { UserProfilesRepository, FollowingsRepository, ChannelFollowingsRepository, BlockingsRepository, NoteThreadMutingsRepository, MutingsRepository, RenoteMutingsRepository, MiMeta, InstancesRepository, MiInstance } from '@/models/_.js'; import { bindThis } from '@/decorators.js'; import { IdService } from '@/core/IdService.js'; -import type { SelectQueryBuilder } from 'typeorm'; +import type { SelectQueryBuilder, FindOptionsWhere, ObjectLiteral } from 'typeorm'; @Injectable() export class QueryService { @@ -36,6 +36,9 @@ export class QueryService { @Inject(DI.renoteMutingsRepository) private renoteMutingsRepository: RenoteMutingsRepository, + @Inject(DI.instancesRepository) + private readonly instancesRepository: InstancesRepository, + @Inject(DI.meta) private meta: MiMeta, @@ -72,215 +75,278 @@ export class QueryService { // ここでいうBlockedは被Blockedの意 @bindThis - public generateBlockedUserQueryForNotes(q: SelectQueryBuilder, me: { id: MiUser['id'] }): void { - const blockingQuery = this.blockingsRepository.createQueryBuilder('blocking') - .select('blocking.blockerId') - .where('blocking.blockeeId = :blockeeId', { blockeeId: me.id }); - + public generateBlockedUserQueryForNotes(q: SelectQueryBuilder, me: { id: MiUser['id'] }): SelectQueryBuilder { // 投稿の作者にブロックされていない かつ // 投稿の返信先の作者にブロックされていない かつ // 投稿の引用元の作者にブロックされていない - q - .andWhere(`note.userId NOT IN (${ blockingQuery.getQuery() })`) + return this.excludeBlockingUser(q, 'note.userId', ':meId') .andWhere(new Brackets(qb => { - qb - .where('note.replyUserId IS NULL') - .orWhere(`note.replyUserId NOT IN (${ blockingQuery.getQuery() })`); + this.excludeBlockingUser(qb, 'note.replyUserId', ':meId') + .orWhere('note.replyUserId IS NULL'); })) .andWhere(new Brackets(qb => { - qb - .where('note.renoteUserId IS NULL') - .orWhere(`note.renoteUserId NOT IN (${ blockingQuery.getQuery() })`); - })); - - q.setParameters(blockingQuery.getParameters()); + this.excludeBlockingUser(qb, 'note.renoteUserId', ':meId') + .orWhere('note.renoteUserId IS NULL'); + })) + .setParameters({ meId: me.id }); } @bindThis - public generateBlockQueryForUsers(q: SelectQueryBuilder, me: { id: MiUser['id'] }): void { - const blockingQuery = this.blockingsRepository.createQueryBuilder('blocking') - .select('blocking.blockeeId') - .where('blocking.blockerId = :blockerId', { blockerId: me.id }); - - const blockedQuery = this.blockingsRepository.createQueryBuilder('blocking') - .select('blocking.blockerId') - .where('blocking.blockeeId = :blockeeId', { blockeeId: me.id }); - - q.andWhere(`user.id NOT IN (${ blockingQuery.getQuery() })`); - q.setParameters(blockingQuery.getParameters()); - - q.andWhere(`user.id NOT IN (${ blockedQuery.getQuery() })`); - q.setParameters(blockedQuery.getParameters()); + public generateBlockQueryForUsers(q: SelectQueryBuilder, me: { id: MiUser['id'] }): SelectQueryBuilder { + this.excludeBlockingUser(q, ':meId', 'user.id'); + this.excludeBlockingUser(q, 'user.id', ':me.id'); + return q.setParameters({ meId: me.id }); } @bindThis - public generateMutedNoteThreadQuery(q: SelectQueryBuilder, me: { id: MiUser['id'] }): void { - const mutedQuery = this.noteThreadMutingsRepository.createQueryBuilder('threadMuted') - .select('threadMuted.threadId') - .where('threadMuted.userId = :userId', { userId: me.id }); - - q.andWhere(`note.id NOT IN (${ mutedQuery.getQuery() })`); - q.andWhere(new Brackets(qb => { - qb - .where('note.threadId IS NULL') - .orWhere(`note.threadId NOT IN (${ mutedQuery.getQuery() })`); - })); - - q.setParameters(mutedQuery.getParameters()); + public generateMutedNoteThreadQuery(q: SelectQueryBuilder, me: { id: MiUser['id'] }): SelectQueryBuilder { + return this.excludeMutingThread(q, ':meId', 'note.id') + .andWhere(new Brackets(qb => { + this.excludeMutingThread(qb, ':meId', 'note.threadId') + .orWhere('note.threadId IS NULL'); + })) + .setParameters({ meId: me.id }); } @bindThis - public generateMutedUserQueryForNotes(q: SelectQueryBuilder, me: { id: MiUser['id'] }, exclude?: { id: MiUser['id'] }): void { - const mutingQuery = this.mutingsRepository.createQueryBuilder('muting') - .select('muting.muteeId') - .where('muting.muterId = :muterId', { muterId: me.id }); - - if (exclude) { - mutingQuery.andWhere('muting.muteeId != :excludeId', { excludeId: exclude.id }); - } - - const mutingInstanceQuery = this.userProfilesRepository.createQueryBuilder('user_profile') - .select('user_profile.mutedInstances') - .where('user_profile.userId = :muterId', { muterId: me.id }); - + public generateMutedUserQueryForNotes(q: SelectQueryBuilder, me: { id: MiUser['id'] }, exclude?: { id: MiUser['id'] }): SelectQueryBuilder { // 投稿の作者をミュートしていない かつ // 投稿の返信先の作者をミュートしていない かつ // 投稿の引用元の作者をミュートしていない - q - .andWhere(`note.userId NOT IN (${ mutingQuery.getQuery() })`) + this.excludeMutingUser(q, ':meId', 'note.userId', exclude) .andWhere(new Brackets(qb => { - qb - .where('note.replyUserId IS NULL') - .orWhere(`note.replyUserId NOT IN (${ mutingQuery.getQuery() })`); - })) - .andWhere(new Brackets(qb => { - qb - .where('note.renoteUserId IS NULL') - .orWhere(`note.renoteUserId NOT IN (${ mutingQuery.getQuery() })`); + this.excludeMutingUser(qb, ':meId', 'note.replyUserId', exclude) + .orWhere('note.replyUserId IS NULL'); })) - // mute instances .andWhere(new Brackets(qb => { - qb - .andWhere('note.userHost IS NULL') - .orWhere(`NOT ((${ mutingInstanceQuery.getQuery() })::jsonb ? note.userHost)`); - })) + this.excludeMutingUser(qb, ':meId', 'note.renoteUserId', exclude) + .orWhere('note.renoteUserId IS NULL'); + })); + + // mute instances + this.excludeMutingInstance(q, ':meId', 'note.userHost') .andWhere(new Brackets(qb => { - qb - .where('note.replyUserHost IS NULL') - .orWhere(`NOT ((${ mutingInstanceQuery.getQuery() })::jsonb ? note.replyUserHost)`); + this.excludeMutingInstance(qb, ':meId', 'note.replyUserHost') + .orWhere('note.replyUserHost IS NULL'); })) .andWhere(new Brackets(qb => { - qb - .where('note.renoteUserHost IS NULL') - .orWhere(`NOT ((${ mutingInstanceQuery.getQuery() })::jsonb ? note.renoteUserHost)`); + this.excludeMutingInstance(qb, ':meId', 'note.renoteUserHost') + .orWhere('note.renoteUserHost IS NULL'); })); - q.setParameters(mutingQuery.getParameters()); - q.setParameters(mutingInstanceQuery.getParameters()); + return q.setParameters({ meId: me.id }); } @bindThis - public generateMutedUserQueryForUsers(q: SelectQueryBuilder, me: { id: MiUser['id'] }): void { - const mutingQuery = this.mutingsRepository.createQueryBuilder('muting') - .select('muting.muteeId') - .where('muting.muterId = :muterId', { muterId: me.id }); - - q.andWhere(`user.id NOT IN (${ mutingQuery.getQuery() })`); - - q.setParameters(mutingQuery.getParameters()); + public generateMutedUserQueryForUsers(q: SelectQueryBuilder, me: { id: MiUser['id'] }): SelectQueryBuilder { + return this.excludeMutingUser(q, ':meId', 'user.id') + .setParameters({ meId: me.id }); } + // This intentionally skips isSuspended, isDeleted, makeNotesFollowersOnlyBefore, makeNotesHiddenBefore, and requireSigninToViewContents. + // NoteEntityService checks these automatically and calls hideNote() to hide them without breaking threads. + // For moderation purposes, you can set isSilenced to forcibly hide existing posts by a user. @bindThis - public generateVisibilityQuery(q: SelectQueryBuilder, me?: { id: MiUser['id'] } | null): void { + public generateVisibilityQuery(q: SelectQueryBuilder, me?: { id: MiUser['id'] } | null): SelectQueryBuilder { // This code must always be synchronized with the checks in Notes.isVisibleForMe. - if (me == null) { - q.andWhere(new Brackets(qb => { - qb - .where('note.visibility = \'public\'') - .orWhere('note.visibility = \'home\''); - })); - } else { - const followingQuery = this.followingsRepository.createQueryBuilder('following') - .select('following.followeeId') - .where('following.followerId = :meId'); + return q.andWhere(new Brackets(qb => { + // Public post + qb.orWhere('note.visibility = \'public\'') + .orWhere('note.visibility = \'home\''); - q.andWhere(new Brackets(qb => { + if (me != null) { qb - // 公開投稿である - .where(new Brackets(qb => { - qb - .where('note.visibility = \'public\'') - .orWhere('note.visibility = \'home\''); - })) - // または 自分自身 - .orWhere('note.userId = :meId') - // または 自分宛て - .orWhere(':meIdAsList <@ note.visibleUserIds') + // My post + .orWhere(':meId = note.userId') + // Reply to me + .orWhere(':meId = note.replyUserId') + // DM to me + .orWhere(':meId = ANY (note.visibleUserIds)') + // Mentions me + .orWhere(':meId = ANY (note.mentions)') + // Followers-only post .orWhere(new Brackets(qb => { - qb // または フォロワー宛ての投稿であり、 - .where('note.visibility = \'followers\'') - .andWhere(new Brackets(qb => { - qb - // 自分がフォロワーである - .where(`note.userId IN (${ followingQuery.getQuery() })`) - // または 自分の投稿へのリプライ - .orWhere('note.replyUserId = :meId') - .orWhere(':meIdAsList <@ note.mentions'); - })); + this.addFollowingUser(qb, ':meId', 'note.userId') + .andWhere('note.visibility = \'followers\''); })); - })); - q.setParameters({ meId: me.id, meIdAsList: [me.id] }); - } + q.setParameters({ meId: me.id }); + } + })); } @bindThis - public generateMutedUserRenotesQueryForNotes(q: SelectQueryBuilder, me: { id: MiUser['id'] }): void { - const mutingQuery = this.renoteMutingsRepository.createQueryBuilder('renote_muting') - .select('renote_muting.muteeId') - .where('renote_muting.muterId = :muterId', { muterId: me.id }); - - q.andWhere(new Brackets(qb => { - qb + public generateMutedUserRenotesQueryForNotes(q: SelectQueryBuilder, me: { id: MiUser['id'] }): SelectQueryBuilder { + return q.andWhere(new Brackets(qb => { + this.excludeMutingRenote(qb, ':meId', 'note.userId') .orWhere('note.renoteId IS NULL') .orWhere('note.text IS NOT NULL') .orWhere('note.cw IS NOT NULL') .orWhere('note.replyId IS NOT NULL') - .orWhere('note.hasPoll = false') - .orWhere('note.fileIds != \'{}\'') - .orWhere(`note.userId NOT IN (${ mutingQuery.getQuery() })`); - })); - - q.setParameters(mutingQuery.getParameters()); + .orWhere('note.hasPoll = true') + .orWhere('note.fileIds != \'{}\''); + })) + .setParameters({ meId: me.id }); } + // TODO replace allowSilenced with matchingHostQuery @bindThis - public generateBlockedHostQueryForNote(q: SelectQueryBuilder, excludeAuthor?: boolean, allowSilenced = true): void { - function checkFor(key: 'user' | 'replyUser' | 'renoteUser') { - q.leftJoin(`note.${key}Instance`, `${key}Instance`); + public generateBlockedHostQueryForNote(q: SelectQueryBuilder, excludeAuthor?: boolean, allowSilenced = true): SelectQueryBuilder { + const checkFor = (key: 'user' | 'replyUser' | 'renoteUser') => { q.andWhere(new Brackets(qb => { - qb.orWhere(`note.${key}Id IS NULL`) // no corresponding user - .orWhere(`note.${key}Host IS NULL`); // local + qb.orWhere(`note.${key}Host IS NULL`); // local - if (allowSilenced) { - qb.orWhere(`${key}Instance.isBlocked = false`); // not blocked - } else { - qb.orWhere(new Brackets(qbb => qbb - .andWhere(`${key}Instance.isBlocked = false`) // not blocked - .andWhere(`${key}Instance.isSilenced = false`))); // not silenced + if (key !== 'user') { + // note.userId always exists and is non-null + qb.orWhere(`note.${key}Id IS NULL`); // no corresponding user + + // note.userId always equals note.userId + if (excludeAuthor) { + qb.orWhere(`note.userId = note.${key}Id`); // author + } } - if (excludeAuthor) { - qb.orWhere(`note.userId = note.${key}Id`); // author + if (allowSilenced) { + // not blocked + this.excludeInstanceWhere(qb, `note.${key}Host`, { + isBlocked: false, + }, 'orWhere'); + } else { + // not blocked or silenced + this.excludeInstanceWhere(qb, `note.${key}Host`, { + isBlocked: false, + isSilenced: false, + }, 'orWhere'); } })); - } + }; if (!excludeAuthor) { checkFor('user'); } checkFor('replyUser'); checkFor('renoteUser'); + + return q; + } + + @bindThis + public generateMatchingHostQueryForNote(q: SelectQueryBuilder, filters: FindOptionsWhere | FindOptionsWhere[], hostProp = 'note.userHost'): SelectQueryBuilder { + return this.includeInstanceWhere(q, hostProp, filters); + } + + /** + * Adds condition that hostProp (instance host) matches the given filters. + * The prop should be an expression, not raw values. + */ + @bindThis + public includeInstanceWhere(q: Q, hostProp: string, filters: FindOptionsWhere | FindOptionsWhere[], join: 'andWhere' | 'orWhere' = 'andWhere'): Q { + const instancesQuery = this.instancesRepository.createQueryBuilder('instance') + .select('1') + .andWhere(`instance.host = ${hostProp}`) + .andWhere(filters); + + return q[join](`EXISTS (${instancesQuery.getQuery()})`, instancesQuery.getParameters()); + } + + /** + * Adds condition that hostProp (instance host) matches the given filters. + * The prop should be an expression, not raw values. + */ + @bindThis + public excludeInstanceWhere(q: Q, hostProp: string, filters: FindOptionsWhere | FindOptionsWhere[], join: 'andWhere' | 'orWhere' = 'andWhere'): Q { + const instancesQuery = this.instancesRepository.createQueryBuilder('instance') + .select('1') + .andWhere(`instance.host = ${hostProp}`) + .andWhere(filters); + + return q[join](`NOT EXISTS (${instancesQuery.getQuery()})`, instancesQuery.getParameters()); + } + + /** + * Adds condition that followerProp (user ID) is following followeeProp (user ID). + * Both props should be expressions, not raw values. + */ + public addFollowingUser(q: Q, followerProp: string, followeeProp: string): Q { + const followingQuery = this.followingsRepository.createQueryBuilder('following') + .select('1') + .andWhere(`following.followerId = ${followerProp}`) + .andWhere(`following.followeeId = ${followeeProp}`); + + return q.andWhere(`EXISTS (${followingQuery.getQuery()})`, followingQuery.getParameters()); + }; + + /** + * Adds condition that blockerProp (user ID) is not blocking blockeeProp (user ID). + * Both props should be expressions, not raw values. + */ + @bindThis + public excludeBlockingUser(q: Q, blockerProp: string, blockeeProp: string): Q { + const blockingQuery = this.blockingsRepository.createQueryBuilder('blocking') + .select('1') + .andWhere(`blocking.blockerId = ${blockerProp}`) + .andWhere(`blocking.blockeeId = ${blockeeProp}`); + + return q.andWhere(`NOT EXISTS (${blockingQuery.getQuery()})`, blockingQuery.getParameters()); + }; + + /** + * Adds condition that muterProp (user ID) is not muting muteeProp (user ID). + * Both props should be expressions, not raw values. + */ + @bindThis + public excludeMutingUser(q: Q, muterProp: string, muteeProp: string, exclude?: { id: MiUser['id'] }): Q { + const mutingQuery = this.mutingsRepository.createQueryBuilder('muting') + .select('1') + .andWhere(`muting.muterId = ${muterProp}`) + .andWhere(`muting.muteeId = ${muteeProp}`); + + if (exclude) { + mutingQuery.andWhere({ muteeId: Not(exclude.id) }); + } + + return q.andWhere(`NOT EXISTS (${mutingQuery.getQuery()})`, mutingQuery.getParameters()); + } + + /** + * Adds condition that muterProp (user ID) is not muting renotes by muteeProp (user ID). + * Both props should be expressions, not raw values. + */ + public excludeMutingRenote(q: Q, muterProp: string, muteeProp: string): Q { + const mutingQuery = this.renoteMutingsRepository.createQueryBuilder('renote_muting') + .select('1') + .andWhere(`renote_muting.muterId = ${muterProp}`) + .andWhere(`renote_muting.muteeId = ${muteeProp}`); + + return q.andWhere(`NOT EXISTS (${mutingQuery.getQuery()})`, mutingQuery.getParameters()); + }; + + /** + * Adds condition that muterProp (user ID) is not muting muteeProp (instance host). + * Both props should be expressions, not raw values. + */ + @bindThis + public excludeMutingInstance(q: Q, muterProp: string, muteeProp: string): Q { + const mutingInstanceQuery = this.userProfilesRepository.createQueryBuilder('user_profile') + .select('1') + .andWhere(`user_profile.userId = ${muterProp}`) + .andWhere(`"user_profile"."mutedInstances"::jsonb ? ${muteeProp}`); + + return q.andWhere(`NOT EXISTS (${mutingInstanceQuery.getQuery()})`, mutingInstanceQuery.getParameters()); + } + + /** + * Adds condition that muterProp (user ID) is not muting muteeProp (note ID). + * Both props should be expressions, not raw values. + */ + @bindThis + public excludeMutingThread(q: Q, muterProp: string, muteeProp: string): Q { + const threadMutedQuery = this.noteThreadMutingsRepository.createQueryBuilder('threadMuted') + .select('1') + .andWhere(`threadMuted.userId = ${muterProp}`) + .andWhere(`threadMuted.threadId = ${muteeProp}`); + + return q.andWhere(`NOT EXISTS (${threadMutedQuery.getQuery()})`, threadMutedQuery.getParameters()); } } -- cgit v1.2.3-freya From 0b9c0a6bc7f8bcd4ef1d3025884487467df81598 Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Mon, 2 Jun 2025 22:51:10 -0400 Subject: fix andWhere/orWhere in QueryService.ts --- packages/backend/src/core/QueryService.ts | 61 ++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 21 deletions(-) (limited to 'packages/backend/src/core/QueryService.ts') diff --git a/packages/backend/src/core/QueryService.ts b/packages/backend/src/core/QueryService.ts index 548887959d..0f74e7cab1 100644 --- a/packages/backend/src/core/QueryService.ts +++ b/packages/backend/src/core/QueryService.ts @@ -81,11 +81,11 @@ export class QueryService { // 投稿の引用元の作者にブロックされていない return this.excludeBlockingUser(q, 'note.userId', ':meId') .andWhere(new Brackets(qb => { - this.excludeBlockingUser(qb, 'note.replyUserId', ':meId') + this.excludeBlockingUser(qb, 'note.replyUserId', ':meId', 'orWhere') .orWhere('note.replyUserId IS NULL'); })) .andWhere(new Brackets(qb => { - this.excludeBlockingUser(qb, 'note.renoteUserId', ':meId') + this.excludeBlockingUser(qb, 'note.renoteUserId', ':meId', 'orWhere') .orWhere('note.renoteUserId IS NULL'); })) .setParameters({ meId: me.id }); @@ -102,7 +102,7 @@ export class QueryService { public generateMutedNoteThreadQuery(q: SelectQueryBuilder, me: { id: MiUser['id'] }): SelectQueryBuilder { return this.excludeMutingThread(q, ':meId', 'note.id') .andWhere(new Brackets(qb => { - this.excludeMutingThread(qb, ':meId', 'note.threadId') + this.excludeMutingThread(qb, ':meId', 'note.threadId', 'orWhere') .orWhere('note.threadId IS NULL'); })) .setParameters({ meId: me.id }); @@ -113,24 +113,24 @@ export class QueryService { // 投稿の作者をミュートしていない かつ // 投稿の返信先の作者をミュートしていない かつ // 投稿の引用元の作者をミュートしていない - this.excludeMutingUser(q, ':meId', 'note.userId', exclude) + this.excludeMutingUser(q, ':meId', 'note.userId', 'andWhere', exclude) .andWhere(new Brackets(qb => { - this.excludeMutingUser(qb, ':meId', 'note.replyUserId', exclude) + this.excludeMutingUser(qb, ':meId', 'note.replyUserId', 'orWhere', exclude) .orWhere('note.replyUserId IS NULL'); })) .andWhere(new Brackets(qb => { - this.excludeMutingUser(qb, ':meId', 'note.renoteUserId', exclude) + this.excludeMutingUser(qb, ':meId', 'note.renoteUserId', 'orWhere', exclude) .orWhere('note.renoteUserId IS NULL'); })); // mute instances - this.excludeMutingInstance(q, ':meId', 'note.userHost') + this.excludeMutingInstance(q, ':meId', 'note.userHost', 'andWhere') .andWhere(new Brackets(qb => { - this.excludeMutingInstance(qb, ':meId', 'note.replyUserHost') + this.excludeMutingInstance(qb, ':meId', 'note.replyUserHost', 'orWhere') .orWhere('note.replyUserHost IS NULL'); })) .andWhere(new Brackets(qb => { - this.excludeMutingInstance(qb, ':meId', 'note.renoteUserHost') + this.excludeMutingInstance(qb, ':meId', 'note.renoteUserHost', 'orWhere') .orWhere('note.renoteUserHost IS NULL'); })); @@ -231,6 +231,25 @@ export class QueryService { return q; } + @bindThis + public generateSilencedUserQueryForNotes(q: SelectQueryBuilder, me?: { id: MiUser['id'] } | null, userProp = 'user'): SelectQueryBuilder { + if (!me) { + return q.andWhere(`${userProp}.isSilenced = false`); + } + + return q + .andWhere(new Brackets(qb => { + // case 1: we are following the user + this.addFollowingUser(qb, ':meId', `${userProp}.id`, 'orWhere'); + // case 2: user not silenced AND instance not silenced + qb.orWhere(new Brackets(qbb => { + this.includeInstanceWhere(qbb, `${userProp}.host`, { isSilenced: false }); + qbb.andWhere(`${userProp}.isSilenced = false`); + })); + })) + .setParameters({ meId: me.id }); + } + @bindThis public generateMatchingHostQueryForNote(q: SelectQueryBuilder, filters: FindOptionsWhere | FindOptionsWhere[], hostProp = 'note.userHost'): SelectQueryBuilder { return this.includeInstanceWhere(q, hostProp, filters); @@ -268,13 +287,13 @@ export class QueryService { * Adds condition that followerProp (user ID) is following followeeProp (user ID). * Both props should be expressions, not raw values. */ - public addFollowingUser(q: Q, followerProp: string, followeeProp: string): Q { + public addFollowingUser(q: Q, followerProp: string, followeeProp: string, join: 'andWhere' | 'orWhere' = 'andWhere'): Q { const followingQuery = this.followingsRepository.createQueryBuilder('following') .select('1') .andWhere(`following.followerId = ${followerProp}`) .andWhere(`following.followeeId = ${followeeProp}`); - return q.andWhere(`EXISTS (${followingQuery.getQuery()})`, followingQuery.getParameters()); + return q[join](`EXISTS (${followingQuery.getQuery()})`, followingQuery.getParameters()); }; /** @@ -282,13 +301,13 @@ export class QueryService { * Both props should be expressions, not raw values. */ @bindThis - public excludeBlockingUser(q: Q, blockerProp: string, blockeeProp: string): Q { + public excludeBlockingUser(q: Q, blockerProp: string, blockeeProp: string, join: 'andWhere' | 'orWhere' = 'andWhere'): Q { const blockingQuery = this.blockingsRepository.createQueryBuilder('blocking') .select('1') .andWhere(`blocking.blockerId = ${blockerProp}`) .andWhere(`blocking.blockeeId = ${blockeeProp}`); - return q.andWhere(`NOT EXISTS (${blockingQuery.getQuery()})`, blockingQuery.getParameters()); + return q[join](`NOT EXISTS (${blockingQuery.getQuery()})`, blockingQuery.getParameters()); }; /** @@ -296,7 +315,7 @@ export class QueryService { * Both props should be expressions, not raw values. */ @bindThis - public excludeMutingUser(q: Q, muterProp: string, muteeProp: string, exclude?: { id: MiUser['id'] }): Q { + public excludeMutingUser(q: Q, muterProp: string, muteeProp: string, join: 'andWhere' | 'orWhere' = 'andWhere', exclude?: { id: MiUser['id'] }): Q { const mutingQuery = this.mutingsRepository.createQueryBuilder('muting') .select('1') .andWhere(`muting.muterId = ${muterProp}`) @@ -306,20 +325,20 @@ export class QueryService { mutingQuery.andWhere({ muteeId: Not(exclude.id) }); } - return q.andWhere(`NOT EXISTS (${mutingQuery.getQuery()})`, mutingQuery.getParameters()); + return q[join](`NOT EXISTS (${mutingQuery.getQuery()})`, mutingQuery.getParameters()); } /** * Adds condition that muterProp (user ID) is not muting renotes by muteeProp (user ID). * Both props should be expressions, not raw values. */ - public excludeMutingRenote(q: Q, muterProp: string, muteeProp: string): Q { + public excludeMutingRenote(q: Q, muterProp: string, muteeProp: string, join: 'andWhere' | 'orWhere' = 'andWhere'): Q { const mutingQuery = this.renoteMutingsRepository.createQueryBuilder('renote_muting') .select('1') .andWhere(`renote_muting.muterId = ${muterProp}`) .andWhere(`renote_muting.muteeId = ${muteeProp}`); - return q.andWhere(`NOT EXISTS (${mutingQuery.getQuery()})`, mutingQuery.getParameters()); + return q[join](`NOT EXISTS (${mutingQuery.getQuery()})`, mutingQuery.getParameters()); }; /** @@ -327,13 +346,13 @@ export class QueryService { * Both props should be expressions, not raw values. */ @bindThis - public excludeMutingInstance(q: Q, muterProp: string, muteeProp: string): Q { + public excludeMutingInstance(q: Q, muterProp: string, muteeProp: string, join: 'andWhere' | 'orWhere' = 'andWhere'): Q { const mutingInstanceQuery = this.userProfilesRepository.createQueryBuilder('user_profile') .select('1') .andWhere(`user_profile.userId = ${muterProp}`) .andWhere(`"user_profile"."mutedInstances"::jsonb ? ${muteeProp}`); - return q.andWhere(`NOT EXISTS (${mutingInstanceQuery.getQuery()})`, mutingInstanceQuery.getParameters()); + return q[join](`NOT EXISTS (${mutingInstanceQuery.getQuery()})`, mutingInstanceQuery.getParameters()); } /** @@ -341,12 +360,12 @@ export class QueryService { * Both props should be expressions, not raw values. */ @bindThis - public excludeMutingThread(q: Q, muterProp: string, muteeProp: string): Q { + public excludeMutingThread(q: Q, muterProp: string, muteeProp: string, join: 'andWhere' | 'orWhere' = 'andWhere'): Q { const threadMutedQuery = this.noteThreadMutingsRepository.createQueryBuilder('threadMuted') .select('1') .andWhere(`threadMuted.userId = ${muterProp}`) .andWhere(`threadMuted.threadId = ${muteeProp}`); - return q.andWhere(`NOT EXISTS (${threadMutedQuery.getQuery()})`, threadMutedQuery.getParameters()); + return q[join](`NOT EXISTS (${threadMutedQuery.getQuery()})`, threadMutedQuery.getParameters()); } } -- cgit v1.2.3-freya From 7ab5ce1537feb47480a47e4f3faab16d089820f1 Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Mon, 2 Jun 2025 22:56:35 -0400 Subject: replace generateBlockedHostQueryForNote allowSilenced parameter with generateSilencedUserQueryForNotes --- packages/backend/src/core/QueryService.ts | 17 +++-------------- .../src/server/api/endpoints/notes/search-by-tag.ts | 3 ++- 2 files changed, 5 insertions(+), 15 deletions(-) (limited to 'packages/backend/src/core/QueryService.ts') diff --git a/packages/backend/src/core/QueryService.ts b/packages/backend/src/core/QueryService.ts index 0f74e7cab1..2e0a368bd7 100644 --- a/packages/backend/src/core/QueryService.ts +++ b/packages/backend/src/core/QueryService.ts @@ -190,9 +190,8 @@ export class QueryService { .setParameters({ meId: me.id }); } - // TODO replace allowSilenced with matchingHostQuery @bindThis - public generateBlockedHostQueryForNote(q: SelectQueryBuilder, excludeAuthor?: boolean, allowSilenced = true): SelectQueryBuilder { + public generateBlockedHostQueryForNote(q: SelectQueryBuilder, excludeAuthor?: boolean): SelectQueryBuilder { const checkFor = (key: 'user' | 'replyUser' | 'renoteUser') => { q.andWhere(new Brackets(qb => { qb.orWhere(`note.${key}Host IS NULL`); // local @@ -207,18 +206,8 @@ export class QueryService { } } - if (allowSilenced) { - // not blocked - this.excludeInstanceWhere(qb, `note.${key}Host`, { - isBlocked: false, - }, 'orWhere'); - } else { - // not blocked or silenced - this.excludeInstanceWhere(qb, `note.${key}Host`, { - isBlocked: false, - isSilenced: false, - }, 'orWhere'); - } + // not blocked + this.excludeInstanceWhere(qb, `note.${key}Host`, { isBlocked: false }, 'orWhere'); })); }; diff --git a/packages/backend/src/server/api/endpoints/notes/search-by-tag.ts b/packages/backend/src/server/api/endpoints/notes/search-by-tag.ts index 5c1ab0fb78..01bedd9b1d 100644 --- a/packages/backend/src/server/api/endpoints/notes/search-by-tag.ts +++ b/packages/backend/src/server/api/endpoints/notes/search-by-tag.ts @@ -96,7 +96,8 @@ export default class extends Endpoint { // eslint- if (!this.serverSettings.enableBotTrending) query.andWhere('user.isBot = FALSE'); - this.queryService.generateBlockedHostQueryForNote(query, undefined, false); + this.queryService.generateBlockedHostQueryForNote(query); + this.queryService.generateSilencedUserQueryForNotes(query, me); if (me) this.queryService.generateMutedUserQueryForNotes(query, me); if (me) this.queryService.generateBlockedUserQueryForNotes(query, me); if (me) this.queryService.generateMutedUserRenotesQueryForNotes(query, me); -- cgit v1.2.3-freya From 15ebb0ef85f2db2dad0fad4bf0c9b06f0daf1339 Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Tue, 3 Jun 2025 15:12:59 -0400 Subject: more QueryService fixes --- packages/backend/src/core/QueryService.ts | 284 ++++++++++++++++++------------ 1 file changed, 174 insertions(+), 110 deletions(-) (limited to 'packages/backend/src/core/QueryService.ts') diff --git a/packages/backend/src/core/QueryService.ts b/packages/backend/src/core/QueryService.ts index 2e0a368bd7..bf5b0b359f 100644 --- a/packages/backend/src/core/QueryService.ts +++ b/packages/backend/src/core/QueryService.ts @@ -7,10 +7,11 @@ import { Inject, Injectable } from '@nestjs/common'; import { Brackets, Not, WhereExpressionBuilder } from 'typeorm'; import { DI } from '@/di-symbols.js'; import type { MiUser } from '@/models/User.js'; -import type { UserProfilesRepository, FollowingsRepository, ChannelFollowingsRepository, BlockingsRepository, NoteThreadMutingsRepository, MutingsRepository, RenoteMutingsRepository, MiMeta, InstancesRepository, MiInstance } from '@/models/_.js'; +import { MiInstance } from '@/models/Instance.js'; +import type { UserProfilesRepository, FollowingsRepository, ChannelFollowingsRepository, BlockingsRepository, NoteThreadMutingsRepository, MutingsRepository, RenoteMutingsRepository, MiMeta, InstancesRepository } from '@/models/_.js'; import { bindThis } from '@/decorators.js'; import { IdService } from '@/core/IdService.js'; -import type { SelectQueryBuilder, FindOptionsWhere, ObjectLiteral } from 'typeorm'; +import type { SelectQueryBuilder, ObjectLiteral } from 'typeorm'; @Injectable() export class QueryService { @@ -79,32 +80,31 @@ export class QueryService { // 投稿の作者にブロックされていない かつ // 投稿の返信先の作者にブロックされていない かつ // 投稿の引用元の作者にブロックされていない - return this.excludeBlockingUser(q, 'note.userId', ':meId') - .andWhere(new Brackets(qb => { - this.excludeBlockingUser(qb, 'note.replyUserId', ':meId', 'orWhere') - .orWhere('note.replyUserId IS NULL'); - })) - .andWhere(new Brackets(qb => { - this.excludeBlockingUser(qb, 'note.renoteUserId', ':meId', 'orWhere') - .orWhere('note.renoteUserId IS NULL'); - })) + return this + .andNotBlockingUser(q, 'note.userId', ':meId') + .andWhere(new Brackets(qb => this + .orNotBlockingUser(qb, 'note.replyUserId', ':meId') + .orWhere('note.replyUserId IS NULL'))) + .andWhere(new Brackets(qb => this + .orNotBlockingUser(qb, 'note.renoteUserId', ':meId') + .orWhere('note.renoteUserId IS NULL'))) .setParameters({ meId: me.id }); } @bindThis public generateBlockQueryForUsers(q: SelectQueryBuilder, me: { id: MiUser['id'] }): SelectQueryBuilder { - this.excludeBlockingUser(q, ':meId', 'user.id'); - this.excludeBlockingUser(q, 'user.id', ':me.id'); + this.andNotBlockingUser(q, ':meId', 'user.id'); + this.andNotBlockingUser(q, 'user.id', ':me.id'); return q.setParameters({ meId: me.id }); } @bindThis public generateMutedNoteThreadQuery(q: SelectQueryBuilder, me: { id: MiUser['id'] }): SelectQueryBuilder { - return this.excludeMutingThread(q, ':meId', 'note.id') - .andWhere(new Brackets(qb => { - this.excludeMutingThread(qb, ':meId', 'note.threadId', 'orWhere') - .orWhere('note.threadId IS NULL'); - })) + return this + .andNotMutingThread(q, ':meId', 'note.id') + .andWhere(new Brackets(qb => this + .orNotMutingThread(qb, ':meId', 'note.threadId') + .orWhere('note.threadId IS NULL'))) .setParameters({ meId: me.id }); } @@ -113,33 +113,32 @@ export class QueryService { // 投稿の作者をミュートしていない かつ // 投稿の返信先の作者をミュートしていない かつ // 投稿の引用元の作者をミュートしていない - this.excludeMutingUser(q, ':meId', 'note.userId', 'andWhere', exclude) - .andWhere(new Brackets(qb => { - this.excludeMutingUser(qb, ':meId', 'note.replyUserId', 'orWhere', exclude) - .orWhere('note.replyUserId IS NULL'); - })) - .andWhere(new Brackets(qb => { - this.excludeMutingUser(qb, ':meId', 'note.renoteUserId', 'orWhere', exclude) - .orWhere('note.renoteUserId IS NULL'); - })); - - // mute instances - this.excludeMutingInstance(q, ':meId', 'note.userHost', 'andWhere') - .andWhere(new Brackets(qb => { - this.excludeMutingInstance(qb, ':meId', 'note.replyUserHost', 'orWhere') - .orWhere('note.replyUserHost IS NULL'); - })) - .andWhere(new Brackets(qb => { - this.excludeMutingInstance(qb, ':meId', 'note.renoteUserHost', 'orWhere') - .orWhere('note.renoteUserHost IS NULL'); - })); - - return q.setParameters({ meId: me.id }); + return this + .andNotMutingUser(q, ':meId', 'note.userId', exclude) + .andWhere(new Brackets(qb => this + .orNotMutingUser(qb, ':meId', 'note.replyUserId', exclude) + .orWhere('note.replyUserId IS NULL'))) + .andWhere(new Brackets(qb => this + .orNotMutingUser(qb, ':meId', 'note.renoteUserId', exclude) + .orWhere('note.renoteUserId IS NULL'))) + // TODO exclude should also pass a host to skip these instances + // mute instances + .andWhere(new Brackets(qb => this + .andNotMutingInstance(qb, ':meId', 'note.userHost') + .orWhere('note.userHost IS NULL'))) + .andWhere(new Brackets(qb => this + .orNotMutingInstance(qb, ':meId', 'note.replyUserHost') + .orWhere('note.replyUserHost IS NULL'))) + .andWhere(new Brackets(qb => this + .orNotMutingInstance(qb, ':meId', 'note.renoteUserHost') + .orWhere('note.renoteUserHost IS NULL'))) + .setParameters({ meId: me.id }); } @bindThis public generateMutedUserQueryForUsers(q: SelectQueryBuilder, me: { id: MiUser['id'] }): SelectQueryBuilder { - return this.excludeMutingUser(q, ':meId', 'user.id') + return this + .andNotMutingUser(q, ':meId', 'user.id') .setParameters({ meId: me.id }); } @@ -165,11 +164,9 @@ export class QueryService { // Mentions me .orWhere(':meId = ANY (note.mentions)') // Followers-only post - .orWhere(new Brackets(qb => { - // または フォロワー宛ての投稿であり、 - this.addFollowingUser(qb, ':meId', 'note.userId') - .andWhere('note.visibility = \'followers\''); - })); + .orWhere(new Brackets(qb => this + .andFollowingUser(qb, ':meId', 'note.userId') + .andWhere('note.visibility = \'followers\''))); q.setParameters({ meId: me.id }); } @@ -178,38 +175,43 @@ export class QueryService { @bindThis public generateMutedUserRenotesQueryForNotes(q: SelectQueryBuilder, me: { id: MiUser['id'] }): SelectQueryBuilder { - return q.andWhere(new Brackets(qb => { - this.excludeMutingRenote(qb, ':meId', 'note.userId') + return q + .andWhere(new Brackets(qb => this + .orNotMutingRenote(qb, ':meId', 'note.userId') .orWhere('note.renoteId IS NULL') .orWhere('note.text IS NOT NULL') .orWhere('note.cw IS NOT NULL') .orWhere('note.replyId IS NOT NULL') .orWhere('note.hasPoll = true') - .orWhere('note.fileIds != \'{}\''); - })) + .orWhere('note.fileIds != \'{}\''))) .setParameters({ meId: me.id }); } + @bindThis + public generateExcludedRenotesQueryForNotes(q: SelectQueryBuilder): SelectQueryBuilder { + return q + .andWhere(new Brackets(qb => qb + .orWhere('note.renoteId IS NULL') + .orWhere('note.text IS NOT NULL') + .orWhere('note.cw IS NOT NULL') + .orWhere('note.replyId IS NOT NULL') + .orWhere('note.hasPoll = true') + .orWhere('note.fileIds != \'{}\''))); + } + @bindThis public generateBlockedHostQueryForNote(q: SelectQueryBuilder, excludeAuthor?: boolean): SelectQueryBuilder { - const checkFor = (key: 'user' | 'replyUser' | 'renoteUser') => { - q.andWhere(new Brackets(qb => { - qb.orWhere(`note.${key}Host IS NULL`); // local - - if (key !== 'user') { - // note.userId always exists and is non-null - qb.orWhere(`note.${key}Id IS NULL`); // no corresponding user - - // note.userId always equals note.userId - if (excludeAuthor) { - qb.orWhere(`note.userId = note.${key}Id`); // author - } - } + const checkFor = (key: 'user' | 'replyUser' | 'renoteUser') => this + .leftJoinInstance(q, `note.${key}Instance`, `${key}Instance`) + .andWhere(new Brackets(qb => { + qb + .orWhere(`"${key}Instance" IS NULL`) // local + .orWhere(`"${key}Instance"."isBlocked" = false`); // not blocked - // not blocked - this.excludeInstanceWhere(qb, `note.${key}Host`, { isBlocked: false }, 'orWhere'); + if (excludeAuthor) { + qb.orWhere(`note.userId = note.${key}Id`); // author + } })); - }; if (!excludeAuthor) { checkFor('user'); @@ -221,62 +223,58 @@ export class QueryService { } @bindThis - public generateSilencedUserQueryForNotes(q: SelectQueryBuilder, me?: { id: MiUser['id'] } | null, userProp = 'user'): SelectQueryBuilder { + public generateSilencedUserQueryForNotes(q: SelectQueryBuilder, me?: { id: MiUser['id'] } | null): SelectQueryBuilder { if (!me) { - return q.andWhere(`${userProp}.isSilenced = false`); + return q.andWhere('user.isSilenced = false'); } - return q - .andWhere(new Brackets(qb => { + return this + .leftJoinInstance(q, 'note.userInstance', 'userInstance') + .andWhere(new Brackets(qb => this // case 1: we are following the user - this.addFollowingUser(qb, ':meId', `${userProp}.id`, 'orWhere'); + .orFollowingUser(qb, ':meId', 'note.userId') // case 2: user not silenced AND instance not silenced - qb.orWhere(new Brackets(qbb => { - this.includeInstanceWhere(qbb, `${userProp}.host`, { isSilenced: false }); - qbb.andWhere(`${userProp}.isSilenced = false`); - })); - })) + .orWhere(new Brackets(qbb => qbb + .andWhere(new Brackets(qbbb => qbbb + .orWhere('"userInstance"."isSilenced" = false') + .orWhere('"userInstance" IS NULL'))) + .andWhere('user.isSilenced = false'))))) .setParameters({ meId: me.id }); } - @bindThis - public generateMatchingHostQueryForNote(q: SelectQueryBuilder, filters: FindOptionsWhere | FindOptionsWhere[], hostProp = 'note.userHost'): SelectQueryBuilder { - return this.includeInstanceWhere(q, hostProp, filters); - } - /** - * Adds condition that hostProp (instance host) matches the given filters. - * The prop should be an expression, not raw values. + * Left-joins an instance in to the query with a given alias and optional condition. + * These calls are de-duplicated - multiple uses of the same alias are skipped. */ @bindThis - public includeInstanceWhere(q: Q, hostProp: string, filters: FindOptionsWhere | FindOptionsWhere[], join: 'andWhere' | 'orWhere' = 'andWhere'): Q { - const instancesQuery = this.instancesRepository.createQueryBuilder('instance') - .select('1') - .andWhere(`instance.host = ${hostProp}`) - .andWhere(filters); + public leftJoinInstance(q: SelectQueryBuilder, relation: string | typeof MiInstance, alias: string, condition?: string): SelectQueryBuilder { + // Skip if it's already joined, otherwise we'll get an error + if (!q.expressionMap.joinAttributes.some(j => j.alias.name === alias)) { + q.leftJoin(relation, alias, condition); + } - return q[join](`EXISTS (${instancesQuery.getQuery()})`, instancesQuery.getParameters()); + return q; } /** - * Adds condition that hostProp (instance host) matches the given filters. - * The prop should be an expression, not raw values. + * Adds OR condition that followerProp (user ID) is following followeeProp (user ID). + * Both props should be expressions, not raw values. */ @bindThis - public excludeInstanceWhere(q: Q, hostProp: string, filters: FindOptionsWhere | FindOptionsWhere[], join: 'andWhere' | 'orWhere' = 'andWhere'): Q { - const instancesQuery = this.instancesRepository.createQueryBuilder('instance') - .select('1') - .andWhere(`instance.host = ${hostProp}`) - .andWhere(filters); - - return q[join](`NOT EXISTS (${instancesQuery.getQuery()})`, instancesQuery.getParameters()); + public orFollowingUser(q: Q, followerProp: string, followeeProp: string): Q { + return this.addFollowingUser(q, followerProp, followeeProp, 'orWhere'); } /** - * Adds condition that followerProp (user ID) is following followeeProp (user ID). + * Adds AND condition that followerProp (user ID) is following followeeProp (user ID). * Both props should be expressions, not raw values. */ - public addFollowingUser(q: Q, followerProp: string, followeeProp: string, join: 'andWhere' | 'orWhere' = 'andWhere'): Q { + @bindThis + public andFollowingUser(q: Q, followerProp: string, followeeProp: string): Q { + return this.addFollowingUser(q, followerProp, followeeProp, 'andWhere'); + } + + private addFollowingUser(q: Q, followerProp: string, followeeProp: string, join: 'andWhere' | 'orWhere'): Q { const followingQuery = this.followingsRepository.createQueryBuilder('following') .select('1') .andWhere(`following.followerId = ${followerProp}`) @@ -286,11 +284,24 @@ export class QueryService { }; /** - * Adds condition that blockerProp (user ID) is not blocking blockeeProp (user ID). + * Adds OR condition that blockerProp (user ID) is not blocking blockeeProp (user ID). * Both props should be expressions, not raw values. */ @bindThis - public excludeBlockingUser(q: Q, blockerProp: string, blockeeProp: string, join: 'andWhere' | 'orWhere' = 'andWhere'): Q { + public orNotBlockingUser(q: Q, blockerProp: string, blockeeProp: string): Q { + return this.excludeBlockingUser(q, blockerProp, blockeeProp, 'orWhere'); + } + + /** + * Adds AND condition that blockerProp (user ID) is not blocking blockeeProp (user ID). + * Both props should be expressions, not raw values. + */ + @bindThis + public andNotBlockingUser(q: Q, blockerProp: string, blockeeProp: string): Q { + return this.excludeBlockingUser(q, blockerProp, blockeeProp, 'andWhere'); + } + + private excludeBlockingUser(q: Q, blockerProp: string, blockeeProp: string, join: 'andWhere' | 'orWhere'): Q { const blockingQuery = this.blockingsRepository.createQueryBuilder('blocking') .select('1') .andWhere(`blocking.blockerId = ${blockerProp}`) @@ -300,11 +311,24 @@ export class QueryService { }; /** - * Adds condition that muterProp (user ID) is not muting muteeProp (user ID). + * Adds OR condition that muterProp (user ID) is not muting muteeProp (user ID). * Both props should be expressions, not raw values. */ @bindThis - public excludeMutingUser(q: Q, muterProp: string, muteeProp: string, join: 'andWhere' | 'orWhere' = 'andWhere', exclude?: { id: MiUser['id'] }): Q { + public orNotMutingUser(q: Q, muterProp: string, muteeProp: string, exclude?: { id: MiUser['id'] }): Q { + return this.excludeMutingUser(q, muterProp, muteeProp, 'orWhere', exclude); + } + + /** + * Adds AND condition that muterProp (user ID) is not muting muteeProp (user ID). + * Both props should be expressions, not raw values. + */ + @bindThis + public andNotMutingUser(q: Q, muterProp: string, muteeProp: string, exclude?: { id: MiUser['id'] }): Q { + return this.excludeMutingUser(q, muterProp, muteeProp, 'andWhere', exclude); + } + + private excludeMutingUser(q: Q, muterProp: string, muteeProp: string, join: 'andWhere' | 'orWhere', exclude?: { id: MiUser['id'] }): Q { const mutingQuery = this.mutingsRepository.createQueryBuilder('muting') .select('1') .andWhere(`muting.muterId = ${muterProp}`) @@ -318,10 +342,24 @@ export class QueryService { } /** - * Adds condition that muterProp (user ID) is not muting renotes by muteeProp (user ID). + * Adds OR condition that muterProp (user ID) is not muting renotes by muteeProp (user ID). * Both props should be expressions, not raw values. */ - public excludeMutingRenote(q: Q, muterProp: string, muteeProp: string, join: 'andWhere' | 'orWhere' = 'andWhere'): Q { + @bindThis + public orNotMutingRenote(q: Q, muterProp: string, muteeProp: string): Q { + return this.excludeMutingRenote(q, muterProp, muteeProp, 'orWhere'); + } + + /** + * Adds AND condition that muterProp (user ID) is not muting renotes by muteeProp (user ID). + * Both props should be expressions, not raw values. + */ + @bindThis + public andNotMutingRenote(q: Q, muterProp: string, muteeProp: string): Q { + return this.excludeMutingRenote(q, muterProp, muteeProp, 'andWhere'); + } + + private excludeMutingRenote(q: Q, muterProp: string, muteeProp: string, join: 'andWhere' | 'orWhere'): Q { const mutingQuery = this.renoteMutingsRepository.createQueryBuilder('renote_muting') .select('1') .andWhere(`renote_muting.muterId = ${muterProp}`) @@ -331,11 +369,24 @@ export class QueryService { }; /** - * Adds condition that muterProp (user ID) is not muting muteeProp (instance host). + * Adds OR condition that muterProp (user ID) is not muting muteeProp (instance host). + * Both props should be expressions, not raw values. + */ + @bindThis + public orNotMutingInstance(q: Q, muterProp: string, muteeProp: string): Q { + return this.excludeMutingInstance(q, muterProp, muteeProp, 'orWhere'); + } + + /** + * Adds AND condition that muterProp (user ID) is not muting muteeProp (instance host). * Both props should be expressions, not raw values. */ @bindThis - public excludeMutingInstance(q: Q, muterProp: string, muteeProp: string, join: 'andWhere' | 'orWhere' = 'andWhere'): Q { + public andNotMutingInstance(q: Q, muterProp: string, muteeProp: string): Q { + return this.excludeMutingInstance(q, muterProp, muteeProp, 'andWhere'); + } + + private excludeMutingInstance(q: Q, muterProp: string, muteeProp: string, join: 'andWhere' | 'orWhere'): Q { const mutingInstanceQuery = this.userProfilesRepository.createQueryBuilder('user_profile') .select('1') .andWhere(`user_profile.userId = ${muterProp}`) @@ -345,11 +396,24 @@ export class QueryService { } /** - * Adds condition that muterProp (user ID) is not muting muteeProp (note ID). + * Adds OR condition that muterProp (user ID) is not muting muteeProp (note ID). + * Both props should be expressions, not raw values. + */ + @bindThis + public orNotMutingThread(q: Q, muterProp: string, muteeProp: string): Q { + return this.excludeMutingThread(q, muterProp, muteeProp, 'orWhere'); + } + + /** + * Adds AND condition that muterProp (user ID) is not muting muteeProp (note ID). * Both props should be expressions, not raw values. */ @bindThis - public excludeMutingThread(q: Q, muterProp: string, muteeProp: string, join: 'andWhere' | 'orWhere' = 'andWhere'): Q { + public andNotMutingThread(q: Q, muterProp: string, muteeProp: string): Q { + return this.excludeMutingThread(q, muterProp, muteeProp, 'andWhere'); + } + + private excludeMutingThread(q: Q, muterProp: string, muteeProp: string, join: 'andWhere' | 'orWhere'): Q { const threadMutedQuery = this.noteThreadMutingsRepository.createQueryBuilder('threadMuted') .select('1') .andWhere(`threadMuted.userId = ${muterProp}`) -- cgit v1.2.3-freya