]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/activitypub/videos.ts
Don't expose constants directly in initializers/
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / activitypub / videos.ts
1 import * as validator from 'validator'
2 import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers/constants'
3 import { peertubeTruncate } from '../../core-utils'
4 import { exists, isArray, isBooleanValid, isDateValid, isUUIDValid } from '../misc'
5 import {
6 isVideoDurationValid,
7 isVideoNameValid,
8 isVideoStateValid,
9 isVideoTagValid,
10 isVideoTruncatedDescriptionValid,
11 isVideoViewsValid
12 } from '../videos'
13 import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
14 import { VideoState } from '../../../../shared/models/videos'
15
16 function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) {
17 return isBaseActivityValid(activity, 'Update') &&
18 sanitizeAndCheckVideoTorrentObject(activity.object)
19 }
20
21 function 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') &&
27 isVideoDurationValid(value.replace(/[^0-9]+/g, ''))
28 }
29
30 function sanitizeAndCheckVideoTorrentObject (video: any) {
31 if (!video || video.type !== 'Video') return false
32
33 if (!setValidRemoteTags(video)) return false
34 if (!setValidRemoteVideoUrls(video)) return false
35 if (!setRemoteVideoTruncatedContent(video)) return false
36 if (!setValidAttributedTo(video)) return false
37 if (!setValidRemoteCaptions(video)) return false
38
39 // Default attributes
40 if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED
41 if (!isBooleanValid(video.waitTranscoding)) video.waitTranscoding = false
42 if (!isBooleanValid(video.downloadEnabled)) video.downloadEnabled = true
43
44 return isActivityPubUrlValid(video.id) &&
45 isVideoNameValid(video.name) &&
46 isActivityPubVideoDurationValid(video.duration) &&
47 isUUIDValid(video.uuid) &&
48 (!video.category || isRemoteNumberIdentifierValid(video.category)) &&
49 (!video.licence || isRemoteNumberIdentifierValid(video.licence)) &&
50 (!video.language || isRemoteStringIdentifierValid(video.language)) &&
51 isVideoViewsValid(video.views) &&
52 isBooleanValid(video.sensitive) &&
53 isBooleanValid(video.commentsEnabled) &&
54 isBooleanValid(video.downloadEnabled) &&
55 isDateValid(video.published) &&
56 isDateValid(video.updated) &&
57 (!video.originallyPublishedAt || isDateValid(video.originallyPublishedAt)) &&
58 (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) &&
59 isRemoteVideoIconValid(video.icon) &&
60 video.url.length !== 0 &&
61 video.attributedTo.length !== 0
62 }
63
64 function 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 (
70 // TODO: remove mimeType (backward compatibility, introduced in v1.1.0)
71 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mediaType || url.mimeType) !== -1 &&
72 isActivityPubUrlValid(url.href) &&
73 validator.isInt(url.height + '', { min: 0 }) &&
74 validator.isInt(url.size + '', { min: 0 }) &&
75 (!url.fps || validator.isInt(url.fps + '', { min: -1 }))
76 ) ||
77 (
78 ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mediaType || url.mimeType) !== -1 &&
79 isActivityPubUrlValid(url.href) &&
80 validator.isInt(url.height + '', { min: 0 })
81 ) ||
82 (
83 ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mediaType || url.mimeType) !== -1 &&
84 validator.isLength(url.href, { min: 5 }) &&
85 validator.isInt(url.height + '', { min: 0 })
86 ) ||
87 (
88 (url.mediaType || url.mimeType) === 'application/x-mpegURL' &&
89 isActivityPubUrlValid(url.href) &&
90 isArray(url.tag)
91 )
92 }
93
94 // ---------------------------------------------------------------------------
95
96 export {
97 sanitizeAndCheckVideoTorrentUpdateActivity,
98 isRemoteStringIdentifierValid,
99 sanitizeAndCheckVideoTorrentObject,
100 isRemoteVideoUrlValid
101 }
102
103 // ---------------------------------------------------------------------------
104
105 function setValidRemoteTags (video: any) {
106 if (Array.isArray(video.tag) === false) return false
107
108 video.tag = video.tag.filter(t => {
109 return t.type === 'Hashtag' &&
110 isVideoTagValid(t.name)
111 })
112
113 return true
114 }
115
116 function 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
128 function isRemoteNumberIdentifierValid (data: any) {
129 return validator.isInt(data.identifier, { min: 0 })
130 }
131
132 function isRemoteStringIdentifierValid (data: any) {
133 return typeof data.identifier === 'string'
134 }
135
136 function isRemoteVideoContentValid (mediaType: string, content: string) {
137 return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content)
138 }
139
140 function isRemoteVideoIconValid (icon: any) {
141 return icon.type === 'Image' &&
142 isActivityPubUrlValid(icon.url) &&
143 icon.mediaType === 'image/jpeg' &&
144 validator.isInt(icon.width + '', { min: 0 }) &&
145 validator.isInt(icon.height + '', { min: 0 })
146 }
147
148 function setValidRemoteVideoUrls (video: any) {
149 if (Array.isArray(video.url) === false) return false
150
151 video.url = video.url.filter(u => isRemoteVideoUrlValid(u))
152
153 return true
154 }
155
156 function setRemoteVideoTruncatedContent (video: any) {
157 if (video.content) {
158 video.content = peertubeTruncate(video.content, CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max)
159 }
160
161 return true
162 }