2021-08-19 06:55:45 -06:00
|
|
|
import { User } from '@/models/entities/user';
|
|
|
|
import { Followings } from '@/models/index';
|
2019-04-07 06:50:36 -06:00
|
|
|
import { Brackets, SelectQueryBuilder } from 'typeorm';
|
|
|
|
|
2021-03-23 20:05:37 -06:00
|
|
|
export function generateVisibilityQuery(q: SelectQueryBuilder<any>, me?: { id: User['id'] } | null) {
|
2019-04-07 06:50:36 -06:00
|
|
|
if (me == null) {
|
|
|
|
q.andWhere(new Brackets(qb => { qb
|
|
|
|
.where(`note.visibility = 'public'`)
|
|
|
|
.orWhere(`note.visibility = 'home'`);
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
const followingQuery = Followings.createQueryBuilder('following')
|
|
|
|
.select('following.followeeId')
|
|
|
|
.where('following.followerId = :followerId', { followerId: me.id });
|
|
|
|
|
|
|
|
q.andWhere(new Brackets(qb => { qb
|
|
|
|
// 公開投稿である
|
|
|
|
.where(new Brackets(qb => { qb
|
|
|
|
.where(`note.visibility = 'public'`)
|
|
|
|
.orWhere(`note.visibility = 'home'`);
|
|
|
|
}))
|
|
|
|
// または 自分自身
|
|
|
|
.orWhere('note.userId = :userId1', { userId1: me.id })
|
|
|
|
// または 自分宛て
|
2021-05-20 23:13:03 -06:00
|
|
|
.orWhere(`'{"${me.id}"}' <@ note.visibleUserIds`)
|
2019-04-07 06:50:36 -06:00
|
|
|
.orWhere(new Brackets(qb => { qb
|
|
|
|
// または フォロワー宛ての投稿であり、
|
|
|
|
.where('note.visibility = \'followers\'')
|
|
|
|
.andWhere(new Brackets(qb => { qb
|
|
|
|
// 自分がフォロワーである
|
|
|
|
.where(`note.userId IN (${ followingQuery.getQuery() })`)
|
|
|
|
// または 自分の投稿へのリプライ
|
|
|
|
.orWhere('note.replyUserId = :userId3', { userId3: me.id });
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
|
|
|
|
q.setParameters(followingQuery.getParameters());
|
|
|
|
}
|
|
|
|
}
|