]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/custom-validators/activitypub/videos.ts
Add latency setting support
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / activitypub / videos.ts
index 0362f43ab2b7a3c521d0497e85ad0d9677671d26..80a32111704a8e59e19fdf78ac1f33a0963e73c0 100644 (file)
@@ -1,7 +1,11 @@
-import * as validator from 'validator'
-import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers'
+import validator from 'validator'
+import { logger } from '@server/helpers/logger'
+import { ActivityTrackerUrlObject, ActivityVideoFileMetadataUrlObject } from '@shared/models'
+import { LiveVideoLatencyMode, VideoState } from '../../../../shared/models/videos'
+import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers/constants'
 import { peertubeTruncate } from '../../core-utils'
-import { exists, isBooleanValid, isDateValid, isUUIDValid } from '../misc'
+import { exists, isArray, isBooleanValid, isDateValid, isUUIDValid } from '../misc'
+import { isLiveLatencyModeValid } from '../video-lives'
 import {
   isVideoDurationValid,
   isVideoNameValid,
@@ -11,30 +15,12 @@ import {
   isVideoViewsValid
 } from '../videos'
 import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
-import { VideoState } from '../../../../shared/models/videos'
-import { isVideoAbuseReasonValid } from '../video-abuses'
-
-function sanitizeAndCheckVideoTorrentCreateActivity (activity: any) {
-  return isBaseActivityValid(activity, 'Create') &&
-    sanitizeAndCheckVideoTorrentObject(activity.object)
-}
 
 function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) {
   return isBaseActivityValid(activity, 'Update') &&
     sanitizeAndCheckVideoTorrentObject(activity.object)
 }
 
-function isVideoTorrentDeleteActivityValid (activity: any) {
-  return isBaseActivityValid(activity, 'Delete')
-}
-
-function isVideoFlagValid (activity: any) {
-  return isBaseActivityValid(activity, 'Create') &&
-    activity.object.type === 'Flag' &&
-    isVideoAbuseReasonValid(activity.object.content) &&
-    isActivityPubUrlValid(activity.object.object)
-}
-
 function isActivityPubVideoDurationValid (value: string) {
   // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
   return exists(value) &&
@@ -47,15 +33,40 @@ function isActivityPubVideoDurationValid (value: string) {
 function sanitizeAndCheckVideoTorrentObject (video: any) {
   if (!video || video.type !== 'Video') return false
 
-  if (!setValidRemoteTags(video)) return false
-  if (!setValidRemoteVideoUrls(video)) return false
-  if (!setRemoteVideoTruncatedContent(video)) return false
-  if (!setValidAttributedTo(video)) return false
-  if (!setValidRemoteCaptions(video)) return false
+  if (!setValidRemoteTags(video)) {
+    logger.debug('Video has invalid tags', { video })
+    return false
+  }
+  if (!setValidRemoteVideoUrls(video)) {
+    logger.debug('Video has invalid urls', { video })
+    return false
+  }
+  if (!setRemoteVideoTruncatedContent(video)) {
+    logger.debug('Video has invalid content', { video })
+    return false
+  }
+  if (!setValidAttributedTo(video)) {
+    logger.debug('Video has invalid attributedTo', { video })
+    return false
+  }
+  if (!setValidRemoteCaptions(video)) {
+    logger.debug('Video has invalid captions', { video })
+    return false
+  }
+  if (!setValidRemoteIcon(video)) {
+    logger.debug('Video has invalid icons', { video })
+    return false
+  }
 
   // Default attributes
   if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED
   if (!isBooleanValid(video.waitTranscoding)) video.waitTranscoding = false
+  if (!isBooleanValid(video.downloadEnabled)) video.downloadEnabled = true
+  if (!isBooleanValid(video.commentsEnabled)) video.commentsEnabled = false
+  if (!isBooleanValid(video.isLiveBroadcast)) video.isLiveBroadcast = false
+  if (!isBooleanValid(video.liveSaveReplay)) video.liveSaveReplay = false
+  if (!isBooleanValid(video.permanentLive)) video.permanentLive = false
+  if (!isLiveLatencyModeValid(video.latencyMode)) video.latencyMode = LiveVideoLatencyMode.DEFAULT
 
   return isActivityPubUrlValid(video.id) &&
     isVideoNameValid(video.name) &&
@@ -66,24 +77,67 @@ function sanitizeAndCheckVideoTorrentObject (video: any) {
     (!video.language || isRemoteStringIdentifierValid(video.language)) &&
     isVideoViewsValid(video.views) &&
     isBooleanValid(video.sensitive) &&
-    isBooleanValid(video.commentsEnabled) &&
     isDateValid(video.published) &&
     isDateValid(video.updated) &&
+    (!video.originallyPublishedAt || isDateValid(video.originallyPublishedAt)) &&
     (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) &&
-    isRemoteVideoIconValid(video.icon) &&
-    video.url.length !== 0 &&
     video.attributedTo.length !== 0
 }
 
+function isRemoteVideoUrlValid (url: any) {
+  return url.type === 'Link' &&
+    // Video file link
+    (
+      ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.includes(url.mediaType) &&
+      isActivityPubUrlValid(url.href) &&
+      validator.isInt(url.height + '', { min: 0 }) &&
+      validator.isInt(url.size + '', { min: 0 }) &&
+      (!url.fps || validator.isInt(url.fps + '', { min: -1 }))
+    ) ||
+    // Torrent link
+    (
+      ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.includes(url.mediaType) &&
+      isActivityPubUrlValid(url.href) &&
+      validator.isInt(url.height + '', { min: 0 })
+    ) ||
+    // Magnet link
+    (
+      ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.includes(url.mediaType) &&
+      validator.isLength(url.href, { min: 5 }) &&
+      validator.isInt(url.height + '', { min: 0 })
+    ) ||
+    // HLS playlist link
+    (
+      (url.mediaType || url.mimeType) === 'application/x-mpegURL' &&
+      isActivityPubUrlValid(url.href) &&
+      isArray(url.tag)
+    ) ||
+    isAPVideoTrackerUrlObject(url) ||
+    isAPVideoFileUrlMetadataObject(url)
+}
+
+function isAPVideoFileUrlMetadataObject (url: any): url is ActivityVideoFileMetadataUrlObject {
+  return url &&
+    url.type === 'Link' &&
+    url.mediaType === 'application/json' &&
+    isArray(url.rel) && url.rel.includes('metadata')
+}
+
+function isAPVideoTrackerUrlObject (url: any): url is ActivityTrackerUrlObject {
+  return isArray(url.rel) &&
+    url.rel.includes('tracker') &&
+    isActivityPubUrlValid(url.href)
+}
+
 // ---------------------------------------------------------------------------
 
 export {
-  sanitizeAndCheckVideoTorrentCreateActivity,
   sanitizeAndCheckVideoTorrentUpdateActivity,
-  isVideoTorrentDeleteActivityValid,
   isRemoteStringIdentifierValid,
-  isVideoFlagValid,
-  sanitizeAndCheckVideoTorrentObject
+  sanitizeAndCheckVideoTorrentObject,
+  isRemoteVideoUrlValid,
+  isAPVideoFileUrlMetadataObject,
+  isAPVideoTrackerUrlObject
 }
 
 // ---------------------------------------------------------------------------
@@ -105,6 +159,8 @@ function setValidRemoteCaptions (video: any) {
   if (Array.isArray(video.subtitleLanguage) === false) return false
 
   video.subtitleLanguage = video.subtitleLanguage.filter(caption => {
+    if (!isActivityPubUrlValid(caption.url)) caption.url = null
+
     return isRemoteStringIdentifierValid(caption)
   })
 
@@ -123,12 +179,19 @@ function isRemoteVideoContentValid (mediaType: string, content: string) {
   return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content)
 }
 
-function isRemoteVideoIconValid (icon: any) {
-  return icon.type === 'Image' &&
-    isActivityPubUrlValid(icon.url) &&
-    icon.mediaType === 'image/jpeg' &&
-    validator.isInt(icon.width + '', { min: 0 }) &&
-    validator.isInt(icon.height + '', { min: 0 })
+function setValidRemoteIcon (video: any) {
+  if (video.icon && !isArray(video.icon)) video.icon = [ video.icon ]
+  if (!video.icon) video.icon = []
+
+  video.icon = video.icon.filter(icon => {
+    return icon.type === 'Image' &&
+      isActivityPubUrlValid(icon.url) &&
+      icon.mediaType === 'image/jpeg' &&
+      validator.isInt(icon.width + '', { min: 0 }) &&
+      validator.isInt(icon.height + '', { min: 0 })
+  })
+
+  return video.icon.length !== 0
 }
 
 function setValidRemoteVideoUrls (video: any) {
@@ -141,32 +204,8 @@ function setValidRemoteVideoUrls (video: any) {
 
 function setRemoteVideoTruncatedContent (video: any) {
   if (video.content) {
-    video.content = peertubeTruncate(video.content, CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max)
+    video.content = peertubeTruncate(video.content, { length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max })
   }
 
   return true
 }
-
-function isRemoteVideoUrlValid (url: any) {
-  // FIXME: Old bug, we used the width to represent the resolution. Remove it in a few realease (currently beta.11)
-  if (url.width && !url.height) url.height = url.width
-
-  return url.type === 'Link' &&
-    (
-      ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mimeType) !== -1 &&
-      isActivityPubUrlValid(url.href) &&
-      validator.isInt(url.height + '', { min: 0 }) &&
-      validator.isInt(url.size + '', { min: 0 }) &&
-      (!url.fps || validator.isInt(url.fps + '', { min: 0 }))
-    ) ||
-    (
-      ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mimeType) !== -1 &&
-      isActivityPubUrlValid(url.href) &&
-      validator.isInt(url.height + '', { min: 0 })
-    ) ||
-    (
-      ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mimeType) !== -1 &&
-      validator.isLength(url.href, { min: 5 }) &&
-      validator.isInt(url.height + '', { min: 0 })
-    )
-}