1 import validator from 'validator'
2 import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers/constants'
3 import { peertubeTruncate } from '../../core-utils'
4 import { exists, isArray, isBooleanValid, isDateValid, isUUIDValid } from '../misc'
10 isVideoTruncatedDescriptionValid,
13 import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
14 import { VideoState } from '../../../../shared/models/videos'
15 import { logger } from '@server/helpers/logger'
17 function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) {
18 return isBaseActivityValid(activity, 'Update') &&
19 sanitizeAndCheckVideoTorrentObject(activity.object)
22 function isActivityPubVideoDurationValid (value: string) {
23 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
24 return exists(value) &&
25 typeof value === 'string' &&
26 value.startsWith('PT') &&
27 value.endsWith('S') &&
28 isVideoDurationValid(value.replace(/[^0-9]+/g, ''))
31 function sanitizeAndCheckVideoTorrentObject (video: any) {
32 if (!video || video.type !== 'Video') return false
34 if (!setValidRemoteTags(video)) {
35 logger.debug('Video has invalid tags', { video })
38 if (!setValidRemoteVideoUrls(video)) {
39 logger.debug('Video has invalid urls', { video })
42 if (!setRemoteVideoTruncatedContent(video)) {
43 logger.debug('Video has invalid content', { video })
46 if (!setValidAttributedTo(video)) {
47 logger.debug('Video has invalid attributedTo', { video })
50 if (!setValidRemoteCaptions(video)) {
51 logger.debug('Video has invalid captions', { video })
56 if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED
57 if (!isBooleanValid(video.waitTranscoding)) video.waitTranscoding = false
58 if (!isBooleanValid(video.downloadEnabled)) video.downloadEnabled = true
59 if (!isBooleanValid(video.commentsEnabled)) video.commentsEnabled = false
61 return isActivityPubUrlValid(video.id) &&
62 isVideoNameValid(video.name) &&
63 isActivityPubVideoDurationValid(video.duration) &&
64 isUUIDValid(video.uuid) &&
65 (!video.category || isRemoteNumberIdentifierValid(video.category)) &&
66 (!video.licence || isRemoteNumberIdentifierValid(video.licence)) &&
67 (!video.language || isRemoteStringIdentifierValid(video.language)) &&
68 isVideoViewsValid(video.views) &&
69 isBooleanValid(video.sensitive) &&
70 isBooleanValid(video.commentsEnabled) &&
71 isBooleanValid(video.downloadEnabled) &&
72 isDateValid(video.published) &&
73 isDateValid(video.updated) &&
74 (!video.originallyPublishedAt || isDateValid(video.originallyPublishedAt)) &&
75 (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) &&
76 isRemoteVideoIconValid(video.icon) &&
77 video.url.length !== 0 &&
78 video.attributedTo.length !== 0
81 function isRemoteVideoUrlValid (url: any) {
82 return url.type === 'Link' &&
84 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mediaType) !== -1 &&
85 isActivityPubUrlValid(url.href) &&
86 validator.isInt(url.height + '', { min: 0 }) &&
87 validator.isInt(url.size + '', { min: 0 }) &&
88 (!url.fps || validator.isInt(url.fps + '', { min: -1 }))
91 ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mediaType) !== -1 &&
92 isActivityPubUrlValid(url.href) &&
93 validator.isInt(url.height + '', { min: 0 })
96 ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mediaType) !== -1 &&
97 validator.isLength(url.href, { min: 5 }) &&
98 validator.isInt(url.height + '', { min: 0 })
101 (url.mediaType || url.mimeType) === 'application/x-mpegURL' &&
102 isActivityPubUrlValid(url.href) &&
107 // ---------------------------------------------------------------------------
110 sanitizeAndCheckVideoTorrentUpdateActivity,
111 isRemoteStringIdentifierValid,
112 sanitizeAndCheckVideoTorrentObject,
113 isRemoteVideoUrlValid
116 // ---------------------------------------------------------------------------
118 function setValidRemoteTags (video: any) {
119 if (Array.isArray(video.tag) === false) return false
121 video.tag = video.tag.filter(t => {
122 return t.type === 'Hashtag' &&
123 isVideoTagValid(t.name)
129 function setValidRemoteCaptions (video: any) {
130 if (!video.subtitleLanguage) video.subtitleLanguage = []
132 if (Array.isArray(video.subtitleLanguage) === false) return false
134 video.subtitleLanguage = video.subtitleLanguage.filter(caption => {
135 return isRemoteStringIdentifierValid(caption)
141 function isRemoteNumberIdentifierValid (data: any) {
142 return validator.isInt(data.identifier, { min: 0 })
145 function isRemoteStringIdentifierValid (data: any) {
146 return typeof data.identifier === 'string'
149 function isRemoteVideoContentValid (mediaType: string, content: string) {
150 return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content)
153 function isRemoteVideoIconValid (icon: any) {
154 return icon.type === 'Image' &&
155 isActivityPubUrlValid(icon.url) &&
156 icon.mediaType === 'image/jpeg' &&
157 validator.isInt(icon.width + '', { min: 0 }) &&
158 validator.isInt(icon.height + '', { min: 0 })
161 function setValidRemoteVideoUrls (video: any) {
162 if (Array.isArray(video.url) === false) return false
164 video.url = video.url.filter(u => isRemoteVideoUrlValid(u))
169 function setRemoteVideoTruncatedContent (video: any) {
171 video.content = peertubeTruncate(video.content, { length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max })