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