]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/video.ts
Add model cache for video
[github/Chocobozzz/PeerTube.git] / server / helpers / video.ts
CommitLineData
4157cdb1 1import { VideoModel } from '../models/video/video'
453e83ea 2import * as Bluebird from 'bluebird'
0283eaac
C
3import {
4 MVideoAccountLightBlacklistAllFiles,
5 MVideoFullLight,
6 MVideoIdThumbnail,
7 MVideoThumbnail,
7eba5e1f
C
8 MVideoWithRights,
9 MVideoImmutable
0283eaac 10} from '@server/typings/models'
453e83ea 11import { Response } from 'express'
4157cdb1 12
7eba5e1f 13type VideoFetchType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none' | 'only-immutable-attributes'
4157cdb1 14
453e83ea 15function fetchVideo (id: number | string, fetchType: 'all', userId?: number): Bluebird<MVideoFullLight>
7eba5e1f 16function fetchVideo (id: number | string, fetchType: 'only-immutable-attributes'): Bluebird<MVideoImmutable>
453e83ea
C
17function fetchVideo (id: number | string, fetchType: 'only-video', userId?: number): Bluebird<MVideoThumbnail>
18function fetchVideo (id: number | string, fetchType: 'only-video-with-rights', userId?: number): Bluebird<MVideoWithRights>
19function fetchVideo (id: number | string, fetchType: 'id' | 'none', userId?: number): Bluebird<MVideoIdThumbnail>
20function fetchVideo (
21 id: number | string,
22 fetchType: VideoFetchType,
23 userId?: number
7eba5e1f 24): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable>
453e83ea
C
25function fetchVideo (
26 id: number | string,
27 fetchType: VideoFetchType,
28 userId?: number
7eba5e1f 29): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable> {
6e46de09 30 if (fetchType === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id, undefined, userId)
4157cdb1 31
7eba5e1f
C
32 if (fetchType === 'only-immutable-attributes') return VideoModel.loadImmutableAttributes(id)
33
09209296
C
34 if (fetchType === 'only-video-with-rights') return VideoModel.loadWithRights(id)
35
4157cdb1
C
36 if (fetchType === 'only-video') return VideoModel.load(id)
37
38 if (fetchType === 'id' || fetchType === 'none') return VideoModel.loadOnlyId(id)
39}
40
41type VideoFetchByUrlType = 'all' | 'only-video'
453e83ea 42
0283eaac 43function fetchVideoByUrl (url: string, fetchType: 'all'): Bluebird<MVideoAccountLightBlacklistAllFiles>
453e83ea 44function fetchVideoByUrl (url: string, fetchType: 'only-video'): Bluebird<MVideoThumbnail>
0283eaac
C
45function fetchVideoByUrl (url: string, fetchType: VideoFetchByUrlType): Bluebird<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail>
46function fetchVideoByUrl (url: string, fetchType: VideoFetchByUrlType): Bluebird<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail> {
4157cdb1
C
47 if (fetchType === 'all') return VideoModel.loadByUrlAndPopulateAccount(url)
48
49 if (fetchType === 'only-video') return VideoModel.loadByUrl(url)
50}
51
0283eaac
C
52function getVideoWithAttributes (res: Response) {
53 return res.locals.videoAll || res.locals.onlyVideo || res.locals.onlyVideoWithRights
54}
55
4157cdb1
C
56export {
57 VideoFetchType,
58 VideoFetchByUrlType,
59 fetchVideo,
0283eaac 60 getVideoWithAttributes,
4157cdb1
C
61 fetchVideoByUrl
62}