2022-02-27 03:07:39 +01:00
|
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
import { Followings } from '@/models/index.js';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { Brackets, SelectQueryBuilder } from 'typeorm';
|
|
|
|
|
2021-03-24 03:05:37 +01:00
|
|
|
export function generateVisibilityQuery(q: SelectQueryBuilder<any>, me?: { id: User['id'] } | null) {
|
2022-06-11 09:14:44 +02:00
|
|
|
// This code must always be synchronized with the checks in Notes.isVisibleForMe.
|
2019-04-07 14:50:36 +02: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')
|
2022-06-11 09:14:44 +02:00
|
|
|
.where('following.followerId = :meId');
|
2019-04-07 14:50:36 +02:00
|
|
|
|
|
|
|
q.andWhere(new Brackets(qb => { qb
|
|
|
|
// 公開投稿である
|
|
|
|
.where(new Brackets(qb => { qb
|
|
|
|
.where(`note.visibility = 'public'`)
|
|
|
|
.orWhere(`note.visibility = 'home'`);
|
|
|
|
}))
|
|
|
|
// または 自分自身
|
2022-06-11 09:14:44 +02:00
|
|
|
.orWhere('note.userId = :meId')
|
2019-04-07 14:50:36 +02:00
|
|
|
// または 自分宛て
|
2022-06-11 09:14:44 +02:00
|
|
|
.orWhere(':meId = ANY(note.visibleUserIds)')
|
|
|
|
.orWhere(':meId = ANY(note.mentions)')
|
2019-04-07 14:50:36 +02:00
|
|
|
.orWhere(new Brackets(qb => { qb
|
|
|
|
// または フォロワー宛ての投稿であり、
|
2022-06-11 09:14:44 +02:00
|
|
|
.where(`note.visibility = 'followers'`)
|
2019-04-07 14:50:36 +02:00
|
|
|
.andWhere(new Brackets(qb => { qb
|
|
|
|
// 自分がフォロワーである
|
|
|
|
.where(`note.userId IN (${ followingQuery.getQuery() })`)
|
|
|
|
// または 自分の投稿へのリプライ
|
2022-06-11 09:14:44 +02:00
|
|
|
.orWhere('note.replyUserId = :meId');
|
2019-04-07 14:50:36 +02:00
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
|
2022-06-11 09:14:44 +02:00
|
|
|
q.setParameters({ meId: me.id });
|
2019-04-07 14:50:36 +02:00
|
|
|
}
|
|
|
|
}
|