]>
Commit | Line | Data |
---|---|---|
2a491182 F |
1 | import * as express from 'express' |
2 | import { body, param } from 'express-validator' | |
3 | import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc' | |
2a491182 | 4 | import { CONFIG } from '@server/initializers/config' |
2a491182 F |
5 | import { VideoChannelSyncModel } from '@server/models/video/video-channel-sync' |
6 | import { HttpStatusCode, VideoChannelSyncCreate } from '@shared/models' | |
7 | import { areValidationErrors, doesVideoChannelIdExist } from '../shared' | |
a3b472a1 | 8 | import { doesVideoChannelSyncIdExist } from '../shared/video-channel-syncs' |
2a491182 F |
9 | |
10 | export const ensureSyncIsEnabled = (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
11 | if (!CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED) { | |
12 | return res.fail({ | |
13 | status: HttpStatusCode.FORBIDDEN_403, | |
14 | message: 'Synchronization is impossible as video channel synchronization is not enabled on the server' | |
15 | }) | |
16 | } | |
17 | ||
18 | return next() | |
19 | } | |
20 | ||
21 | export const videoChannelSyncValidator = [ | |
396f6f01 C |
22 | body('externalChannelUrl') |
23 | .custom(isUrlValid), | |
24 | ||
25 | body('videoChannelId') | |
26 | .isInt(), | |
2a491182 F |
27 | |
28 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
2a491182 F |
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 | ||
a3b472a1 C |
51 | if (!await doesVideoChannelSyncIdExist(+req.params.id, res)) return |
52 | if (!await doesVideoChannelIdExist(res.locals.videoChannelSync.videoChannelId, res)) return | |
2a491182 F |
53 | |
54 | return next() | |
55 | } | |
56 | ] |