]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { body, param } from 'express-validator' | |
3 | import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc' | |
4 | import { logger } from '@server/helpers/logger' | |
5 | import { CONFIG } from '@server/initializers/config' | |
6 | import { VideoChannelSyncModel } from '@server/models/video/video-channel-sync' | |
7 | import { HttpStatusCode, VideoChannelSyncCreate } from '@shared/models' | |
8 | import { areValidationErrors, doesVideoChannelIdExist } from '../shared' | |
9 | import { doesVideoChannelSyncIdExist } from '../shared/video-channel-syncs' | |
10 | ||
11 | export const ensureSyncIsEnabled = (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
12 | if (!CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED) { | |
13 | return res.fail({ | |
14 | status: HttpStatusCode.FORBIDDEN_403, | |
15 | message: 'Synchronization is impossible as video channel synchronization is not enabled on the server' | |
16 | }) | |
17 | } | |
18 | ||
19 | return next() | |
20 | } | |
21 | ||
22 | export const videoChannelSyncValidator = [ | |
23 | body('externalChannelUrl').custom(isUrlValid).withMessage('Should have a valid channel url'), | |
24 | body('videoChannelId').isInt().withMessage('Should have a valid video channel id'), | |
25 | ||
26 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
27 | logger.debug('Checking videoChannelSync parameters', { parameters: req.body }) | |
28 | ||
29 | if (areValidationErrors(req, res)) return | |
30 | ||
31 | const body: VideoChannelSyncCreate = req.body | |
32 | if (!await doesVideoChannelIdExist(body.videoChannelId, res)) return | |
33 | ||
34 | const count = await VideoChannelSyncModel.countByAccount(res.locals.videoChannel.accountId) | |
35 | if (count >= CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.MAX_PER_USER) { | |
36 | return res.fail({ | |
37 | message: `You cannot create more than ${CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.MAX_PER_USER} channel synchronizations` | |
38 | }) | |
39 | } | |
40 | ||
41 | return next() | |
42 | } | |
43 | ] | |
44 | ||
45 | export const ensureSyncExists = [ | |
46 | param('id').exists().isInt().withMessage('Should have an sync id'), | |
47 | ||
48 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
49 | if (areValidationErrors(req, res)) return | |
50 | ||
51 | if (!await doesVideoChannelSyncIdExist(+req.params.id, res)) return | |
52 | if (!await doesVideoChannelIdExist(res.locals.videoChannelSync.videoChannelId, res)) return | |
53 | ||
54 | return next() | |
55 | } | |
56 | ] |