From a3b472a12ec6e57dbe2f650419f8064864686eab Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 10 Aug 2022 11:51:13 +0200 Subject: Add ability to list imports of a channel sync --- server/middlewares/validators/shared/index.ts | 1 + .../validators/shared/video-channel-syncs.ts | 24 ++++++++++++++++++++++ .../validators/videos/video-channel-sync.ts | 16 +++------------ .../validators/videos/video-channels.ts | 14 +++++++++++-- .../middlewares/validators/videos/video-imports.ts | 19 +++++++++++++++-- 5 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 server/middlewares/validators/shared/video-channel-syncs.ts (limited to 'server/middlewares') diff --git a/server/middlewares/validators/shared/index.ts b/server/middlewares/validators/shared/index.ts index fa89d05f2..bbd03b248 100644 --- a/server/middlewares/validators/shared/index.ts +++ b/server/middlewares/validators/shared/index.ts @@ -4,6 +4,7 @@ export * from './utils' export * from './video-blacklists' export * from './video-captions' export * from './video-channels' +export * from './video-channel-syncs' export * from './video-comments' export * from './video-imports' export * from './video-ownerships' diff --git a/server/middlewares/validators/shared/video-channel-syncs.ts b/server/middlewares/validators/shared/video-channel-syncs.ts new file mode 100644 index 000000000..a6e51eb97 --- /dev/null +++ b/server/middlewares/validators/shared/video-channel-syncs.ts @@ -0,0 +1,24 @@ +import express from 'express' +import { VideoChannelSyncModel } from '@server/models/video/video-channel-sync' +import { HttpStatusCode } from '@shared/models' + +async function doesVideoChannelSyncIdExist (id: number, res: express.Response) { + const sync = await VideoChannelSyncModel.loadWithChannel(+id) + + if (!sync) { + res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Video channel sync not found' + }) + return false + } + + res.locals.videoChannelSync = sync + return true +} + +// --------------------------------------------------------------------------- + +export { + doesVideoChannelSyncIdExist +} diff --git a/server/middlewares/validators/videos/video-channel-sync.ts b/server/middlewares/validators/videos/video-channel-sync.ts index b18498243..081f09bba 100644 --- a/server/middlewares/validators/videos/video-channel-sync.ts +++ b/server/middlewares/validators/videos/video-channel-sync.ts @@ -3,10 +3,10 @@ import { body, param } from 'express-validator' import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc' import { logger } from '@server/helpers/logger' import { CONFIG } from '@server/initializers/config' -import { VideoChannelModel } from '@server/models/video/video-channel' import { VideoChannelSyncModel } from '@server/models/video/video-channel-sync' import { HttpStatusCode, VideoChannelSyncCreate } from '@shared/models' import { areValidationErrors, doesVideoChannelIdExist } from '../shared' +import { doesVideoChannelSyncIdExist } from '../shared/video-channel-syncs' export const ensureSyncIsEnabled = (req: express.Request, res: express.Response, next: express.NextFunction) => { if (!CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED) { @@ -48,18 +48,8 @@ export const ensureSyncExists = [ async (req: express.Request, res: express.Response, next: express.NextFunction) => { if (areValidationErrors(req, res)) return - const syncId = parseInt(req.params.id, 10) - const sync = await VideoChannelSyncModel.loadWithChannel(syncId) - - if (!sync) { - return res.fail({ - status: HttpStatusCode.NOT_FOUND_404, - message: 'Synchronization not found' - }) - } - - res.locals.videoChannelSync = sync - res.locals.videoChannel = await VideoChannelModel.loadAndPopulateAccount(sync.videoChannelId) + if (!await doesVideoChannelSyncIdExist(+req.params.id, res)) return + if (!await doesVideoChannelIdExist(res.locals.videoChannelSync.videoChannelId, res)) return return next() } diff --git a/server/middlewares/validators/videos/video-channels.ts b/server/middlewares/validators/videos/video-channels.ts index 88f8b814d..d53c777fa 100644 --- a/server/middlewares/validators/videos/video-channels.ts +++ b/server/middlewares/validators/videos/video-channels.ts @@ -3,8 +3,9 @@ import { body, param, query } from 'express-validator' import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc' import { CONFIG } from '@server/initializers/config' import { MChannelAccountDefault } from '@server/types/models' +import { VideosImportInChannelCreate } from '@shared/models' import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' -import { isBooleanValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc' +import { isBooleanValid, isIdValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc' import { isVideoChannelDescriptionValid, isVideoChannelDisplayNameValid, @@ -15,6 +16,7 @@ import { logger } from '../../../helpers/logger' import { ActorModel } from '../../../models/actor/actor' import { VideoChannelModel } from '../../../models/video/video-channel' import { areValidationErrors, checkUserQuota, doesVideoChannelNameWithHostExist } from '../shared' +import { doesVideoChannelSyncIdExist } from '../shared/video-channel-syncs' export const videoChannelsAddValidator = [ body('name').custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'), @@ -145,11 +147,17 @@ export const videoChannelsListValidator = [ export const videoChannelImportVideosValidator = [ body('externalChannelUrl').custom(isUrlValid).withMessage('Should have a valid channel url'), - (req: express.Request, res: express.Response, next: express.NextFunction) => { + body('videoChannelSyncId') + .optional() + .custom(isIdValid).withMessage('Should have a valid channel sync id'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking videoChannelImport parameters', { parameters: req.body }) if (areValidationErrors(req, res)) return + const body: VideosImportInChannelCreate = req.body + if (!CONFIG.IMPORT.VIDEOS.HTTP.ENABLED) { return res.fail({ status: HttpStatusCode.FORBIDDEN_403, @@ -157,6 +165,8 @@ export const videoChannelImportVideosValidator = [ }) } + if (body.videoChannelSyncId && !await doesVideoChannelSyncIdExist(body.videoChannelSyncId, res)) return + return next() } ] diff --git a/server/middlewares/validators/videos/video-imports.ts b/server/middlewares/validators/videos/video-imports.ts index 9c6d213c4..3115acb21 100644 --- a/server/middlewares/validators/videos/video-imports.ts +++ b/server/middlewares/validators/videos/video-imports.ts @@ -1,5 +1,5 @@ import express from 'express' -import { body, param } from 'express-validator' +import { body, param, query } from 'express-validator' import { isResolvingToUnicastOnly } from '@server/helpers/dns' import { isPreImportVideoAccepted } from '@server/lib/moderation' import { Hooks } from '@server/lib/plugins/hooks' @@ -92,6 +92,20 @@ const videoImportAddValidator = getCommonVideoEditAttributes().concat([ } ]) +const getMyVideoImportsValidator = [ + query('videoChannelSyncId') + .optional() + .custom(isIdValid).withMessage('Should have correct videoChannelSync id'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking getMyVideoImportsValidator parameters', { parameters: req.params }) + + if (areValidationErrors(req, res)) return + + return next() + } +] + const videoImportDeleteValidator = [ param('id') .custom(isIdValid).withMessage('Should have correct import id'), @@ -143,7 +157,8 @@ const videoImportCancelValidator = [ export { videoImportAddValidator, videoImportCancelValidator, - videoImportDeleteValidator + videoImportDeleteValidator, + getMyVideoImportsValidator } // --------------------------------------------------------------------------- -- cgit v1.2.3