]>
Commit | Line | Data |
---|---|---|
1 | import * as validator from 'validator' | |
2 | import { ACTIVITY_PUB } from '../../../initializers' | |
3 | import { exists, isBooleanValid, isDateValid, isUUIDValid } from '../misc' | |
4 | import { | |
5 | isVideoAbuseReasonValid, | |
6 | isVideoDurationValid, | |
7 | isVideoNameValid, | |
8 | isVideoTagValid, | |
9 | isVideoTruncatedDescriptionValid, | |
10 | isVideoViewsValid | |
11 | } from '../videos' | |
12 | import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' | |
13 | ||
14 | function isVideoTorrentCreateActivityValid (activity: any) { | |
15 | return isBaseActivityValid(activity, 'Create') && | |
16 | isVideoTorrentObjectValid(activity.object) | |
17 | } | |
18 | ||
19 | function isVideoTorrentUpdateActivityValid (activity: any) { | |
20 | return isBaseActivityValid(activity, 'Update') && | |
21 | isVideoTorrentObjectValid(activity.object) | |
22 | } | |
23 | ||
24 | function isVideoTorrentDeleteActivityValid (activity: any) { | |
25 | return isBaseActivityValid(activity, 'Delete') | |
26 | } | |
27 | ||
28 | function isVideoFlagValid (activity: any) { | |
29 | return isBaseActivityValid(activity, 'Create') && | |
30 | activity.object.type === 'Flag' && | |
31 | isVideoAbuseReasonValid(activity.object.content) && | |
32 | isActivityPubUrlValid(activity.object.object) | |
33 | } | |
34 | ||
35 | function isActivityPubVideoDurationValid (value: string) { | |
36 | // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration | |
37 | return exists(value) && | |
38 | typeof value === 'string' && | |
39 | value.startsWith('PT') && | |
40 | value.endsWith('S') && | |
41 | isVideoDurationValid(value.replace(/[^0-9]+/g, '')) | |
42 | } | |
43 | ||
44 | function isVideoTorrentObjectValid (video: any) { | |
45 | return video.type === 'Video' && | |
46 | isActivityPubUrlValid(video.id) && | |
47 | isVideoNameValid(video.name) && | |
48 | isActivityPubVideoDurationValid(video.duration) && | |
49 | isUUIDValid(video.uuid) && | |
50 | setValidRemoteTags(video) && | |
51 | (!video.category || isRemoteIdentifierValid(video.category)) && | |
52 | (!video.licence || isRemoteIdentifierValid(video.licence)) && | |
53 | (!video.language || isRemoteIdentifierValid(video.language)) && | |
54 | isVideoViewsValid(video.views) && | |
55 | isBooleanValid(video.sensitive) && | |
56 | isBooleanValid(video.commentsEnabled) && | |
57 | isDateValid(video.published) && | |
58 | isDateValid(video.updated) && | |
59 | (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) && | |
60 | isRemoteVideoIconValid(video.icon) && | |
61 | setValidRemoteVideoUrls(video) && | |
62 | video.url.length !== 0 && | |
63 | setValidAttributedTo(video) && | |
64 | video.attributedTo.length !== 0 | |
65 | } | |
66 | ||
67 | // --------------------------------------------------------------------------- | |
68 | ||
69 | export { | |
70 | isVideoTorrentCreateActivityValid, | |
71 | isVideoTorrentUpdateActivityValid, | |
72 | isVideoTorrentDeleteActivityValid, | |
73 | isVideoFlagValid, | |
74 | isVideoTorrentObjectValid | |
75 | } | |
76 | ||
77 | // --------------------------------------------------------------------------- | |
78 | ||
79 | function setValidRemoteTags (video: any) { | |
80 | if (Array.isArray(video.tag) === false) return false | |
81 | ||
82 | video.tag = video.tag.filter(t => { | |
83 | return t.type === 'Hashtag' && | |
84 | isVideoTagValid(t.name) | |
85 | }) | |
86 | ||
87 | return true | |
88 | } | |
89 | ||
90 | function isRemoteIdentifierValid (data: any) { | |
91 | return validator.isInt(data.identifier, { min: 0 }) | |
92 | } | |
93 | ||
94 | function isRemoteVideoContentValid (mediaType: string, content: string) { | |
95 | return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content) | |
96 | } | |
97 | ||
98 | function isRemoteVideoIconValid (icon: any) { | |
99 | return icon.type === 'Image' && | |
100 | isActivityPubUrlValid(icon.url) && | |
101 | icon.mediaType === 'image/jpeg' && | |
102 | validator.isInt(icon.width + '', { min: 0 }) && | |
103 | validator.isInt(icon.height + '', { min: 0 }) | |
104 | } | |
105 | ||
106 | function setValidRemoteVideoUrls (video: any) { | |
107 | if (Array.isArray(video.url) === false) return false | |
108 | ||
109 | video.url = video.url.filter(u => isRemoteVideoUrlValid(u)) | |
110 | ||
111 | return true | |
112 | } | |
113 | ||
114 | function isRemoteVideoUrlValid (url: any) { | |
115 | return url.type === 'Link' && | |
116 | ( | |
117 | ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mimeType) !== -1 && | |
118 | isActivityPubUrlValid(url.href) && | |
119 | validator.isInt(url.width + '', { min: 0 }) && | |
120 | validator.isInt(url.size + '', { min: 0 }) | |
121 | ) || | |
122 | ( | |
123 | ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mimeType) !== -1 && | |
124 | isActivityPubUrlValid(url.href) && | |
125 | validator.isInt(url.width + '', { min: 0 }) | |
126 | ) || | |
127 | ( | |
128 | ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mimeType) !== -1 && | |
129 | validator.isLength(url.href, { min: 5 }) && | |
130 | validator.isInt(url.width + '', { min: 0 }) | |
131 | ) | |
132 | } |