iceshrimp-legacy/packages/backend/src/server/api/common/generate-channel-query.ts

36 lines
1,010 B
TypeScript
Raw Normal View History

2023-01-13 05:40:33 +01:00
import type { User } from "@/models/entities/user.js";
import { ChannelFollowings } from "@/models/index.js";
import type { SelectQueryBuilder } from "typeorm";
import { Brackets } from "typeorm";
2023-01-13 05:40:33 +01:00
export function generateChannelQuery(
q: SelectQueryBuilder<any>,
me?: { id: User["id"] } | null,
) {
if (me == null) {
2023-01-13 05:40:33 +01:00
q.andWhere("note.channelId IS NULL");
} else {
2023-01-13 05:40:33 +01:00
q.leftJoinAndSelect("note.channel", "channel");
2023-01-13 05:40:33 +01:00
const channelFollowingQuery = ChannelFollowings.createQueryBuilder(
"channelFollowing",
)
.select("channelFollowing.followeeId")
.where("channelFollowing.followerId = :followerId", {
followerId: me.id,
});
2023-01-13 05:40:33 +01:00
q.andWhere(
new Brackets((qb) => {
qb
// チャンネルのノートではない
.where("note.channelId IS NULL")
// または自分がフォローしているチャンネルのノート
.orWhere(`note.channelId IN (${channelFollowingQuery.getQuery()})`);
}),
);
q.setParameters(channelFollowingQuery.getParameters());
}
}