]>
Commit | Line | Data |
---|---|---|
4e50b6a1 | 1 | import * as Bluebird from 'bluebird' |
72c7248b C |
2 | import * as express from 'express' |
3 | import 'express-validator' | |
4 | import 'multer' | |
4e50b6a1 | 5 | import * as validator from 'validator' |
72c7248b | 6 | |
4e50b6a1 | 7 | import { CONSTRAINTS_FIELDS, database as db } from '../../initializers' |
72c7248b C |
8 | import { VideoChannelInstance } from '../../models' |
9 | import { logger } from '../logger' | |
e34c85e5 | 10 | import { isActivityPubUrlValid } from './index' |
4e50b6a1 | 11 | import { exists } from './misc' |
72c7248b C |
12 | |
13 | const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS | |
14 | ||
e34c85e5 C |
15 | function isVideoChannelUrlValid (value: string) { |
16 | return isActivityPubUrlValid(value) | |
17 | } | |
18 | ||
72c7248b C |
19 | function isVideoChannelDescriptionValid (value: string) { |
20 | return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION) | |
21 | } | |
22 | ||
23 | function isVideoChannelNameValid (value: string) { | |
24 | return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME) | |
25 | } | |
26 | ||
72c7248b | 27 | function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) { |
4e50b6a1 | 28 | let promise: Bluebird<VideoChannelInstance> |
72c7248b | 29 | if (validator.isInt(id)) { |
38fa2065 | 30 | promise = db.VideoChannel.loadAndPopulateAccount(+id) |
72c7248b | 31 | } else { // UUID |
38fa2065 | 32 | promise = db.VideoChannel.loadByUUIDAndPopulateAccount(id) |
72c7248b C |
33 | } |
34 | ||
35 | promise.then(videoChannel => { | |
36 | if (!videoChannel) { | |
37 | return res.status(404) | |
38 | .json({ error: 'Video channel not found' }) | |
39 | .end() | |
40 | } | |
41 | ||
42 | res.locals.videoChannel = videoChannel | |
43 | callback() | |
44 | }) | |
45 | .catch(err => { | |
46 | logger.error('Error in video channel request validator.', err) | |
47 | return res.sendStatus(500) | |
48 | }) | |
49 | } | |
50 | ||
4e50b6a1 C |
51 | async function isVideoChannelExistsPromise (id: string, res: express.Response) { |
52 | let videoChannel: VideoChannelInstance | |
53 | if (validator.isInt(id)) { | |
54 | videoChannel = await db.VideoChannel.loadAndPopulateAccount(+id) | |
55 | } else { // UUID | |
56 | videoChannel = await db.VideoChannel.loadByUUIDAndPopulateAccount(id) | |
57 | } | |
58 | ||
59 | if (!videoChannel) { | |
60 | res.status(404) | |
61 | .json({ error: 'Video channel not found' }) | |
62 | .end() | |
63 | ||
64 | return false | |
65 | } | |
66 | ||
67 | res.locals.videoChannel = videoChannel | |
68 | return true | |
69 | } | |
70 | ||
72c7248b C |
71 | // --------------------------------------------------------------------------- |
72 | ||
73 | export { | |
74 | isVideoChannelDescriptionValid, | |
e34c85e5 | 75 | checkVideoChannelExists, |
4e50b6a1 C |
76 | isVideoChannelNameValid, |
77 | isVideoChannelExistsPromise, | |
e34c85e5 | 78 | isVideoChannelUrlValid |
72c7248b | 79 | } |