Add ability to disable webtorrent
[oweals/peertube.git] / server / helpers / video.ts
1 import { VideoModel } from '../models/video/video'
2 import * as Bluebird from 'bluebird'
3 import {
4   MVideoAccountLightBlacklistAllFiles,
5   MVideoFullLight,
6   MVideoIdThumbnail,
7   MVideoThumbnail,
8   MVideoWithRights
9 } from '@server/typings/models'
10 import { Response } from 'express'
11
12 type VideoFetchType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none'
13
14 function fetchVideo (id: number | string, fetchType: 'all', userId?: number): Bluebird<MVideoFullLight>
15 function fetchVideo (id: number | string, fetchType: 'only-video', userId?: number): Bluebird<MVideoThumbnail>
16 function fetchVideo (id: number | string, fetchType: 'only-video-with-rights', userId?: number): Bluebird<MVideoWithRights>
17 function fetchVideo (id: number | string, fetchType: 'id' | 'none', userId?: number): Bluebird<MVideoIdThumbnail>
18 function fetchVideo (
19   id: number | string,
20   fetchType: VideoFetchType,
21   userId?: number
22 ): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail>
23 function fetchVideo (
24   id: number | string,
25   fetchType: VideoFetchType,
26   userId?: number
27 ): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail> {
28   if (fetchType === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id, undefined, userId)
29
30   if (fetchType === 'only-video-with-rights') return VideoModel.loadWithRights(id)
31
32   if (fetchType === 'only-video') return VideoModel.load(id)
33
34   if (fetchType === 'id' || fetchType === 'none') return VideoModel.loadOnlyId(id)
35 }
36
37 type VideoFetchByUrlType = 'all' | 'only-video'
38
39 function fetchVideoByUrl (url: string, fetchType: 'all'): Bluebird<MVideoAccountLightBlacklistAllFiles>
40 function fetchVideoByUrl (url: string, fetchType: 'only-video'): Bluebird<MVideoThumbnail>
41 function fetchVideoByUrl (url: string, fetchType: VideoFetchByUrlType): Bluebird<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail>
42 function fetchVideoByUrl (url: string, fetchType: VideoFetchByUrlType): Bluebird<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail> {
43   if (fetchType === 'all') return VideoModel.loadByUrlAndPopulateAccount(url)
44
45   if (fetchType === 'only-video') return VideoModel.loadByUrl(url)
46 }
47
48 function getVideoWithAttributes (res: Response) {
49   return res.locals.videoAll || res.locals.onlyVideo || res.locals.onlyVideoWithRights
50 }
51
52 export {
53   VideoFetchType,
54   VideoFetchByUrlType,
55   fetchVideo,
56   getVideoWithAttributes,
57   fetchVideoByUrl
58 }