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