summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/common/generate-replies-query.ts
blob: 301782eab98267c3a18344a97f9133f672d4e9dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { User } from '@/models/entities/user.js';
import { Brackets, SelectQueryBuilder } from 'typeorm';

export function generateRepliesQuery(q: SelectQueryBuilder<any>, me?: Pick<User, 'id' | 'showTimelineReplies'> | null) {
	if (me == null) {
		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');
			}));
		}));
	} else if (!me.showTimelineReplies) {
		q.andWhere(new Brackets(qb => { qb
			.where(`note.replyId IS NULL`) // 返信ではない
			.orWhere('note.replyUserId = :meId', { meId: me.id }) // 返信だけど自分のノートへの返信
			.orWhere(new Brackets(qb => { qb // 返信だけど自分の行った返信
				.where(`note.replyId IS NOT NULL`)
				.andWhere('note.userId = :meId', { meId: me.id });
			}))
			.orWhere(new Brackets(qb => { qb // 返信だけど投稿者自身への返信
				.where(`note.replyId IS NOT NULL`)
				.andWhere('note.replyUserId = note.userId');
			}));
		}));
	}
}