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