]>
Commit | Line | Data |
---|---|---|
1 | import * as validator from 'validator' | |
2 | import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers' | |
3 | import { peertubeTruncate } from '../../core-utils' | |
4 | import { exists, 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 | import { isVideoAbuseReasonValid } from '../video-abuses' | |
16 | ||
17 | function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) { | |
18 | return isBaseActivityValid(activity, 'Update') && | |
19 | sanitizeAndCheckVideoTorrentObject(activity.object) | |
20 | } | |
21 | ||
22 | function 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 | ||
31 | function sanitizeAndCheckVideoTorrentObject (video: any) { | |
32 | if (!video || video.type !== 'Video') return false | |
33 | ||
34 | if (!setValidRemoteTags(video)) return false | |
35 | if (!setValidRemoteVideoUrls(video)) return false | |
36 | if (!setRemoteVideoTruncatedContent(video)) return false | |
37 | if (!setValidAttributedTo(video)) return false | |
38 | if (!setValidRemoteCaptions(video)) return false | |
39 | ||
40 | // Default attributes | |
41 | if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED | |
42 | if (!isBooleanValid(video.waitTranscoding)) video.waitTranscoding = false | |
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 | isDateValid(video.published) && | |
55 | isDateValid(video.updated) && | |
56 | (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) && | |
57 | isRemoteVideoIconValid(video.icon) && | |
58 | video.url.length !== 0 && | |
59 | video.attributedTo.length !== 0 | |
60 | } | |
61 | ||
62 | function 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 | ( | |
68 | // TODO: remove mimeType (backward compatibility, introduced in v1.1.0) | |
69 | ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mediaType || url.mimeType) !== -1 && | |
70 | isActivityPubUrlValid(url.href) && | |
71 | validator.isInt(url.height + '', { min: 0 }) && | |
72 | validator.isInt(url.size + '', { min: 0 }) && | |
73 | (!url.fps || validator.isInt(url.fps + '', { min: -1 })) | |
74 | ) || | |
75 | ( | |
76 | ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mediaType || url.mimeType) !== -1 && | |
77 | isActivityPubUrlValid(url.href) && | |
78 | validator.isInt(url.height + '', { min: 0 }) | |
79 | ) || | |
80 | ( | |
81 | ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mediaType || url.mimeType) !== -1 && | |
82 | validator.isLength(url.href, { min: 5 }) && | |
83 | validator.isInt(url.height + '', { min: 0 }) | |
84 | ) | |
85 | } | |
86 | ||
87 | // --------------------------------------------------------------------------- | |
88 | ||
89 | export { | |
90 | sanitizeAndCheckVideoTorrentUpdateActivity, | |
91 | isRemoteStringIdentifierValid, | |
92 | sanitizeAndCheckVideoTorrentObject, | |
93 | isRemoteVideoUrlValid | |
94 | } | |
95 | ||
96 | // --------------------------------------------------------------------------- | |
97 | ||
98 | function setValidRemoteTags (video: any) { | |
99 | if (Array.isArray(video.tag) === false) return false | |
100 | ||
101 | video.tag = video.tag.filter(t => { | |
102 | return t.type === 'Hashtag' && | |
103 | isVideoTagValid(t.name) | |
104 | }) | |
105 | ||
106 | return true | |
107 | } | |
108 | ||
109 | function 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 | ||
121 | function isRemoteNumberIdentifierValid (data: any) { | |
122 | return validator.isInt(data.identifier, { min: 0 }) | |
123 | } | |
124 | ||
125 | function isRemoteStringIdentifierValid (data: any) { | |
126 | return typeof data.identifier === 'string' | |
127 | } | |
128 | ||
129 | function isRemoteVideoContentValid (mediaType: string, content: string) { | |
130 | return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content) | |
131 | } | |
132 | ||
133 | function isRemoteVideoIconValid (icon: any) { | |
134 | return icon.type === 'Image' && | |
135 | isActivityPubUrlValid(icon.url) && | |
136 | icon.mediaType === 'image/jpeg' && | |
137 | validator.isInt(icon.width + '', { min: 0 }) && | |
138 | validator.isInt(icon.height + '', { min: 0 }) | |
139 | } | |
140 | ||
141 | function setValidRemoteVideoUrls (video: any) { | |
142 | if (Array.isArray(video.url) === false) return false | |
143 | ||
144 | video.url = video.url.filter(u => isRemoteVideoUrlValid(u)) | |
145 | ||
146 | return true | |
147 | } | |
148 | ||
149 | function setRemoteVideoTruncatedContent (video: any) { | |
150 | if (video.content) { | |
151 | video.content = peertubeTruncate(video.content, CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max) | |
152 | } | |
153 | ||
154 | return true | |
155 | } |