2023-01-13 05:40:33 +01:00
|
|
|
import type { User } from "@/models/entities/user.js";
|
|
|
|
import type { SelectQueryBuilder } from "typeorm";
|
|
|
|
import { Brackets } from "typeorm";
|
2020-02-15 13:39:38 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export function generateRepliesQuery(
|
|
|
|
q: SelectQueryBuilder<any>,
|
2023-06-15 05:17:56 +02:00
|
|
|
withReplies: boolean,
|
|
|
|
me?: Pick<User, "id"> | null,
|
2023-01-13 05:40:33 +01:00
|
|
|
) {
|
2020-02-15 13:39:38 +01:00
|
|
|
if (me == null) {
|
2023-01-13 05:40:33 +01:00
|
|
|
q.andWhere(
|
|
|
|
new Brackets((qb) => {
|
|
|
|
qb.where("note.replyId IS NULL") // 返信ではない
|
|
|
|
.orWhere(
|
|
|
|
new Brackets((qb) => {
|
|
|
|
qb.where(
|
|
|
|
// 返信だけど投稿者自身への返信
|
|
|
|
"note.replyId IS NOT NULL",
|
|
|
|
).andWhere("note.replyUserId = note.userId");
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
);
|
2023-06-15 05:17:56 +02:00
|
|
|
} else if (!withReplies) {
|
2023-01-13 05:40:33 +01:00
|
|
|
q.andWhere(
|
|
|
|
new Brackets((qb) => {
|
|
|
|
qb.where("note.replyId IS NULL") // 返信ではない
|
|
|
|
.orWhere("note.replyUserId = :meId", { meId: me.id }) // 返信だけど自分のノートへの返信
|
|
|
|
.orWhere(
|
|
|
|
new Brackets((qb) => {
|
2023-06-15 05:17:56 +02:00
|
|
|
qb.where("note.replyId IS NOT NULL") // 返信だけど自分の行った返信
|
|
|
|
.andWhere("note.userId = :meId", { meId: me.id });
|
2023-01-13 05:40:33 +01:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
.orWhere(
|
|
|
|
new Brackets((qb) => {
|
2023-06-15 05:17:56 +02:00
|
|
|
qb.where("note.replyId IS NOT NULL") // 返信だけど投稿者自身への返信
|
|
|
|
.andWhere("note.replyUserId = note.userId");
|
2023-01-13 05:40:33 +01:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
);
|
2020-02-15 13:39:38 +01:00
|
|
|
}
|
|
|
|
}
|