1
0
Fork 0
peertube/server/helpers/custom-validators/activitypub/videos.ts

133 lines
4.2 KiB
TypeScript
Raw Normal View History

2017-11-10 08:34:45 -05:00
import * as validator from 'validator'
2017-11-15 10:28:35 -05:00
import { ACTIVITY_PUB } from '../../../initializers'
2018-01-03 04:12:36 -05:00
import { exists, isBooleanValid, isDateValid, isUUIDValid } from '../misc'
2017-05-15 16:22:03 -04:00
import {
2017-11-15 11:56:21 -05:00
isVideoAbuseReasonValid,
2017-05-15 16:22:03 -04:00
isVideoDurationValid,
isVideoNameValid,
2017-11-14 04:57:56 -05:00
isVideoTagValid,
2017-11-15 10:28:35 -05:00
isVideoTruncatedDescriptionValid,
isVideoViewsValid
2017-05-15 16:22:03 -04:00
} from '../videos'
2017-12-14 11:38:41 -05:00
import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
2017-11-10 08:34:45 -05:00
2017-12-14 11:38:41 -05:00
function isVideoTorrentCreateActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Create') &&
2017-11-10 08:34:45 -05:00
isVideoTorrentObjectValid(activity.object)
}
function isVideoTorrentUpdateActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Update') &&
isVideoTorrentObjectValid(activity.object)
2017-05-15 16:22:03 -04:00
}
2017-11-14 11:31:26 -05:00
function isVideoTorrentDeleteActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Delete')
}
2017-11-20 03:43:39 -05:00
function isVideoFlagValid (activity: any) {
return isBaseActivityValid(activity, 'Create') &&
activity.object.type === 'Flag' &&
isVideoAbuseReasonValid(activity.object.content) &&
isActivityPubUrlValid(activity.object.object)
}
2017-11-15 10:28:35 -05:00
function isActivityPubVideoDurationValid (value: string) {
// https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
return exists(value) &&
typeof value === 'string' &&
value.startsWith('PT') &&
value.endsWith('S') &&
isVideoDurationValid(value.replace(/[^0-9]+/g, ''))
2017-11-15 10:28:35 -05:00
}
2017-11-10 08:34:45 -05:00
function isVideoTorrentObjectValid (video: any) {
return video.type === 'Video' &&
2017-11-16 09:22:39 -05:00
isActivityPubUrlValid(video.id) &&
2017-11-10 08:34:45 -05:00
isVideoNameValid(video.name) &&
2017-11-15 10:28:35 -05:00
isActivityPubVideoDurationValid(video.duration) &&
2017-11-10 08:34:45 -05:00
isUUIDValid(video.uuid) &&
setValidRemoteTags(video) &&
2017-12-08 11:31:21 -05:00
(!video.category || isRemoteIdentifierValid(video.category)) &&
(!video.licence || isRemoteIdentifierValid(video.licence)) &&
2017-11-22 10:25:03 -05:00
(!video.language || isRemoteIdentifierValid(video.language)) &&
isVideoViewsValid(video.views) &&
isBooleanValid(video.sensitive) &&
2018-01-03 04:12:36 -05:00
isBooleanValid(video.commentsEnabled) &&
2017-11-10 08:34:45 -05:00
isDateValid(video.published) &&
isDateValid(video.updated) &&
2017-12-08 11:31:21 -05:00
(!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) &&
2017-11-10 08:34:45 -05:00
isRemoteVideoIconValid(video.icon) &&
setValidRemoteVideoUrls(video) &&
2017-12-14 11:38:41 -05:00
video.url.length !== 0 &&
setValidAttributedTo(video) &&
video.attributedTo.length !== 0
2017-05-15 16:22:03 -04:00
}
// ---------------------------------------------------------------------------
export {
2017-12-14 11:38:41 -05:00
isVideoTorrentCreateActivityValid,
2017-11-10 08:34:45 -05:00
isVideoTorrentUpdateActivityValid,
2017-11-15 11:56:21 -05:00
isVideoTorrentDeleteActivityValid,
2018-01-10 11:18:12 -05:00
isVideoFlagValid,
isVideoTorrentObjectValid
2017-05-15 16:22:03 -04:00
}
// ---------------------------------------------------------------------------
2017-11-10 08:34:45 -05:00
function setValidRemoteTags (video: any) {
if (Array.isArray(video.tag) === false) return false
2017-05-15 16:22:03 -04:00
2017-11-27 11:30:46 -05:00
video.tag = video.tag.filter(t => {
2017-11-10 08:34:45 -05:00
return t.type === 'Hashtag' &&
isVideoTagValid(t.name)
})
2017-10-24 13:41:09 -04:00
2017-11-10 08:34:45 -05:00
return true
2017-10-24 13:41:09 -04:00
}
2017-11-10 08:34:45 -05:00
function isRemoteIdentifierValid (data: any) {
return validator.isInt(data.identifier, { min: 0 })
2017-10-24 13:41:09 -04:00
}
2017-11-10 08:34:45 -05:00
function isRemoteVideoContentValid (mediaType: string, content: string) {
return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content)
2017-10-24 13:41:09 -04:00
}
2017-11-10 08:34:45 -05:00
function isRemoteVideoIconValid (icon: any) {
return icon.type === 'Image' &&
2017-11-27 11:30:46 -05:00
isActivityPubUrlValid(icon.url) &&
2017-11-10 08:34:45 -05:00
icon.mediaType === 'image/jpeg' &&
validator.isInt(icon.width + '', { min: 0 }) &&
validator.isInt(icon.height + '', { min: 0 })
2017-10-24 13:41:09 -04:00
}
2017-11-10 08:34:45 -05:00
function setValidRemoteVideoUrls (video: any) {
if (Array.isArray(video.url) === false) return false
2017-05-15 16:22:03 -04:00
2017-11-27 11:30:46 -05:00
video.url = video.url.filter(u => isRemoteVideoUrlValid(u))
2017-05-15 16:22:03 -04:00
2017-11-10 08:34:45 -05:00
return true
2017-05-15 16:22:03 -04:00
}
2017-11-10 08:34:45 -05:00
function isRemoteVideoUrlValid (url: any) {
return url.type === 'Link' &&
2017-11-16 09:22:39 -05:00
(
ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mimeType) !== -1 &&
2018-01-12 09:35:30 -05:00
isActivityPubUrlValid(url.href) &&
2017-11-16 09:22:39 -05:00
validator.isInt(url.width + '', { min: 0 }) &&
validator.isInt(url.size + '', { min: 0 })
) ||
(
ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mimeType) !== -1 &&
2018-01-12 09:35:30 -05:00
isActivityPubUrlValid(url.href) &&
2017-11-16 09:22:39 -05:00
validator.isInt(url.width + '', { min: 0 })
) ||
(
ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mimeType) !== -1 &&
2018-01-12 09:35:30 -05:00
validator.isLength(url.href, { min: 5 }) &&
2017-11-16 09:22:39 -05:00
validator.isInt(url.width + '', { min: 0 })
)
2017-05-15 16:22:03 -04:00
}