]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/activitypub/videos.ts
Merge branch 'release/1.4.0' into develop
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / activitypub / videos.ts
CommitLineData
0d0e8dd0 1import * as 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'
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) &&
7519127b 57 (!video.originallyPublishedAt || isDateValid(video.originallyPublishedAt)) &&
f595d394 58 (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) &&
0d0e8dd0 59 isRemoteVideoIconValid(video.icon) &&
50d6de9c 60 video.url.length !== 0 &&
50d6de9c 61 video.attributedTo.length !== 0
65fcc311
C
62}
63
c48e82b5
C
64function isRemoteVideoUrlValid (url: any) {
65 // FIXME: Old bug, we used the width to represent the resolution. Remove it in a few release (currently beta.11)
66 if (url.width && !url.height) url.height = url.width
67
68 return url.type === 'Link' &&
69 (
e27ff5da
C
70 // TODO: remove mimeType (backward compatibility, introduced in v1.1.0)
71 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mediaType || url.mimeType) !== -1 &&
c48e82b5
C
72 isActivityPubUrlValid(url.href) &&
73 validator.isInt(url.height + '', { min: 0 }) &&
74 validator.isInt(url.size + '', { min: 0 }) &&
a3737cbf 75 (!url.fps || validator.isInt(url.fps + '', { min: -1 }))
c48e82b5
C
76 ) ||
77 (
e27ff5da 78 ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mediaType || url.mimeType) !== -1 &&
c48e82b5
C
79 isActivityPubUrlValid(url.href) &&
80 validator.isInt(url.height + '', { min: 0 })
81 ) ||
82 (
e27ff5da 83 ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mediaType || url.mimeType) !== -1 &&
c48e82b5
C
84 validator.isLength(url.href, { min: 5 }) &&
85 validator.isInt(url.height + '', { min: 0 })
09209296
C
86 ) ||
87 (
88 (url.mediaType || url.mimeType) === 'application/x-mpegURL' &&
89 isActivityPubUrlValid(url.href) &&
90 isArray(url.tag)
c48e82b5
C
91 )
92}
93
65fcc311
C
94// ---------------------------------------------------------------------------
95
96export {
1d6e5dfc 97 sanitizeAndCheckVideoTorrentUpdateActivity,
9d3ef9fe 98 isRemoteStringIdentifierValid,
c48e82b5
C
99 sanitizeAndCheckVideoTorrentObject,
100 isRemoteVideoUrlValid
65fcc311
C
101}
102
103// ---------------------------------------------------------------------------
104
0d0e8dd0
C
105function setValidRemoteTags (video: any) {
106 if (Array.isArray(video.tag) === false) return false
65fcc311 107
a2431b7d 108 video.tag = video.tag.filter(t => {
0d0e8dd0
C
109 return t.type === 'Hashtag' &&
110 isVideoTagValid(t.name)
111 })
72c7248b 112
0d0e8dd0 113 return true
72c7248b
C
114}
115
40e87e9e
C
116function setValidRemoteCaptions (video: any) {
117 if (!video.subtitleLanguage) video.subtitleLanguage = []
118
119 if (Array.isArray(video.subtitleLanguage) === false) return false
120
121 video.subtitleLanguage = video.subtitleLanguage.filter(caption => {
122 return isRemoteStringIdentifierValid(caption)
123 })
124
125 return true
126}
127
9d3ef9fe 128function isRemoteNumberIdentifierValid (data: any) {
0d0e8dd0 129 return validator.isInt(data.identifier, { min: 0 })
72c7248b
C
130}
131
9d3ef9fe
C
132function isRemoteStringIdentifierValid (data: any) {
133 return typeof data.identifier === 'string'
134}
135
0d0e8dd0
C
136function isRemoteVideoContentValid (mediaType: string, content: string) {
137 return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content)
72c7248b
C
138}
139
0d0e8dd0
C
140function isRemoteVideoIconValid (icon: any) {
141 return icon.type === 'Image' &&
a2431b7d 142 isActivityPubUrlValid(icon.url) &&
0d0e8dd0 143 icon.mediaType === 'image/jpeg' &&
efc32059
C
144 validator.isInt(icon.width + '', { min: 0 }) &&
145 validator.isInt(icon.height + '', { min: 0 })
72c7248b
C
146}
147
0d0e8dd0
C
148function setValidRemoteVideoUrls (video: any) {
149 if (Array.isArray(video.url) === false) return false
65fcc311 150
a2431b7d 151 video.url = video.url.filter(u => isRemoteVideoUrlValid(u))
65fcc311 152
0d0e8dd0 153 return true
65fcc311
C
154}
155
45cd28b6 156function setRemoteVideoTruncatedContent (video: any) {
c73e83da
C
157 if (video.content) {
158 video.content = peertubeTruncate(video.content, CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max)
159 }
160
161 return true
162}