2ed2988f55e04fd393a0b123f4a34ed2e604c222
[oweals/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   isVideoViewsValid
12 } from '../videos'
13 import { isActivityPubUrlValid, isBaseActivityValid } from './misc'
14
15 function isVideoTorrentAddActivityValid (activity: any) {
16   return isBaseActivityValid(activity, 'Add') &&
17     isVideoTorrentObjectValid(activity.object)
18 }
19
20 function isVideoTorrentUpdateActivityValid (activity: any) {
21   return isBaseActivityValid(activity, 'Update') &&
22     isVideoTorrentObjectValid(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 isVideoTorrentObjectValid (video: any) {
46   return video.type === 'Video' &&
47     isActivityPubUrlValid(video.id) &&
48     isVideoNameValid(video.name) &&
49     isActivityPubVideoDurationValid(video.duration) &&
50     isUUIDValid(video.uuid) &&
51     setValidRemoteTags(video) &&
52     (!video.category || isRemoteIdentifierValid(video.category)) &&
53     (!video.licence || isRemoteIdentifierValid(video.licence)) &&
54     (!video.language || isRemoteIdentifierValid(video.language)) &&
55     isVideoViewsValid(video.views) &&
56     isVideoNSFWValid(video.nsfw) &&
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 }
64
65 // ---------------------------------------------------------------------------
66
67 export {
68   isVideoTorrentAddActivityValid,
69   isVideoTorrentUpdateActivityValid,
70   isVideoTorrentDeleteActivityValid,
71   isVideoFlagValid
72 }
73
74 // ---------------------------------------------------------------------------
75
76 function setValidRemoteTags (video: any) {
77   if (Array.isArray(video.tag) === false) return false
78
79   video.tag = video.tag.filter(t => {
80     return t.type === 'Hashtag' &&
81       isVideoTagValid(t.name)
82   })
83
84   return true
85 }
86
87 function isRemoteIdentifierValid (data: any) {
88   return validator.isInt(data.identifier, { min: 0 })
89 }
90
91 function isRemoteVideoContentValid (mediaType: string, content: string) {
92   return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content)
93 }
94
95 function isRemoteVideoIconValid (icon: any) {
96   return icon.type === 'Image' &&
97     isActivityPubUrlValid(icon.url) &&
98     icon.mediaType === 'image/jpeg' &&
99     validator.isInt(icon.width + '', { min: 0 }) &&
100     validator.isInt(icon.height + '', { min: 0 })
101 }
102
103 function setValidRemoteVideoUrls (video: any) {
104   if (Array.isArray(video.url) === false) return false
105
106   video.url = video.url.filter(u => isRemoteVideoUrlValid(u))
107
108   return true
109 }
110
111 function isRemoteVideoUrlValid (url: any) {
112   return url.type === 'Link' &&
113     (
114       ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mimeType) !== -1 &&
115       isActivityPubUrlValid(url.url) &&
116       validator.isInt(url.width + '', { min: 0 }) &&
117       validator.isInt(url.size + '', { min: 0 })
118     ) ||
119     (
120       ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mimeType) !== -1 &&
121       isActivityPubUrlValid(url.url) &&
122       validator.isInt(url.width + '', { min: 0 })
123     ) ||
124     (
125       ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mimeType) !== -1 &&
126       validator.isLength(url.url, { min: 5 }) &&
127       validator.isInt(url.width + '', { min: 0 })
128     )
129 }