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'
11 export const ensureSyncIsEnabled = (req: express.Request, res: express.Response, next: express.NextFunction) => {
12 if (!CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED) {
14 status: HttpStatusCode.FORBIDDEN_403,
15 message: 'Synchronization is impossible as video channel synchronization is not enabled on the server'
22 export const videoChannelSyncValidator = [
23 body('externalChannelUrl')
26 body('videoChannelId')
29 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
30 logger.debug('Checking videoChannelSync parameters', { parameters: req.body })
32 if (areValidationErrors(req, res)) return
34 const body: VideoChannelSyncCreate = req.body
35 if (!await doesVideoChannelIdExist(body.videoChannelId, res)) return
37 const count = await VideoChannelSyncModel.countByAccount(res.locals.videoChannel.accountId)
38 if (count >= CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.MAX_PER_USER) {
40 message: `You cannot create more than ${CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.MAX_PER_USER} channel synchronizations`
48 export const ensureSyncExists = [
49 param('id').exists().isInt().withMessage('Should have an sync id'),
51 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
52 if (areValidationErrors(req, res)) return
54 if (!await doesVideoChannelSyncIdExist(+req.params.id, res)) return
55 if (!await doesVideoChannelIdExist(res.locals.videoChannelSync.videoChannelId, res)) return