]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/activitypub/videos.ts
Add ability to list redundancies
[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'
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
d7a25329
C
34 if (!setValidRemoteTags(video)) {
35 logger.debug('Video has invalid tags', { video })
36 return false
37 }
38 if (!setValidRemoteVideoUrls(video)) {
39 logger.debug('Video has invalid urls', { video })
40 return false
41 }
42 if (!setRemoteVideoTruncatedContent(video)) {
43 logger.debug('Video has invalid content', { video })
44 return false
45 }
46 if (!setValidAttributedTo(video)) {
47 logger.debug('Video has invalid attributedTo', { video })
48 return false
49 }
50 if (!setValidRemoteCaptions(video)) {
51 logger.debug('Video has invalid captions', { video })
52 return false
53 }
1d6e5dfc 54
2186386c
C
55 // Default attributes
56 if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED
57 if (!isBooleanValid(video.waitTranscoding)) video.waitTranscoding = false
7f2cfe3a 58 if (!isBooleanValid(video.downloadEnabled)) video.downloadEnabled = true
2186386c 59
5cf13500 60 return isActivityPubUrlValid(video.id) &&
0d0e8dd0 61 isVideoNameValid(video.name) &&
8e10cf1a 62 isActivityPubVideoDurationValid(video.duration) &&
0d0e8dd0 63 isUUIDValid(video.uuid) &&
9d3ef9fe
C
64 (!video.category || isRemoteNumberIdentifierValid(video.category)) &&
65 (!video.licence || isRemoteNumberIdentifierValid(video.licence)) &&
66 (!video.language || isRemoteStringIdentifierValid(video.language)) &&
efc32059 67 isVideoViewsValid(video.views) &&
0a67e28b 68 isBooleanValid(video.sensitive) &&
47564bbe 69 isBooleanValid(video.commentsEnabled) &&
7f2cfe3a 70 isBooleanValid(video.downloadEnabled) &&
0d0e8dd0
C
71 isDateValid(video.published) &&
72 isDateValid(video.updated) &&
7519127b 73 (!video.originallyPublishedAt || isDateValid(video.originallyPublishedAt)) &&
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 80function isRemoteVideoUrlValid (url: any) {
c48e82b5
C
81 return url.type === 'Link' &&
82 (
d7a25329 83 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mediaType) !== -1 &&
c48e82b5
C
84 isActivityPubUrlValid(url.href) &&
85 validator.isInt(url.height + '', { min: 0 }) &&
86 validator.isInt(url.size + '', { min: 0 }) &&
a3737cbf 87 (!url.fps || validator.isInt(url.fps + '', { min: -1 }))
c48e82b5
C
88 ) ||
89 (
d7a25329 90 ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mediaType) !== -1 &&
c48e82b5
C
91 isActivityPubUrlValid(url.href) &&
92 validator.isInt(url.height + '', { min: 0 })
93 ) ||
94 (
d7a25329 95 ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mediaType) !== -1 &&
c48e82b5
C
96 validator.isLength(url.href, { min: 5 }) &&
97 validator.isInt(url.height + '', { min: 0 })
09209296
C
98 ) ||
99 (
100 (url.mediaType || url.mimeType) === 'application/x-mpegURL' &&
101 isActivityPubUrlValid(url.href) &&
102 isArray(url.tag)
c48e82b5
C
103 )
104}
105
65fcc311
C
106// ---------------------------------------------------------------------------
107
108export {
1d6e5dfc 109 sanitizeAndCheckVideoTorrentUpdateActivity,
9d3ef9fe 110 isRemoteStringIdentifierValid,
c48e82b5
C
111 sanitizeAndCheckVideoTorrentObject,
112 isRemoteVideoUrlValid
65fcc311
C
113}
114
115// ---------------------------------------------------------------------------
116
0d0e8dd0
C
117function setValidRemoteTags (video: any) {
118 if (Array.isArray(video.tag) === false) return false
65fcc311 119
a2431b7d 120 video.tag = video.tag.filter(t => {
0d0e8dd0
C
121 return t.type === 'Hashtag' &&
122 isVideoTagValid(t.name)
123 })
72c7248b 124
0d0e8dd0 125 return true
72c7248b
C
126}
127
40e87e9e
C
128function setValidRemoteCaptions (video: any) {
129 if (!video.subtitleLanguage) video.subtitleLanguage = []
130
131 if (Array.isArray(video.subtitleLanguage) === false) return false
132
133 video.subtitleLanguage = video.subtitleLanguage.filter(caption => {
134 return isRemoteStringIdentifierValid(caption)
135 })
136
137 return true
138}
139
9d3ef9fe 140function isRemoteNumberIdentifierValid (data: any) {
0d0e8dd0 141 return validator.isInt(data.identifier, { min: 0 })
72c7248b
C
142}
143
9d3ef9fe
C
144function isRemoteStringIdentifierValid (data: any) {
145 return typeof data.identifier === 'string'
146}
147
0d0e8dd0
C
148function isRemoteVideoContentValid (mediaType: string, content: string) {
149 return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content)
72c7248b
C
150}
151
0d0e8dd0
C
152function isRemoteVideoIconValid (icon: any) {
153 return icon.type === 'Image' &&
a2431b7d 154 isActivityPubUrlValid(icon.url) &&
0d0e8dd0 155 icon.mediaType === 'image/jpeg' &&
efc32059
C
156 validator.isInt(icon.width + '', { min: 0 }) &&
157 validator.isInt(icon.height + '', { min: 0 })
72c7248b
C
158}
159
0d0e8dd0
C
160function setValidRemoteVideoUrls (video: any) {
161 if (Array.isArray(video.url) === false) return false
65fcc311 162
a2431b7d 163 video.url = video.url.filter(u => isRemoteVideoUrlValid(u))
65fcc311 164
0d0e8dd0 165 return true
65fcc311
C
166}
167
45cd28b6 168function setRemoteVideoTruncatedContent (video: any) {
c73e83da 169 if (video.content) {
687c6180 170 video.content = peertubeTruncate(video.content, { length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max })
c73e83da
C
171 }
172
173 return true
174}