]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/custom-validators/videos.ts
Continue activitypub
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / videos.ts
index 5b9102275cebac16b4d7e5d842441f3efbdc3406..83407f17b0890d0e2d482ca97609634095c9e0e5 100644 (file)
@@ -11,6 +11,7 @@ import {
   VIDEO_LICENCES,
   VIDEO_LANGUAGES,
   VIDEO_RATE_TYPES,
+  VIDEO_PRIVACIES,
   database as db
 } from '../../initializers'
 import { isUserUsernameValid } from './users'
@@ -36,6 +37,15 @@ function isVideoLicenceValid (value: number) {
   return VIDEO_LICENCES[value] !== undefined
 }
 
+function isVideoPrivacyValid (value: string) {
+  return VIDEO_PRIVACIES[value] !== undefined
+}
+
+// Maybe we don't know the remote privacy setting, but that doesn't matter
+function isRemoteVideoPrivacyValid (value: string) {
+  return validator.isInt('' + value)
+}
+
 // Maybe we don't know the remote licence, but that doesn't matter
 function isRemoteVideoLicenceValid (value: string) {
   return validator.isInt('' + value)
@@ -63,19 +73,26 @@ function isVideoDescriptionValid (value: string) {
 }
 
 function isVideoDurationValid (value: string) {
-  return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
+  // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
+  return exists(value) &&
+    typeof value === 'string' &&
+    value.startsWith('PT') &&
+    value.endsWith('S') &&
+    validator.isInt(value.replace(/[^0-9]+/, ''), VIDEOS_CONSTRAINTS_FIELDS.DURATION)
 }
 
 function isVideoNameValid (value: string) {
   return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
 }
 
+function isVideoTagValid (tag: string) {
+  return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
+}
+
 function isVideoTagsValid (tags: string[]) {
   return isArray(tags) &&
          validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
-         tags.every(tag => {
-           return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
-         })
+         tags.every(tag => isVideoTagValid(tag))
 }
 
 function isVideoThumbnailValid (value: string) {
@@ -195,8 +212,11 @@ export {
   isVideoDislikesValid,
   isVideoEventCountValid,
   isVideoFileSizeValid,
+  isVideoPrivacyValid,
+  isRemoteVideoPrivacyValid,
   isVideoFileResolutionValid,
   checkVideoExists,
+  isVideoTagValid,
   isRemoteVideoCategoryValid,
   isRemoteVideoLicenceValid,
   isRemoteVideoLanguageValid