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