]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/model-loaders/video.ts
Use raw SQL for most of video queries
[github/Chocobozzz/PeerTube.git] / server / lib / model-loaders / video.ts
1 import { VideoModel } from '@server/models/video/video'
2 import {
3 MVideoAccountLightBlacklistAllFiles,
4 MVideoFormattableDetails,
5 MVideoFullLight,
6 MVideoId,
7 MVideoImmutable,
8 MVideoThumbnail,
9 MVideoWithRights
10 } from '@server/types/models'
11 import { Hooks } from '../plugins/hooks'
12
13 type VideoLoadType = 'for-api' | 'all' | 'only-video' | 'id' | 'none' | 'only-immutable-attributes'
14
15 function loadVideo (id: number | string, fetchType: 'for-api', userId?: number): Promise<MVideoFormattableDetails>
16 function loadVideo (id: number | string, fetchType: 'all', userId?: number): Promise<MVideoFullLight>
17 function loadVideo (id: number | string, fetchType: 'only-immutable-attributes'): Promise<MVideoImmutable>
18 function loadVideo (id: number | string, fetchType: 'only-video', userId?: number): Promise<MVideoThumbnail>
19 function loadVideo (id: number | string, fetchType: 'id' | 'none', userId?: number): Promise<MVideoId>
20 function loadVideo (
21 id: number | string,
22 fetchType: VideoLoadType,
23 userId?: number
24 ): Promise<MVideoFullLight | MVideoThumbnail | MVideoId | MVideoImmutable>
25 function loadVideo (
26 id: number | string,
27 fetchType: VideoLoadType,
28 userId?: number
29 ): Promise<MVideoFullLight | MVideoThumbnail | MVideoId | MVideoImmutable> {
30
31 if (fetchType === 'for-api') {
32 return Hooks.wrapPromiseFun(
33 VideoModel.loadForGetAPI,
34 { id, userId },
35 'filter:api.video.get.result'
36 )
37 }
38
39 if (fetchType === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id, undefined, userId)
40
41 if (fetchType === 'only-immutable-attributes') return VideoModel.loadImmutableAttributes(id)
42
43 if (fetchType === 'only-video') return VideoModel.load(id)
44
45 if (fetchType === 'id' || fetchType === 'none') return VideoModel.loadOnlyId(id)
46 }
47
48 type VideoLoadByUrlType = 'all' | 'only-video' | 'only-immutable-attributes'
49
50 function loadVideoByUrl (url: string, fetchType: 'all'): Promise<MVideoAccountLightBlacklistAllFiles>
51 function loadVideoByUrl (url: string, fetchType: 'only-immutable-attributes'): Promise<MVideoImmutable>
52 function loadVideoByUrl (url: string, fetchType: 'only-video'): Promise<MVideoThumbnail>
53 function loadVideoByUrl (
54 url: string,
55 fetchType: VideoLoadByUrlType
56 ): Promise<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable>
57 function loadVideoByUrl (
58 url: string,
59 fetchType: VideoLoadByUrlType
60 ): Promise<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> {
61 if (fetchType === 'all') return VideoModel.loadByUrlAndPopulateAccount(url)
62
63 if (fetchType === 'only-immutable-attributes') return VideoModel.loadByUrlImmutableAttributes(url)
64
65 if (fetchType === 'only-video') return VideoModel.loadByUrl(url)
66 }
67
68 export {
69 VideoLoadType,
70 VideoLoadByUrlType,
71
72 loadVideo,
73 loadVideoByUrl
74 }