video-channel-sync.ts 2.1 KB

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