1 import validator from 'validator'
2 import { logger } from '@server/helpers/logger'
3 import { ActivityTrackerUrlObject, ActivityVideoFileMetadataUrlObject } from '@shared/models'
4 import { LiveVideoLatencyMode, VideoState } from '../../../../shared/models/videos'
5 import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers/constants'
6 import { peertubeTruncate } from '../../core-utils'
7 import { isArray, isBooleanValid, isDateValid, isUUIDValid } from '../misc'
8 import { isLiveLatencyModeValid } from '../video-lives'
10 isVideoDescriptionValid,
17 import { isActivityPubUrlValid, isActivityPubVideoDurationValid, isBaseActivityValid, setValidAttributedTo } from './misc'
19 function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) {
20 return isBaseActivityValid(activity, 'Update') &&
21 sanitizeAndCheckVideoTorrentObject(activity.object)
24 function sanitizeAndCheckVideoTorrentObject (video: any) {
25 if (!video || video.type !== 'Video') return false
27 if (!setValidRemoteTags(video)) {
28 logger.debug('Video has invalid tags', { video })
31 if (!setValidRemoteVideoUrls(video)) {
32 logger.debug('Video has invalid urls', { video })
35 if (!setRemoteVideoContent(video)) {
36 logger.debug('Video has invalid content', { video })
39 if (!setValidAttributedTo(video)) {
40 logger.debug('Video has invalid attributedTo', { video })
43 if (!setValidRemoteCaptions(video)) {
44 logger.debug('Video has invalid captions', { video })
47 if (!setValidRemoteIcon(video)) {
48 logger.debug('Video has invalid icons', { video })
53 if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED
54 if (!isBooleanValid(video.waitTranscoding)) video.waitTranscoding = false
55 if (!isBooleanValid(video.downloadEnabled)) video.downloadEnabled = true
56 if (!isBooleanValid(video.commentsEnabled)) video.commentsEnabled = false
57 if (!isBooleanValid(video.isLiveBroadcast)) video.isLiveBroadcast = false
58 if (!isBooleanValid(video.liveSaveReplay)) video.liveSaveReplay = false
59 if (!isBooleanValid(video.permanentLive)) video.permanentLive = false
60 if (!isLiveLatencyModeValid(video.latencyMode)) video.latencyMode = LiveVideoLatencyMode.DEFAULT
62 return isActivityPubUrlValid(video.id) &&
63 isVideoNameValid(video.name) &&
64 isActivityPubVideoDurationValid(video.duration) &&
65 isVideoDurationValid(video.duration.replace(/[^0-9]+/g, '')) &&
66 isUUIDValid(video.uuid) &&
67 (!video.category || isRemoteNumberIdentifierValid(video.category)) &&
68 (!video.licence || isRemoteNumberIdentifierValid(video.licence)) &&
69 (!video.language || isRemoteStringIdentifierValid(video.language)) &&
70 isVideoViewsValid(video.views) &&
71 isBooleanValid(video.sensitive) &&
72 isDateValid(video.published) &&
73 isDateValid(video.updated) &&
74 (!video.originallyPublishedAt || isDateValid(video.originallyPublishedAt)) &&
75 (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) &&
76 video.attributedTo.length !== 0
79 function isRemoteVideoUrlValid (url: any) {
80 return url.type === 'Link' &&
83 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.includes(url.mediaType) &&
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 }))
91 ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.includes(url.mediaType) &&
92 isActivityPubUrlValid(url.href) &&
93 validator.isInt(url.height + '', { min: 0 })
97 ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.includes(url.mediaType) &&
98 validator.isLength(url.href, { min: 5 }) &&
99 validator.isInt(url.height + '', { min: 0 })
103 (url.mediaType || url.mimeType) === 'application/x-mpegURL' &&
104 isActivityPubUrlValid(url.href) &&
107 isAPVideoTrackerUrlObject(url) ||
108 isAPVideoFileUrlMetadataObject(url)
111 function isAPVideoFileUrlMetadataObject (url: any): url is ActivityVideoFileMetadataUrlObject {
113 url.type === 'Link' &&
114 url.mediaType === 'application/json' &&
115 isArray(url.rel) && url.rel.includes('metadata')
118 function isAPVideoTrackerUrlObject (url: any): url is ActivityTrackerUrlObject {
119 return isArray(url.rel) &&
120 url.rel.includes('tracker') &&
121 isActivityPubUrlValid(url.href)
124 // ---------------------------------------------------------------------------
127 sanitizeAndCheckVideoTorrentUpdateActivity,
128 isRemoteStringIdentifierValid,
129 sanitizeAndCheckVideoTorrentObject,
130 isRemoteVideoUrlValid,
131 isAPVideoFileUrlMetadataObject,
132 isAPVideoTrackerUrlObject
135 // ---------------------------------------------------------------------------
137 function setValidRemoteTags (video: any) {
138 if (Array.isArray(video.tag) === false) return false
140 video.tag = video.tag.filter(t => {
141 return t.type === 'Hashtag' &&
142 isVideoTagValid(t.name)
148 function setValidRemoteCaptions (video: any) {
149 if (!video.subtitleLanguage) video.subtitleLanguage = []
151 if (Array.isArray(video.subtitleLanguage) === false) return false
153 video.subtitleLanguage = video.subtitleLanguage.filter(caption => {
154 if (!isActivityPubUrlValid(caption.url)) caption.url = null
156 return isRemoteStringIdentifierValid(caption)
162 function isRemoteNumberIdentifierValid (data: any) {
163 return validator.isInt(data.identifier, { min: 0 })
166 function isRemoteStringIdentifierValid (data: any) {
167 return typeof data.identifier === 'string'
170 function isRemoteVideoContentValid (mediaType: string, content: string) {
171 return mediaType === 'text/markdown' && isVideoDescriptionValid(content)
174 function setValidRemoteIcon (video: any) {
175 if (video.icon && !isArray(video.icon)) video.icon = [ video.icon ]
176 if (!video.icon) video.icon = []
178 video.icon = video.icon.filter(icon => {
179 return icon.type === 'Image' &&
180 isActivityPubUrlValid(icon.url) &&
181 icon.mediaType === 'image/jpeg' &&
182 validator.isInt(icon.width + '', { min: 0 }) &&
183 validator.isInt(icon.height + '', { min: 0 })
186 return video.icon.length !== 0
189 function setValidRemoteVideoUrls (video: any) {
190 if (Array.isArray(video.url) === false) return false
192 video.url = video.url.filter(u => isRemoteVideoUrlValid(u))
197 function setRemoteVideoContent (video: any) {
199 video.content = peertubeTruncate(video.content, { length: CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max })