]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/custom-validators/abuses.ts
Fix remote actor creation date
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / abuses.ts
index a6a895c659edb130d67dbc584d5e6a43e3f93c23..94719641abd7561ff6414c43c31c58d304d0e7ed 100644 (file)
@@ -1,19 +1,25 @@
 import validator from 'validator'
-import { abusePredefinedReasonsMap, AbusePredefinedReasonsString, AbuseVideoIs } from '@shared/models'
-import { CONSTRAINTS_FIELDS, ABUSE_STATES } from '../../initializers/constants'
+import { abusePredefinedReasonsMap } from '@shared/core-utils/abuse'
+import { AbuseCreate, AbuseFilter, AbusePredefinedReasonsString, AbuseVideoIs } from '@shared/models'
+import { ABUSE_STATES, CONSTRAINTS_FIELDS } from '../../initializers/constants'
 import { exists, isArray } from './misc'
 
-const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.ABUSES
+const ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.ABUSES
+const ABUSE_MESSAGES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.ABUSE_MESSAGES
 
 function isAbuseReasonValid (value: string) {
-  return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
+  return exists(value) && validator.isLength(value, ABUSES_CONSTRAINTS_FIELDS.REASON)
 }
 
 function isAbusePredefinedReasonValid (value: AbusePredefinedReasonsString) {
   return exists(value) && value in abusePredefinedReasonsMap
 }
 
-function isAbusePredefinedReasonsValid (value: AbusePredefinedReasonsString[]) {
+function isAbuseFilterValid (value: AbuseFilter) {
+  return value === 'video' || value === 'comment' || value === 'account'
+}
+
+function areAbusePredefinedReasonsValid (value: AbusePredefinedReasonsString[]) {
   return exists(value) && isArray(value) && value.every(v => v in abusePredefinedReasonsMap)
 }
 
@@ -22,11 +28,13 @@ function isAbuseTimestampValid (value: number) {
 }
 
 function isAbuseTimestampCoherent (endAt: number, { req }) {
-  return exists(req.body.startAt) && endAt > req.body.startAt
+  const startAt = (req.body as AbuseCreate).video.startAt
+
+  return exists(startAt) && endAt > startAt
 }
 
 function isAbuseModerationCommentValid (value: string) {
-  return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.MODERATION_COMMENT)
+  return exists(value) && validator.isLength(value, ABUSES_CONSTRAINTS_FIELDS.MODERATION_COMMENT)
 }
 
 function isAbuseStateValid (value: string) {
@@ -40,12 +48,18 @@ function isAbuseVideoIsValid (value: AbuseVideoIs) {
   )
 }
 
+function isAbuseMessageValid (value: string) {
+  return exists(value) && validator.isLength(value, ABUSE_MESSAGES_CONSTRAINTS_FIELDS.MESSAGE)
+}
+
 // ---------------------------------------------------------------------------
 
 export {
   isAbuseReasonValid,
+  isAbuseFilterValid,
   isAbusePredefinedReasonValid,
-  isAbusePredefinedReasonsValid,
+  isAbuseMessageValid,
+  areAbusePredefinedReasonsValid,
   isAbuseTimestampValid,
   isAbuseTimestampCoherent,
   isAbuseModerationCommentValid,