summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/common/generate-muted-instance-query.ts
blob: 72a6fec68f5594d7243dbfc5b0778dac6132ec94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { User } from '@/models/entities/user.js';
import { id } from '@/models/id.js';
import { UserProfiles } from '@/models/index.js';
import { SelectQueryBuilder, Brackets } from 'typeorm';

function createMutesQuery(id: string) {
	return UserProfiles.createQueryBuilder('user_profile')
		.select('user_profile.mutedInstances')
		.where('user_profile.userId = :muterId', { muterId: id });
}

export function generateMutedInstanceQuery(q: SelectQueryBuilder<any>, me: { id: User['id'] }) {
	const mutingQuery = createMutesQuery(me.id);

	q
		.andWhere(new Brackets(qb => { qb
			.andWhere('note.userHost IS NULL')
			.orWhere(`NOT((${ mutingQuery.getQuery() })::jsonb ? note.userHost)`);
		}))
		.andWhere(new Brackets(qb => { qb
			.where(`note.replyUserHost IS NULL`)
			.orWhere(`NOT ((${ mutingQuery.getQuery() })::jsonb ? note.replyUserHost)`);
		}))
		.andWhere(new Brackets(qb => { qb
			.where(`note.renoteUserHost IS NULL`)
			.orWhere(`NOT ((${ mutingQuery.getQuery() })::jsonb ? note.renoteUserHost)`);
		}));
	q.setParameters(mutingQuery.getParameters());
}

export function generateMutedInstanceNotificationQuery(q: SelectQueryBuilder<any>, me: { id: User['id'] }) {
	const mutingQuery = createMutesQuery(me.id);

	q.andWhere(new Brackets(qb => { qb 
		.andWhere('notifier.host IS NULL')
		.orWhere(`NOT (( ${mutingQuery.getQuery()} )::jsonb ? notifier.host)`);
	}));

	q.setParameters(mutingQuery.getParameters());
}