diff options
author | Chocobozzz <me@florianbigard.com> | 2018-09-19 11:16:23 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-09-19 11:16:23 +0200 |
commit | 4157cdb13748cb6e8ce7081d062a8778554cc5a7 (patch) | |
tree | 149ee35079c1f81f1294f88aa0122dd5c4d55b22 /server/helpers | |
parent | 96f29c0f6d2e623fb088e88200934c5df8da9924 (diff) | |
download | PeerTube-4157cdb13748cb6e8ce7081d062a8778554cc5a7.tar.gz PeerTube-4157cdb13748cb6e8ce7081d062a8778554cc5a7.tar.zst PeerTube-4157cdb13748cb6e8ce7081d062a8778554cc5a7.zip |
Refractor videos AP functions
Diffstat (limited to 'server/helpers')
-rw-r--r-- | server/helpers/custom-validators/videos.ts | 12 | ||||
-rw-r--r-- | server/helpers/video.ts | 25 |
2 files changed, 27 insertions, 10 deletions
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index c9ef8445d..9875c68bd 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts | |||
@@ -18,6 +18,7 @@ import { exists, isArray, isFileValid } from './misc' | |||
18 | import { VideoChannelModel } from '../../models/video/video-channel' | 18 | import { VideoChannelModel } from '../../models/video/video-channel' |
19 | import { UserModel } from '../../models/account/user' | 19 | import { UserModel } from '../../models/account/user' |
20 | import * as magnetUtil from 'magnet-uri' | 20 | import * as magnetUtil from 'magnet-uri' |
21 | import { fetchVideo, VideoFetchType } from '../video' | ||
21 | 22 | ||
22 | const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS | 23 | const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS |
23 | 24 | ||
@@ -152,17 +153,8 @@ function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: Use | |||
152 | return true | 153 | return true |
153 | } | 154 | } |
154 | 155 | ||
155 | export type VideoFetchType = 'all' | 'only-video' | 'id' | 'none' | ||
156 | async function isVideoExist (id: string, res: Response, fetchType: VideoFetchType = 'all') { | 156 | async function isVideoExist (id: string, res: Response, fetchType: VideoFetchType = 'all') { |
157 | let video: VideoModel | null | 157 | const video = await fetchVideo(id, fetchType) |
158 | |||
159 | if (fetchType === 'all') { | ||
160 | video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id) | ||
161 | } else if (fetchType === 'only-video') { | ||
162 | video = await VideoModel.load(id) | ||
163 | } else if (fetchType === 'id' || fetchType === 'none') { | ||
164 | video = await VideoModel.loadOnlyId(id) | ||
165 | } | ||
166 | 158 | ||
167 | if (video === null) { | 159 | if (video === null) { |
168 | res.status(404) | 160 | res.status(404) |
diff --git a/server/helpers/video.ts b/server/helpers/video.ts new file mode 100644 index 000000000..b1577a6b0 --- /dev/null +++ b/server/helpers/video.ts | |||
@@ -0,0 +1,25 @@ | |||
1 | import { VideoModel } from '../models/video/video' | ||
2 | |||
3 | type VideoFetchType = 'all' | 'only-video' | 'id' | 'none' | ||
4 | |||
5 | function fetchVideo (id: number | string, fetchType: VideoFetchType) { | ||
6 | if (fetchType === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id) | ||
7 | |||
8 | if (fetchType === 'only-video') return VideoModel.load(id) | ||
9 | |||
10 | if (fetchType === 'id' || fetchType === 'none') return VideoModel.loadOnlyId(id) | ||
11 | } | ||
12 | |||
13 | type VideoFetchByUrlType = 'all' | 'only-video' | ||
14 | function fetchVideoByUrl (url: string, fetchType: VideoFetchByUrlType) { | ||
15 | if (fetchType === 'all') return VideoModel.loadByUrlAndPopulateAccount(url) | ||
16 | |||
17 | if (fetchType === 'only-video') return VideoModel.loadByUrl(url) | ||
18 | } | ||
19 | |||
20 | export { | ||
21 | VideoFetchType, | ||
22 | VideoFetchByUrlType, | ||
23 | fetchVideo, | ||
24 | fetchVideoByUrl | ||
25 | } | ||