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