]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/custom-validators/activitypub/videos.ts
Update server dependencies
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / activitypub / videos.ts
... / ...
CommitLineData
1import * as validator from 'validator'
2import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers/constants'
3import { peertubeTruncate } from '../../core-utils'
4import { exists, isArray, isBooleanValid, isDateValid, isUUIDValid } from '../misc'
5import {
6 isVideoDurationValid,
7 isVideoNameValid,
8 isVideoStateValid,
9 isVideoTagValid,
10 isVideoTruncatedDescriptionValid,
11 isVideoViewsValid
12} from '../videos'
13import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
14import { VideoState } from '../../../../shared/models/videos'
15import { logger } from '@server/helpers/logger'
16
17function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) {
18 return isBaseActivityValid(activity, 'Update') &&
19 sanitizeAndCheckVideoTorrentObject(activity.object)
20}
21
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') &&
28 isVideoDurationValid(value.replace(/[^0-9]+/g, ''))
29}
30
31function sanitizeAndCheckVideoTorrentObject (video: any) {
32 if (!video || video.type !== 'Video') return false
33
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 }
54
55 // Default attributes
56 if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED
57 if (!isBooleanValid(video.waitTranscoding)) video.waitTranscoding = false
58 if (!isBooleanValid(video.downloadEnabled)) video.downloadEnabled = true
59
60 return isActivityPubUrlValid(video.id) &&
61 isVideoNameValid(video.name) &&
62 isActivityPubVideoDurationValid(video.duration) &&
63 isUUIDValid(video.uuid) &&
64 (!video.category || isRemoteNumberIdentifierValid(video.category)) &&
65 (!video.licence || isRemoteNumberIdentifierValid(video.licence)) &&
66 (!video.language || isRemoteStringIdentifierValid(video.language)) &&
67 isVideoViewsValid(video.views) &&
68 isBooleanValid(video.sensitive) &&
69 isBooleanValid(video.commentsEnabled) &&
70 isBooleanValid(video.downloadEnabled) &&
71 isDateValid(video.published) &&
72 isDateValid(video.updated) &&
73 (!video.originallyPublishedAt || isDateValid(video.originallyPublishedAt)) &&
74 (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) &&
75 isRemoteVideoIconValid(video.icon) &&
76 video.url.length !== 0 &&
77 video.attributedTo.length !== 0
78}
79
80function isRemoteVideoUrlValid (url: any) {
81 return url.type === 'Link' &&
82 (
83 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mediaType) !== -1 &&
84 isActivityPubUrlValid(url.href) &&
85 validator.isInt(url.height + '', { min: 0 }) &&
86 validator.isInt(url.size + '', { min: 0 }) &&
87 (!url.fps || validator.isInt(url.fps + '', { min: -1 }))
88 ) ||
89 (
90 ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mediaType) !== -1 &&
91 isActivityPubUrlValid(url.href) &&
92 validator.isInt(url.height + '', { min: 0 })
93 ) ||
94 (
95 ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mediaType) !== -1 &&
96 validator.isLength(url.href, { min: 5 }) &&
97 validator.isInt(url.height + '', { min: 0 })
98 ) ||
99 (
100 (url.mediaType || url.mimeType) === 'application/x-mpegURL' &&
101 isActivityPubUrlValid(url.href) &&
102 isArray(url.tag)
103 )
104}
105
106// ---------------------------------------------------------------------------
107
108export {
109 sanitizeAndCheckVideoTorrentUpdateActivity,
110 isRemoteStringIdentifierValid,
111 sanitizeAndCheckVideoTorrentObject,
112 isRemoteVideoUrlValid
113}
114
115// ---------------------------------------------------------------------------
116
117function setValidRemoteTags (video: any) {
118 if (Array.isArray(video.tag) === false) return false
119
120 video.tag = video.tag.filter(t => {
121 return t.type === 'Hashtag' &&
122 isVideoTagValid(t.name)
123 })
124
125 return true
126}
127
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
140function isRemoteNumberIdentifierValid (data: any) {
141 return validator.isInt(data.identifier, { min: 0 })
142}
143
144function isRemoteStringIdentifierValid (data: any) {
145 return typeof data.identifier === 'string'
146}
147
148function isRemoteVideoContentValid (mediaType: string, content: string) {
149 return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content)
150}
151
152function isRemoteVideoIconValid (icon: any) {
153 return icon.type === 'Image' &&
154 isActivityPubUrlValid(icon.url) &&
155 icon.mediaType === 'image/jpeg' &&
156 validator.isInt(icon.width + '', { min: 0 }) &&
157 validator.isInt(icon.height + '', { min: 0 })
158}
159
160function setValidRemoteVideoUrls (video: any) {
161 if (Array.isArray(video.url) === false) return false
162
163 video.url = video.url.filter(u => isRemoteVideoUrlValid(u))
164
165 return true
166}
167
168function setRemoteVideoTruncatedContent (video: any) {
169 if (video.content) {
170 video.content = peertubeTruncate(video.content, { length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max })
171 }
172
173 return true
174}