2
1

video-abuses.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { Response } from 'express'
  2. import * as validator from 'validator'
  3. import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers/constants'
  4. import { exists } from './misc'
  5. import { VideoAbuseModel } from '../../models/video/video-abuse'
  6. const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
  7. function isVideoAbuseReasonValid (value: string) {
  8. return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
  9. }
  10. function isVideoAbuseModerationCommentValid (value: string) {
  11. return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.MODERATION_COMMENT)
  12. }
  13. function isVideoAbuseStateValid (value: string) {
  14. return exists(value) && VIDEO_ABUSE_STATES[ value ] !== undefined
  15. }
  16. async function doesVideoAbuseExist (abuseId: number, videoId: number, res: Response) {
  17. const videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, videoId)
  18. if (videoAbuse === null) {
  19. res.status(404)
  20. .json({ error: 'Video abuse not found' })
  21. .end()
  22. return false
  23. }
  24. res.locals.videoAbuse = videoAbuse
  25. return true
  26. }
  27. // ---------------------------------------------------------------------------
  28. export {
  29. doesVideoAbuseExist,
  30. isVideoAbuseStateValid,
  31. isVideoAbuseReasonValid,
  32. isVideoAbuseModerationCommentValid
  33. }