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

185 lines
5.7 KiB
TypeScript
Raw Normal View History

2017-09-07 15:27:35 +02:00
import 'express-validator'
2017-05-15 22:22:03 +02:00
import { has, values } from 'lodash'
import {
REQUEST_ENDPOINTS,
REQUEST_ENDPOINT_ACTIONS,
REQUEST_VIDEO_EVENT_TYPES
} from '../../../initializers'
2017-10-24 19:41:09 +02:00
import { isArray, isDateValid, isUUIDValid } from '../misc'
2017-05-15 22:22:03 +02:00
import {
isVideoThumbnailDataValid,
isVideoAbuseReasonValid,
isVideoAbuseReporterUsernameValid,
isVideoViewsValid,
isVideoLikesValid,
isVideoDislikesValid,
isVideoEventCountValid,
isRemoteVideoCategoryValid,
isRemoteVideoLicenceValid,
isRemoteVideoLanguageValid,
2017-05-15 22:22:03 +02:00
isVideoNSFWValid,
2017-10-30 10:16:27 +01:00
isVideoTruncatedDescriptionValid,
2017-05-15 22:22:03 +02:00
isVideoDurationValid,
isVideoFileInfoHashValid,
2017-05-15 22:22:03 +02:00
isVideoNameValid,
isVideoTagsValid,
isVideoFileExtnameValid,
isVideoFileResolutionValid
2017-05-15 22:22:03 +02:00
} from '../videos'
2017-10-24 19:41:09 +02:00
import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../video-channels'
import { isVideoAuthorNameValid } from '../video-authors'
2017-05-15 22:22:03 +02:00
const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS]
2017-10-24 19:41:09 +02:00
const checkers: { [ id: string ]: (obj: any) => boolean } = {}
checkers[ENDPOINT_ACTIONS.ADD_VIDEO] = checkAddVideo
checkers[ENDPOINT_ACTIONS.UPDATE_VIDEO] = checkUpdateVideo
checkers[ENDPOINT_ACTIONS.REMOVE_VIDEO] = checkRemoveVideo
checkers[ENDPOINT_ACTIONS.REPORT_ABUSE] = checkReportVideo
checkers[ENDPOINT_ACTIONS.ADD_CHANNEL] = checkAddVideoChannel
checkers[ENDPOINT_ACTIONS.UPDATE_CHANNEL] = checkUpdateVideoChannel
checkers[ENDPOINT_ACTIONS.REMOVE_CHANNEL] = checkRemoveVideoChannel
checkers[ENDPOINT_ACTIONS.ADD_AUTHOR] = checkAddAuthor
checkers[ENDPOINT_ACTIONS.REMOVE_AUTHOR] = checkRemoveAuthor
function removeBadRequestVideos (requests: any[]) {
for (let i = requests.length - 1; i >= 0 ; i--) {
const request = requests[i]
const video = request.data
if (
!video ||
checkers[request.type] === undefined ||
checkers[request.type](video) === false
) {
requests.splice(i, 1)
}
}
2017-05-15 22:22:03 +02:00
}
function removeBadRequestVideosQadu (requests: any[]) {
for (let i = requests.length - 1; i >= 0 ; i--) {
const request = requests[i]
const video = request.data
2017-05-15 22:22:03 +02:00
if (
!video ||
(
2017-10-24 19:41:09 +02:00
isUUIDValid(video.uuid) &&
2017-06-10 22:15:25 +02:00
(has(video, 'views') === false || isVideoViewsValid(video.views)) &&
(has(video, 'likes') === false || isVideoLikesValid(video.likes)) &&
(has(video, 'dislikes') === false || isVideoDislikesValid(video.dislikes))
) === false
) {
requests.splice(i, 1)
}
}
2017-05-15 22:22:03 +02:00
}
function removeBadRequestVideosEvents (requests: any[]) {
for (let i = requests.length - 1; i >= 0 ; i--) {
const request = requests[i]
const eventData = request.data
2017-05-15 22:22:03 +02:00
if (
!eventData ||
(
2017-10-24 19:41:09 +02:00
isUUIDValid(eventData.uuid) &&
2017-05-15 22:22:03 +02:00
values(REQUEST_VIDEO_EVENT_TYPES).indexOf(eventData.eventType) !== -1 &&
isVideoEventCountValid(eventData.count)
) === false
) {
requests.splice(i, 1)
}
}
2017-05-15 22:22:03 +02:00
}
// ---------------------------------------------------------------------------
export {
removeBadRequestVideos,
removeBadRequestVideosQadu,
removeBadRequestVideosEvents
2017-05-15 22:22:03 +02:00
}
// ---------------------------------------------------------------------------
2017-06-10 22:15:25 +02:00
function isCommonVideoAttributesValid (video: any) {
2017-10-24 19:41:09 +02:00
return isDateValid(video.createdAt) &&
isDateValid(video.updatedAt) &&
isRemoteVideoCategoryValid(video.category) &&
isRemoteVideoLicenceValid(video.licence) &&
isRemoteVideoLanguageValid(video.language) &&
2017-05-15 22:22:03 +02:00
isVideoNSFWValid(video.nsfw) &&
2017-10-30 10:16:27 +01:00
isVideoTruncatedDescriptionValid(video.truncatedDescription) &&
2017-05-15 22:22:03 +02:00
isVideoDurationValid(video.duration) &&
isVideoNameValid(video.name) &&
isVideoTagsValid(video.tags) &&
2017-10-24 19:41:09 +02:00
isUUIDValid(video.uuid) &&
2017-05-15 22:22:03 +02:00
isVideoViewsValid(video.views) &&
isVideoLikesValid(video.likes) &&
isVideoDislikesValid(video.dislikes) &&
isArray(video.files) &&
video.files.every(videoFile => {
if (!videoFile) return false
return (
isVideoFileInfoHashValid(videoFile.infoHash) &&
isVideoFileExtnameValid(videoFile.extname) &&
isVideoFileResolutionValid(videoFile.resolution)
)
})
2017-05-15 22:22:03 +02:00
}
2017-10-24 19:41:09 +02:00
function checkAddVideo (video: any) {
return isCommonVideoAttributesValid(video) &&
isUUIDValid(video.channelUUID) &&
isVideoThumbnailDataValid(video.thumbnailData)
}
function checkUpdateVideo (video: any) {
return isCommonVideoAttributesValid(video)
}
function checkRemoveVideo (video: any) {
return isUUIDValid(video.uuid)
}
function checkReportVideo (abuse: any) {
return isUUIDValid(abuse.videoUUID) &&
isVideoAbuseReasonValid(abuse.reportReason) &&
isVideoAbuseReporterUsernameValid(abuse.reporterUsername)
}
function checkAddVideoChannel (videoChannel: any) {
return isUUIDValid(videoChannel.uuid) &&
isVideoChannelNameValid(videoChannel.name) &&
isVideoChannelDescriptionValid(videoChannel.description) &&
isDateValid(videoChannel.createdAt) &&
isDateValid(videoChannel.updatedAt) &&
isUUIDValid(videoChannel.ownerUUID)
}
function checkUpdateVideoChannel (videoChannel: any) {
return isUUIDValid(videoChannel.uuid) &&
isVideoChannelNameValid(videoChannel.name) &&
isVideoChannelDescriptionValid(videoChannel.description) &&
isDateValid(videoChannel.createdAt) &&
isDateValid(videoChannel.updatedAt) &&
isUUIDValid(videoChannel.ownerUUID)
2017-05-15 22:22:03 +02:00
}
2017-10-24 19:41:09 +02:00
function checkRemoveVideoChannel (videoChannel: any) {
return isUUIDValid(videoChannel.uuid)
2017-05-15 22:22:03 +02:00
}
2017-10-24 19:41:09 +02:00
function checkAddAuthor (author: any) {
return isUUIDValid(author.uuid) &&
isVideoAuthorNameValid(author.name)
2017-05-15 22:22:03 +02:00
}
2017-10-24 19:41:09 +02:00
function checkRemoveAuthor (author: any) {
return isUUIDValid(author.uuid)
2017-05-15 22:22:03 +02:00
}