1 import * as 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
60 return isActivityPubUrlValid(video.id) &&
61 isVideoNameValid(video.name) &&
62 isActivityPubVideoDurationValid(video.duration) &&
63 isUUIDValid(video.uuid) &&
64 (!video.category || isRemoteNumberIdentifierValid(video.category)) &&
65 (!video.licence || isRemoteNumberIdentifierValid(video.licence)) &&
66 (!video.language || isRemoteStringIdentifierValid(video.language)) &&
67 isVideoViewsValid(video.views) &&
68 isBooleanValid(video.sensitive) &&
69 isBooleanValid(video.commentsEnabled) &&
70 isBooleanValid(video.downloadEnabled) &&
71 isDateValid(video.published) &&
72 isDateValid(video.updated) &&
73 (!video.originallyPublishedAt || isDateValid(video.originallyPublishedAt)) &&
74 (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) &&
75 isRemoteVideoIconValid(video.icon) &&
76 video.url.length !== 0 &&
77 video.attributedTo.length !== 0
80 function isRemoteVideoUrlValid (url: any) {
81 return url.type === 'Link' &&
83 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mediaType) !== -1 &&
84 isActivityPubUrlValid(url.href) &&
85 validator.isInt(url.height + '', { min: 0 }) &&
86 validator.isInt(url.size + '', { min: 0 }) &&
87 (!url.fps || validator.isInt(url.fps + '', { min: -1 }))
90 ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mediaType) !== -1 &&
91 isActivityPubUrlValid(url.href) &&
92 validator.isInt(url.height + '', { min: 0 })
95 ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mediaType) !== -1 &&
96 validator.isLength(url.href, { min: 5 }) &&
97 validator.isInt(url.height + '', { min: 0 })
100 (url.mediaType || url.mimeType) === 'application/x-mpegURL' &&
101 isActivityPubUrlValid(url.href) &&
106 // ---------------------------------------------------------------------------
109 sanitizeAndCheckVideoTorrentUpdateActivity,
110 isRemoteStringIdentifierValid,
111 sanitizeAndCheckVideoTorrentObject,
112 isRemoteVideoUrlValid
115 // ---------------------------------------------------------------------------
117 function setValidRemoteTags (video: any) {
118 if (Array.isArray(video.tag) === false) return false
120 video.tag = video.tag.filter(t => {
121 return t.type === 'Hashtag' &&
122 isVideoTagValid(t.name)
128 function setValidRemoteCaptions (video: any) {
129 if (!video.subtitleLanguage) video.subtitleLanguage = []
131 if (Array.isArray(video.subtitleLanguage) === false) return false
133 video.subtitleLanguage = video.subtitleLanguage.filter(caption => {
134 return isRemoteStringIdentifierValid(caption)
140 function isRemoteNumberIdentifierValid (data: any) {
141 return validator.isInt(data.identifier, { min: 0 })
144 function isRemoteStringIdentifierValid (data: any) {
145 return typeof data.identifier === 'string'
148 function isRemoteVideoContentValid (mediaType: string, content: string) {
149 return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content)
152 function isRemoteVideoIconValid (icon: any) {
153 return icon.type === 'Image' &&
154 isActivityPubUrlValid(icon.url) &&
155 icon.mediaType === 'image/jpeg' &&
156 validator.isInt(icon.width + '', { min: 0 }) &&
157 validator.isInt(icon.height + '', { min: 0 })
160 function setValidRemoteVideoUrls (video: any) {
161 if (Array.isArray(video.url) === false) return false
163 video.url = video.url.filter(u => isRemoteVideoUrlValid(u))
168 function setRemoteVideoTruncatedContent (video: any) {
170 video.content = peertubeTruncate(video.content, { length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max })