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