2019-04-07 21:50:36 +09:00
|
|
|
import { SelectQueryBuilder, Brackets } from 'typeorm';
|
2022-06-25 14:23:59 +09:00
|
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
import { Mutings, UserProfiles } from '@/models/index.js';
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2021-03-24 11:05:37 +09:00
|
|
|
export function generateMutedUserQuery(q: SelectQueryBuilder<any>, me: { id: User['id'] }, exclude?: User) {
|
2019-04-07 21:50:36 +09:00
|
|
|
const mutingQuery = Mutings.createQueryBuilder('muting')
|
|
|
|
.select('muting.muteeId')
|
|
|
|
.where('muting.muterId = :muterId', { muterId: me.id });
|
|
|
|
|
2019-10-21 00:52:50 +09:00
|
|
|
if (exclude) {
|
|
|
|
mutingQuery.andWhere('muting.muteeId != :excludeId', { excludeId: exclude.id });
|
|
|
|
}
|
|
|
|
|
2022-06-25 14:23:59 +09:00
|
|
|
const mutingInstanceQuery = UserProfiles.createQueryBuilder('user_profile')
|
|
|
|
.select('user_profile.mutedInstances')
|
|
|
|
.where('user_profile.userId = :muterId', { muterId: me.id });
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
// 投稿の作者をミュートしていない かつ
|
|
|
|
// 投稿の返信先の作者をミュートしていない かつ
|
|
|
|
// 投稿の引用元の作者をミュートしていない
|
|
|
|
q
|
|
|
|
.andWhere(`note.userId NOT IN (${ mutingQuery.getQuery() })`)
|
|
|
|
.andWhere(new Brackets(qb => { qb
|
2022-06-25 14:23:59 +09:00
|
|
|
.where('note.replyUserId IS NULL')
|
2019-04-07 21:50:36 +09:00
|
|
|
.orWhere(`note.replyUserId NOT IN (${ mutingQuery.getQuery() })`);
|
|
|
|
}))
|
|
|
|
.andWhere(new Brackets(qb => { qb
|
2022-06-25 14:23:59 +09:00
|
|
|
.where('note.renoteUserId IS NULL')
|
2019-04-07 21:50:36 +09:00
|
|
|
.orWhere(`note.renoteUserId NOT IN (${ mutingQuery.getQuery() })`);
|
2022-06-25 14:23:59 +09:00
|
|
|
}))
|
|
|
|
// mute instances
|
|
|
|
.andWhere(new Brackets(qb => { qb
|
|
|
|
.andWhere('note.userHost IS NULL')
|
|
|
|
.orWhere(`NOT ((${ mutingInstanceQuery.getQuery() })::jsonb ? note.userHost)`);
|
|
|
|
}))
|
|
|
|
.andWhere(new Brackets(qb => { qb
|
|
|
|
.where('note.replyUserHost IS NULL')
|
|
|
|
.orWhere(`NOT ((${ mutingInstanceQuery.getQuery() })::jsonb ? note.replyUserHost)`);
|
|
|
|
}))
|
|
|
|
.andWhere(new Brackets(qb => { qb
|
|
|
|
.where('note.renoteUserHost IS NULL')
|
|
|
|
.orWhere(`NOT ((${ mutingInstanceQuery.getQuery() })::jsonb ? note.renoteUserHost)`);
|
2019-04-07 21:50:36 +09:00
|
|
|
}));
|
|
|
|
|
|
|
|
q.setParameters(mutingQuery.getParameters());
|
2022-06-25 14:23:59 +09:00
|
|
|
q.setParameters(mutingInstanceQuery.getParameters());
|
2019-04-07 21:50:36 +09:00
|
|
|
}
|
|
|
|
|
2021-03-24 11:05:37 +09:00
|
|
|
export function generateMutedUserQueryForUsers(q: SelectQueryBuilder<any>, me: { id: User['id'] }) {
|
2019-04-07 21:50:36 +09:00
|
|
|
const mutingQuery = Mutings.createQueryBuilder('muting')
|
|
|
|
.select('muting.muteeId')
|
|
|
|
.where('muting.muterId = :muterId', { muterId: me.id });
|
|
|
|
|
2022-06-25 14:23:59 +09:00
|
|
|
const mutingInstanceQuery = UserProfiles.createQueryBuilder('user_profile')
|
|
|
|
.select('user_profile.mutedInstances')
|
|
|
|
.where('user_profile.userId = :muterId', { muterId: me.id });
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
q
|
2022-06-25 14:23:59 +09:00
|
|
|
.andWhere(`user.id NOT IN (${ mutingQuery.getQuery() })`)
|
|
|
|
// mute instances
|
|
|
|
.andWhere(new Brackets(qb => { qb
|
|
|
|
.andWhere('note.userHost IS NULL')
|
|
|
|
.orWhere(`NOT ((${ mutingInstanceQuery.getQuery() })::jsonb ? note.userHost)`);
|
|
|
|
}))
|
|
|
|
.andWhere(new Brackets(qb => { qb
|
|
|
|
.where('note.replyUserHost IS NULL')
|
|
|
|
.orWhere(`NOT ((${ mutingInstanceQuery.getQuery() })::jsonb ? note.replyUserHost)`);
|
|
|
|
}))
|
|
|
|
.andWhere(new Brackets(qb => { qb
|
|
|
|
.where('note.renoteUserHost IS NULL')
|
|
|
|
.orWhere(`NOT ((${ mutingInstanceQuery.getQuery() })::jsonb ? note.renoteUserHost)`);
|
|
|
|
}));
|
2019-04-07 21:50:36 +09:00
|
|
|
|
|
|
|
q.setParameters(mutingQuery.getParameters());
|
2022-06-25 14:23:59 +09:00
|
|
|
q.setParameters(mutingInstanceQuery.getParameters());
|
2019-04-07 21:50:36 +09:00
|
|
|
}
|