]>
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 | isVideoAbuseReasonValid, | |
7 | isVideoDurationValid, | |
8 | isVideoNameValid, | |
9 | isVideoStateValid, | |
10 | isVideoTagValid, | |
11 | isVideoTruncatedDescriptionValid, | |
12 | isVideoViewsValid | |
13 | } from '../videos' | |
14 | import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' | |
15 | import { VideoState } from '../../../../shared/models/videos' | |
16 | ||
17 | function sanitizeAndCheckVideoTorrentCreateActivity (activity: any) { | |
18 | return isBaseActivityValid(activity, 'Create') && | |
19 | sanitizeAndCheckVideoTorrentObject(activity.object) | |
20 | } | |
21 | ||
22 | function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) { | |
23 | return isBaseActivityValid(activity, 'Update') && | |
24 | sanitizeAndCheckVideoTorrentObject(activity.object) | |
25 | } | |
26 | ||
27 | function isVideoTorrentDeleteActivityValid (activity: any) { | |
28 | return isBaseActivityValid(activity, 'Delete') | |
29 | } | |
30 | ||
31 | function 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 | ||
38 | function 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') && | |
44 | isVideoDurationValid(value.replace(/[^0-9]+/g, '')) | |
45 | } | |
46 | ||
47 | function sanitizeAndCheckVideoTorrentObject (video: any) { | |
48 | if (video.type !== 'Video') return false | |
49 | ||
50 | if (!setValidRemoteTags(video)) return false | |
51 | if (!setValidRemoteVideoUrls(video)) return false | |
52 | if (!setRemoteVideoTruncatedContent(video)) return false | |
53 | if (!setValidAttributedTo(video)) return false | |
54 | ||
55 | // Default attributes | |
56 | if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED | |
57 | if (!isBooleanValid(video.waitTranscoding)) video.waitTranscoding = false | |
58 | ||
59 | return isActivityPubUrlValid(video.id) && | |
60 | isVideoNameValid(video.name) && | |
61 | isActivityPubVideoDurationValid(video.duration) && | |
62 | isUUIDValid(video.uuid) && | |
63 | (!video.category || isRemoteNumberIdentifierValid(video.category)) && | |
64 | (!video.licence || isRemoteNumberIdentifierValid(video.licence)) && | |
65 | (!video.language || isRemoteStringIdentifierValid(video.language)) && | |
66 | isVideoViewsValid(video.views) && | |
67 | isBooleanValid(video.sensitive) && | |
68 | isBooleanValid(video.commentsEnabled) && | |
69 | isDateValid(video.published) && | |
70 | isDateValid(video.updated) && | |
71 | (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) && | |
72 | isRemoteVideoIconValid(video.icon) && | |
73 | video.url.length !== 0 && | |
74 | video.attributedTo.length !== 0 | |
75 | } | |
76 | ||
77 | // --------------------------------------------------------------------------- | |
78 | ||
79 | export { | |
80 | sanitizeAndCheckVideoTorrentCreateActivity, | |
81 | sanitizeAndCheckVideoTorrentUpdateActivity, | |
82 | isVideoTorrentDeleteActivityValid, | |
83 | isRemoteStringIdentifierValid, | |
84 | isVideoFlagValid, | |
85 | sanitizeAndCheckVideoTorrentObject | |
86 | } | |
87 | ||
88 | // --------------------------------------------------------------------------- | |
89 | ||
90 | function setValidRemoteTags (video: any) { | |
91 | if (Array.isArray(video.tag) === false) return false | |
92 | ||
93 | video.tag = video.tag.filter(t => { | |
94 | return t.type === 'Hashtag' && | |
95 | isVideoTagValid(t.name) | |
96 | }) | |
97 | ||
98 | return true | |
99 | } | |
100 | ||
101 | function isRemoteNumberIdentifierValid (data: any) { | |
102 | return validator.isInt(data.identifier, { min: 0 }) | |
103 | } | |
104 | ||
105 | function isRemoteStringIdentifierValid (data: any) { | |
106 | return typeof data.identifier === 'string' | |
107 | } | |
108 | ||
109 | function isRemoteVideoContentValid (mediaType: string, content: string) { | |
110 | return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content) | |
111 | } | |
112 | ||
113 | function isRemoteVideoIconValid (icon: any) { | |
114 | return icon.type === 'Image' && | |
115 | isActivityPubUrlValid(icon.url) && | |
116 | icon.mediaType === 'image/jpeg' && | |
117 | validator.isInt(icon.width + '', { min: 0 }) && | |
118 | validator.isInt(icon.height + '', { min: 0 }) | |
119 | } | |
120 | ||
121 | function setValidRemoteVideoUrls (video: any) { | |
122 | if (Array.isArray(video.url) === false) return false | |
123 | ||
124 | video.url = video.url.filter(u => isRemoteVideoUrlValid(u)) | |
125 | ||
126 | return true | |
127 | } | |
128 | ||
129 | function setRemoteVideoTruncatedContent (video: any) { | |
130 | if (video.content) { | |
131 | video.content = peertubeTruncate(video.content, CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max) | |
132 | } | |
133 | ||
134 | return true | |
135 | } | |
136 | ||
137 | function isRemoteVideoUrlValid (url: any) { | |
138 | return url.type === 'Link' && | |
139 | ( | |
140 | ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mimeType) !== -1 && | |
141 | isActivityPubUrlValid(url.href) && | |
142 | validator.isInt(url.width + '', { min: 0 }) && | |
143 | validator.isInt(url.size + '', { min: 0 }) | |
144 | ) || | |
145 | ( | |
146 | ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mimeType) !== -1 && | |
147 | isActivityPubUrlValid(url.href) && | |
148 | validator.isInt(url.width + '', { min: 0 }) | |
149 | ) || | |
150 | ( | |
151 | ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mimeType) !== -1 && | |
152 | validator.isLength(url.href, { min: 5 }) && | |
153 | validator.isInt(url.width + '', { min: 0 }) | |
154 | ) | |
155 | } |