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 { CONFIG } from '@server/initializers/config'
5 import { VideoChannelSyncModel } from '@server/models/video/video-channel-sync'
6 import { HttpStatusCode, VideoChannelSyncCreate } from '@shared/models'
7 import { areValidationErrors, doesVideoChannelIdExist } from '../shared'
8 import { doesVideoChannelSyncIdExist } from '../shared/video-channel-syncs'
10 export const ensureSyncIsEnabled = (req: express.Request, res: express.Response, next: express.NextFunction) => {
11 if (!CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED) {
13 status: HttpStatusCode.FORBIDDEN_403,
14 message: 'Synchronization is impossible as video channel synchronization is not enabled on the server'
21 export const videoChannelSyncValidator = [
22 body('externalChannelUrl')
25 body('videoChannelId')
28 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
29 if (areValidationErrors(req, res)) return
31 const body: VideoChannelSyncCreate = req.body
32 if (!await doesVideoChannelIdExist(body.videoChannelId, res)) return
34 const count = await VideoChannelSyncModel.countByAccount(res.locals.videoChannel.accountId)
35 if (count >= CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.MAX_PER_USER) {
37 message: `You cannot create more than ${CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.MAX_PER_USER} channel synchronizations`
45 export const ensureSyncExists = [
46 param('id').exists().isInt().withMessage('Should have an sync id'),
48 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
49 if (areValidationErrors(req, res)) return
51 if (!await doesVideoChannelSyncIdExist(+req.params.id, res)) return
52 if (!await doesVideoChannelIdExist(res.locals.videoChannelSync.videoChannelId, res)) return