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