2
1

video-channels.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import * as express from 'express'
  2. import 'express-validator'
  3. import 'multer'
  4. import * as validator from 'validator'
  5. import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
  6. import { VideoChannelModel } from '../../models/video/video-channel'
  7. import { exists } from './misc'
  8. const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS
  9. function isVideoChannelDescriptionValid (value: string) {
  10. return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION)
  11. }
  12. function isVideoChannelNameValid (value: string) {
  13. return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME)
  14. }
  15. function isVideoChannelSupportValid (value: string) {
  16. return value === null || (exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.SUPPORT))
  17. }
  18. async function doesLocalVideoChannelNameExist (name: string, res: express.Response) {
  19. const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
  20. return processVideoChannelExist(videoChannel, res)
  21. }
  22. async function doesVideoChannelIdExist (id: number, res: express.Response) {
  23. const videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id)
  24. return processVideoChannelExist(videoChannel, res)
  25. }
  26. async function doesVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) {
  27. const videoChannel = await VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithDomain)
  28. return processVideoChannelExist(videoChannel, res)
  29. }
  30. // ---------------------------------------------------------------------------
  31. export {
  32. doesVideoChannelNameWithHostExist,
  33. doesLocalVideoChannelNameExist,
  34. isVideoChannelDescriptionValid,
  35. isVideoChannelNameValid,
  36. isVideoChannelSupportValid,
  37. doesVideoChannelIdExist
  38. }
  39. function processVideoChannelExist (videoChannel: VideoChannelModel, res: express.Response) {
  40. if (!videoChannel) {
  41. res.status(404)
  42. .json({ error: 'Video channel not found' })
  43. .end()
  44. return false
  45. }
  46. res.locals.videoChannel = videoChannel
  47. return true
  48. }