]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/activitypub/videos.ts
Merge branch 'release/2.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / activitypub / videos.ts
CommitLineData
7cde3b9c 1import validator from 'validator'
74dc3bca 2import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers/constants'
c73e83da 3import { peertubeTruncate } from '../../core-utils'
09209296 4import { exists, isArray, isBooleanValid, isDateValid, isUUIDValid } from '../misc'
65fcc311 5import {
65fcc311 6 isVideoDurationValid,
65fcc311 7 isVideoNameValid,
2186386c 8 isVideoStateValid,
e34c85e5 9 isVideoTagValid,
8e10cf1a 10 isVideoTruncatedDescriptionValid,
8e10cf1a 11 isVideoViewsValid
65fcc311 12} from '../videos'
50d6de9c 13import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
2186386c 14import { VideoState } from '../../../../shared/models/videos'
d7a25329 15import { logger } from '@server/helpers/logger'
0d0e8dd0 16
1d6e5dfc 17function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) {
0d0e8dd0 18 return isBaseActivityValid(activity, 'Update') &&
1d6e5dfc 19 sanitizeAndCheckVideoTorrentObject(activity.object)
65fcc311
C
20}
21
8e10cf1a
C
22function 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') &&
efc32059 28 isVideoDurationValid(value.replace(/[^0-9]+/g, ''))
8e10cf1a
C
29}
30
1d6e5dfc 31function sanitizeAndCheckVideoTorrentObject (video: any) {
fbad87b0 32 if (!video || video.type !== 'Video') return false
5cf13500 33
d7a25329
C
34 if (!setValidRemoteTags(video)) {
35 logger.debug('Video has invalid tags', { video })
36 return false
37 }
38 if (!setValidRemoteVideoUrls(video)) {
39 logger.debug('Video has invalid urls', { video })
40 return false
41 }
42 if (!setRemoteVideoTruncatedContent(video)) {
43 logger.debug('Video has invalid content', { video })
44 return false
45 }
46 if (!setValidAttributedTo(video)) {
47 logger.debug('Video has invalid attributedTo', { video })
48 return false
49 }
50 if (!setValidRemoteCaptions(video)) {
51 logger.debug('Video has invalid captions', { video })
52 return false
53 }
ca6d3622
C
54 if (!setValidRemoteIcon(video)) {
55 logger.debug('Video has invalid icons', { video })
56 return false
57 }
1d6e5dfc 58
2186386c
C
59 // Default attributes
60 if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED
61 if (!isBooleanValid(video.waitTranscoding)) video.waitTranscoding = false
7f2cfe3a 62 if (!isBooleanValid(video.downloadEnabled)) video.downloadEnabled = true
0bc1b31d 63 if (!isBooleanValid(video.commentsEnabled)) video.commentsEnabled = false
2186386c 64
5cf13500 65 return isActivityPubUrlValid(video.id) &&
0d0e8dd0 66 isVideoNameValid(video.name) &&
8e10cf1a 67 isActivityPubVideoDurationValid(video.duration) &&
0d0e8dd0 68 isUUIDValid(video.uuid) &&
9d3ef9fe
C
69 (!video.category || isRemoteNumberIdentifierValid(video.category)) &&
70 (!video.licence || isRemoteNumberIdentifierValid(video.licence)) &&
71 (!video.language || isRemoteStringIdentifierValid(video.language)) &&
efc32059 72 isVideoViewsValid(video.views) &&
0a67e28b 73 isBooleanValid(video.sensitive) &&
47564bbe 74 isBooleanValid(video.commentsEnabled) &&
7f2cfe3a 75 isBooleanValid(video.downloadEnabled) &&
0d0e8dd0
C
76 isDateValid(video.published) &&
77 isDateValid(video.updated) &&
7519127b 78 (!video.originallyPublishedAt || isDateValid(video.originallyPublishedAt)) &&
f595d394 79 (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) &&
50d6de9c 80 video.url.length !== 0 &&
50d6de9c 81 video.attributedTo.length !== 0
65fcc311
C
82}
83
c48e82b5 84function isRemoteVideoUrlValid (url: any) {
c48e82b5
C
85 return url.type === 'Link' &&
86 (
d7a25329 87 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mediaType) !== -1 &&
c48e82b5
C
88 isActivityPubUrlValid(url.href) &&
89 validator.isInt(url.height + '', { min: 0 }) &&
90 validator.isInt(url.size + '', { min: 0 }) &&
a3737cbf 91 (!url.fps || validator.isInt(url.fps + '', { min: -1 }))
c48e82b5
C
92 ) ||
93 (
d7a25329 94 ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mediaType) !== -1 &&
c48e82b5
C
95 isActivityPubUrlValid(url.href) &&
96 validator.isInt(url.height + '', { min: 0 })
97 ) ||
98 (
d7a25329 99 ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mediaType) !== -1 &&
c48e82b5
C
100 validator.isLength(url.href, { min: 5 }) &&
101 validator.isInt(url.height + '', { min: 0 })
09209296
C
102 ) ||
103 (
104 (url.mediaType || url.mimeType) === 'application/x-mpegURL' &&
105 isActivityPubUrlValid(url.href) &&
106 isArray(url.tag)
c48e82b5
C
107 )
108}
109
65fcc311
C
110// ---------------------------------------------------------------------------
111
112export {
1d6e5dfc 113 sanitizeAndCheckVideoTorrentUpdateActivity,
9d3ef9fe 114 isRemoteStringIdentifierValid,
c48e82b5
C
115 sanitizeAndCheckVideoTorrentObject,
116 isRemoteVideoUrlValid
65fcc311
C
117}
118
119// ---------------------------------------------------------------------------
120
0d0e8dd0
C
121function setValidRemoteTags (video: any) {
122 if (Array.isArray(video.tag) === false) return false
65fcc311 123
a2431b7d 124 video.tag = video.tag.filter(t => {
0d0e8dd0
C
125 return t.type === 'Hashtag' &&
126 isVideoTagValid(t.name)
127 })
72c7248b 128
0d0e8dd0 129 return true
72c7248b
C
130}
131
40e87e9e
C
132function setValidRemoteCaptions (video: any) {
133 if (!video.subtitleLanguage) video.subtitleLanguage = []
134
135 if (Array.isArray(video.subtitleLanguage) === false) return false
136
137 video.subtitleLanguage = video.subtitleLanguage.filter(caption => {
ca6d3622
C
138 if (!isActivityPubUrlValid(caption.url)) caption.url = null
139
40e87e9e
C
140 return isRemoteStringIdentifierValid(caption)
141 })
142
143 return true
144}
145
9d3ef9fe 146function isRemoteNumberIdentifierValid (data: any) {
0d0e8dd0 147 return validator.isInt(data.identifier, { min: 0 })
72c7248b
C
148}
149
9d3ef9fe
C
150function isRemoteStringIdentifierValid (data: any) {
151 return typeof data.identifier === 'string'
152}
153
0d0e8dd0
C
154function isRemoteVideoContentValid (mediaType: string, content: string) {
155 return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content)
72c7248b
C
156}
157
ca6d3622
C
158function setValidRemoteIcon (video: any) {
159 if (video.icon && !isArray(video.icon)) video.icon = [ video.icon ]
160 if (!video.icon) video.icon = []
161
162 video.icon = video.icon.filter(icon => {
163 return icon.type === 'Image' &&
164 isActivityPubUrlValid(icon.url) &&
165 icon.mediaType === 'image/jpeg' &&
166 validator.isInt(icon.width + '', { min: 0 }) &&
167 validator.isInt(icon.height + '', { min: 0 })
168 })
169
170 return video.icon.length !== 0
72c7248b
C
171}
172
0d0e8dd0
C
173function setValidRemoteVideoUrls (video: any) {
174 if (Array.isArray(video.url) === false) return false
65fcc311 175
a2431b7d 176 video.url = video.url.filter(u => isRemoteVideoUrlValid(u))
65fcc311 177
0d0e8dd0 178 return true
65fcc311
C
179}
180
45cd28b6 181function setRemoteVideoTruncatedContent (video: any) {
c73e83da 182 if (video.content) {
687c6180 183 video.content = peertubeTruncate(video.content, { length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max })
c73e83da
C
184 }
185
186 return true
187}