]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/activitypub/videos.ts
Merge branch 'develop' into pr/1285
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / activitypub / videos.ts
CommitLineData
0d0e8dd0 1import * as validator from 'validator'
c73e83da
C
2import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers'
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'
0d0e8dd0 15
1d6e5dfc 16function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) {
0d0e8dd0 17 return isBaseActivityValid(activity, 'Update') &&
1d6e5dfc 18 sanitizeAndCheckVideoTorrentObject(activity.object)
65fcc311
C
19}
20
8e10cf1a
C
21function isActivityPubVideoDurationValid (value: string) {
22 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
23 return exists(value) &&
24 typeof value === 'string' &&
25 value.startsWith('PT') &&
26 value.endsWith('S') &&
efc32059 27 isVideoDurationValid(value.replace(/[^0-9]+/g, ''))
8e10cf1a
C
28}
29
1d6e5dfc 30function sanitizeAndCheckVideoTorrentObject (video: any) {
fbad87b0 31 if (!video || video.type !== 'Video') return false
5cf13500 32
1d6e5dfc
C
33 if (!setValidRemoteTags(video)) return false
34 if (!setValidRemoteVideoUrls(video)) return false
35 if (!setRemoteVideoTruncatedContent(video)) return false
36 if (!setValidAttributedTo(video)) return false
40e87e9e 37 if (!setValidRemoteCaptions(video)) return false
1d6e5dfc 38
2186386c
C
39 // Default attributes
40 if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED
41 if (!isBooleanValid(video.waitTranscoding)) video.waitTranscoding = false
7f2cfe3a 42 if (!isBooleanValid(video.downloadEnabled)) video.downloadEnabled = true
2186386c 43
5cf13500 44 return isActivityPubUrlValid(video.id) &&
0d0e8dd0 45 isVideoNameValid(video.name) &&
8e10cf1a 46 isActivityPubVideoDurationValid(video.duration) &&
0d0e8dd0 47 isUUIDValid(video.uuid) &&
9d3ef9fe
C
48 (!video.category || isRemoteNumberIdentifierValid(video.category)) &&
49 (!video.licence || isRemoteNumberIdentifierValid(video.licence)) &&
50 (!video.language || isRemoteStringIdentifierValid(video.language)) &&
efc32059 51 isVideoViewsValid(video.views) &&
0a67e28b 52 isBooleanValid(video.sensitive) &&
47564bbe 53 isBooleanValid(video.commentsEnabled) &&
7f2cfe3a 54 isBooleanValid(video.downloadEnabled) &&
0d0e8dd0
C
55 isDateValid(video.published) &&
56 isDateValid(video.updated) &&
f595d394 57 (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) &&
0d0e8dd0 58 isRemoteVideoIconValid(video.icon) &&
50d6de9c 59 video.url.length !== 0 &&
50d6de9c 60 video.attributedTo.length !== 0
65fcc311
C
61}
62
c48e82b5
C
63function isRemoteVideoUrlValid (url: any) {
64 // FIXME: Old bug, we used the width to represent the resolution. Remove it in a few release (currently beta.11)
65 if (url.width && !url.height) url.height = url.width
66
67 return url.type === 'Link' &&
68 (
e27ff5da
C
69 // TODO: remove mimeType (backward compatibility, introduced in v1.1.0)
70 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mediaType || url.mimeType) !== -1 &&
c48e82b5
C
71 isActivityPubUrlValid(url.href) &&
72 validator.isInt(url.height + '', { min: 0 }) &&
73 validator.isInt(url.size + '', { min: 0 }) &&
a3737cbf 74 (!url.fps || validator.isInt(url.fps + '', { min: -1 }))
c48e82b5
C
75 ) ||
76 (
e27ff5da 77 ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mediaType || url.mimeType) !== -1 &&
c48e82b5
C
78 isActivityPubUrlValid(url.href) &&
79 validator.isInt(url.height + '', { min: 0 })
80 ) ||
81 (
e27ff5da 82 ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mediaType || url.mimeType) !== -1 &&
c48e82b5
C
83 validator.isLength(url.href, { min: 5 }) &&
84 validator.isInt(url.height + '', { min: 0 })
09209296
C
85 ) ||
86 (
87 (url.mediaType || url.mimeType) === 'application/x-mpegURL' &&
88 isActivityPubUrlValid(url.href) &&
89 isArray(url.tag)
c48e82b5
C
90 )
91}
92
65fcc311
C
93// ---------------------------------------------------------------------------
94
95export {
1d6e5dfc 96 sanitizeAndCheckVideoTorrentUpdateActivity,
9d3ef9fe 97 isRemoteStringIdentifierValid,
c48e82b5
C
98 sanitizeAndCheckVideoTorrentObject,
99 isRemoteVideoUrlValid
65fcc311
C
100}
101
102// ---------------------------------------------------------------------------
103
0d0e8dd0
C
104function setValidRemoteTags (video: any) {
105 if (Array.isArray(video.tag) === false) return false
65fcc311 106
a2431b7d 107 video.tag = video.tag.filter(t => {
0d0e8dd0
C
108 return t.type === 'Hashtag' &&
109 isVideoTagValid(t.name)
110 })
72c7248b 111
0d0e8dd0 112 return true
72c7248b
C
113}
114
40e87e9e
C
115function setValidRemoteCaptions (video: any) {
116 if (!video.subtitleLanguage) video.subtitleLanguage = []
117
118 if (Array.isArray(video.subtitleLanguage) === false) return false
119
120 video.subtitleLanguage = video.subtitleLanguage.filter(caption => {
121 return isRemoteStringIdentifierValid(caption)
122 })
123
124 return true
125}
126
9d3ef9fe 127function isRemoteNumberIdentifierValid (data: any) {
0d0e8dd0 128 return validator.isInt(data.identifier, { min: 0 })
72c7248b
C
129}
130
9d3ef9fe
C
131function isRemoteStringIdentifierValid (data: any) {
132 return typeof data.identifier === 'string'
133}
134
0d0e8dd0
C
135function isRemoteVideoContentValid (mediaType: string, content: string) {
136 return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content)
72c7248b
C
137}
138
0d0e8dd0
C
139function isRemoteVideoIconValid (icon: any) {
140 return icon.type === 'Image' &&
a2431b7d 141 isActivityPubUrlValid(icon.url) &&
0d0e8dd0 142 icon.mediaType === 'image/jpeg' &&
efc32059
C
143 validator.isInt(icon.width + '', { min: 0 }) &&
144 validator.isInt(icon.height + '', { min: 0 })
72c7248b
C
145}
146
0d0e8dd0
C
147function setValidRemoteVideoUrls (video: any) {
148 if (Array.isArray(video.url) === false) return false
65fcc311 149
a2431b7d 150 video.url = video.url.filter(u => isRemoteVideoUrlValid(u))
65fcc311 151
0d0e8dd0 152 return true
65fcc311
C
153}
154
45cd28b6 155function setRemoteVideoTruncatedContent (video: any) {
c73e83da
C
156 if (video.content) {
157 video.content = peertubeTruncate(video.content, CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max)
158 }
159
160 return true
161}