PeerTube/server/helpers/custom-validators/activitypub/videos.ts

155 lines
4.7 KiB
TypeScript
Raw Normal View History

2017-11-10 07:34:45 -06:00
import * as validator from 'validator'
2017-11-15 09:28:35 -06:00
import { ACTIVITY_PUB } from '../../../initializers'
import { exists, isDateValid, isUUIDValid } from '../misc'
import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../video-channels'
2017-05-15 15:22:03 -05:00
import {
2017-11-15 10:56:21 -06:00
isVideoAbuseReasonValid,
2017-05-15 15:22:03 -05:00
isVideoDurationValid,
isVideoNameValid,
2017-11-15 09:28:35 -06:00
isVideoNSFWValid,
2017-11-14 03:57:56 -06:00
isVideoTagValid,
2017-11-15 09:28:35 -06:00
isVideoTruncatedDescriptionValid,
isVideoUrlValid,
isVideoViewsValid
2017-05-15 15:22:03 -05:00
} from '../videos'
2017-11-15 10:56:21 -06:00
import { isActivityPubUrlValid, isBaseActivityValid } from './misc'
2017-11-10 07:34:45 -06:00
function isVideoTorrentAddActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Add') &&
isVideoTorrentObjectValid(activity.object)
}
function isVideoTorrentUpdateActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Update') &&
isVideoTorrentObjectValid(activity.object)
2017-05-15 15:22:03 -05:00
}
2017-11-14 10:31:26 -06:00
function isVideoTorrentDeleteActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Delete')
}
2017-11-15 09:28:35 -06: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]+/, ''))
}
2017-11-10 07:34:45 -06:00
function isVideoTorrentObjectValid (video: any) {
return video.type === 'Video' &&
isVideoNameValid(video.name) &&
2017-11-15 09:28:35 -06:00
isActivityPubVideoDurationValid(video.duration) &&
2017-11-10 07:34:45 -06:00
isUUIDValid(video.uuid) &&
setValidRemoteTags(video) &&
isRemoteIdentifierValid(video.category) &&
isRemoteIdentifierValid(video.licence) &&
isRemoteIdentifierValid(video.language) &&
isVideoViewsValid(video.video) &&
isVideoNSFWValid(video.nsfw) &&
isDateValid(video.published) &&
isDateValid(video.updated) &&
isRemoteVideoContentValid(video.mediaType, video.content) &&
isRemoteVideoIconValid(video.icon) &&
setValidRemoteVideoUrls(video.url)
2017-05-15 15:22:03 -05:00
}
2017-11-15 10:56:21 -06:00
function isVideoFlagValid (activity: any) {
return isBaseActivityValid(activity, 'Flag') &&
isVideoAbuseReasonValid(activity.content) &&
isActivityPubUrlValid(activity.object)
}
function isVideoAnnounceValid (activity: any) {
return isBaseActivityValid(activity, 'Announce') &&
isVideoTorrentObjectValid(activity.object)
}
function isVideoChannelAnnounceValid (activity: any) {
return isBaseActivityValid(activity, 'Announce') &&
isVideoChannelObjectValid(activity.object)
}
2017-11-10 07:34:45 -06:00
function isVideoChannelCreateActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Create') &&
isVideoChannelObjectValid(activity.object)
}
function isVideoChannelUpdateActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Update') &&
isVideoChannelObjectValid(activity.object)
}
2017-11-14 10:31:26 -06:00
function isVideoChannelDeleteActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Delete')
}
2017-11-10 07:34:45 -06:00
function isVideoChannelObjectValid (videoChannel: any) {
return videoChannel.type === 'VideoChannel' &&
isVideoChannelNameValid(videoChannel.name) &&
isVideoChannelDescriptionValid(videoChannel.description) &&
isUUIDValid(videoChannel.uuid)
2017-05-15 15:22:03 -05:00
}
// ---------------------------------------------------------------------------
export {
2017-11-10 07:34:45 -06:00
isVideoTorrentAddActivityValid,
isVideoChannelCreateActivityValid,
isVideoTorrentUpdateActivityValid,
2017-11-14 10:31:26 -06:00
isVideoChannelUpdateActivityValid,
isVideoChannelDeleteActivityValid,
2017-11-15 10:56:21 -06:00
isVideoTorrentDeleteActivityValid,
isVideoFlagValid,
isVideoAnnounceValid,
isVideoChannelAnnounceValid
2017-05-15 15:22:03 -05:00
}
// ---------------------------------------------------------------------------
2017-11-10 07:34:45 -06:00
function setValidRemoteTags (video: any) {
if (Array.isArray(video.tag) === false) return false
2017-05-15 15:22:03 -05:00
2017-11-10 07:34:45 -06:00
const newTag = video.tag.filter(t => {
return t.type === 'Hashtag' &&
isVideoTagValid(t.name)
})
2017-10-24 12:41:09 -05:00
2017-11-10 07:34:45 -06:00
video.tag = newTag
return true
2017-10-24 12:41:09 -05:00
}
2017-11-10 07:34:45 -06:00
function isRemoteIdentifierValid (data: any) {
return validator.isInt(data.identifier, { min: 0 })
2017-10-24 12:41:09 -05:00
}
2017-11-10 07:34:45 -06:00
function isRemoteVideoContentValid (mediaType: string, content: string) {
return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content)
2017-10-24 12:41:09 -05:00
}
2017-11-10 07:34:45 -06:00
function isRemoteVideoIconValid (icon: any) {
return icon.type === 'Image' &&
2017-11-14 03:57:56 -06:00
isVideoUrlValid(icon.url) &&
2017-11-10 07:34:45 -06:00
icon.mediaType === 'image/jpeg' &&
validator.isInt(icon.width, { min: 0 }) &&
validator.isInt(icon.height, { min: 0 })
2017-10-24 12:41:09 -05:00
}
2017-11-10 07:34:45 -06:00
function setValidRemoteVideoUrls (video: any) {
if (Array.isArray(video.url) === false) return false
2017-05-15 15:22:03 -05:00
2017-11-10 07:34:45 -06:00
const newUrl = video.url.filter(u => isRemoteVideoUrlValid(u))
video.url = newUrl
2017-05-15 15:22:03 -05:00
2017-11-10 07:34:45 -06:00
return true
2017-05-15 15:22:03 -05:00
}
2017-11-10 07:34:45 -06:00
function isRemoteVideoUrlValid (url: any) {
return url.type === 'Link' &&
ACTIVITY_PUB.VIDEO_URL_MIME_TYPES.indexOf(url.mimeType) !== -1 &&
2017-11-14 03:57:56 -06:00
isVideoUrlValid(url.url) &&
2017-11-10 07:34:45 -06:00
validator.isInt(url.width, { min: 0 }) &&
validator.isInt(url.size, { min: 0 })
2017-05-15 15:22:03 -05:00
}