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