Optimize SQL requests of watch page API endpoints

This commit is contained in:
Chocobozzz 2018-09-18 12:00:49 +02:00
parent e972e046db
commit 627621c1e8
No known key found for this signature in database
GPG key ID: 583A612D890159BE
15 changed files with 68 additions and 60 deletions

View file

@ -152,13 +152,15 @@ function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: Use
return true
}
async function isVideoExist (id: string, res: Response) {
async function isVideoExist (id: string, res: Response, fetchType: 'all' | 'only-video' | 'id' | 'none' = 'all') {
let video: VideoModel | null
if (validator.isInt(id)) {
video = await VideoModel.loadAndPopulateAccountAndServerAndTags(+id)
} else { // UUID
video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(id)
if (fetchType === 'all') {
video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id)
} else if (fetchType === 'only-video') {
video = await VideoModel.load(id)
} else if (fetchType === 'id' || fetchType === 'none') {
video = await VideoModel.loadOnlyId(id)
}
if (video === null) {
@ -169,7 +171,7 @@ async function isVideoExist (id: string, res: Response) {
return false
}
res.locals.video = video
if (fetchType !== 'none') res.locals.video = video
return true
}