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