]>
Commit | Line | Data |
---|---|---|
72c7248b C |
1 | import * as Promise from 'bluebird' |
2 | import * as validator from 'validator' | |
3 | import * as express from 'express' | |
4 | import 'express-validator' | |
5 | import 'multer' | |
6 | ||
7 | import { database as db, CONSTRAINTS_FIELDS } from '../../initializers' | |
8 | import { VideoChannelInstance } from '../../models' | |
9 | import { logger } from '../logger' | |
10 | import { exists } from './misc' | |
e34c85e5 | 11 | import { isActivityPubUrlValid } from './index' |
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 | ||
27 | function isVideoChannelUUIDValid (value: string) { | |
28 | return exists(value) && validator.isUUID('' + value, 4) | |
29 | } | |
30 | ||
31 | function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) { | |
32 | let promise: Promise<VideoChannelInstance> | |
33 | if (validator.isInt(id)) { | |
38fa2065 | 34 | promise = db.VideoChannel.loadAndPopulateAccount(+id) |
72c7248b | 35 | } else { // UUID |
38fa2065 | 36 | promise = db.VideoChannel.loadByUUIDAndPopulateAccount(id) |
72c7248b C |
37 | } |
38 | ||
39 | promise.then(videoChannel => { | |
40 | if (!videoChannel) { | |
41 | return res.status(404) | |
42 | .json({ error: 'Video channel not found' }) | |
43 | .end() | |
44 | } | |
45 | ||
46 | res.locals.videoChannel = videoChannel | |
47 | callback() | |
48 | }) | |
49 | .catch(err => { | |
50 | logger.error('Error in video channel request validator.', err) | |
51 | return res.sendStatus(500) | |
52 | }) | |
53 | } | |
54 | ||
55 | // --------------------------------------------------------------------------- | |
56 | ||
57 | export { | |
58 | isVideoChannelDescriptionValid, | |
59 | isVideoChannelNameValid, | |
60 | isVideoChannelUUIDValid, | |
e34c85e5 C |
61 | checkVideoChannelExists, |
62 | isVideoChannelUrlValid | |
72c7248b | 63 | } |