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

157 lines
4.4 KiB
TypeScript
Raw Normal View History

2017-09-07 08:27:35 -05:00
import 'express-validator'
2017-05-15 15:22:03 -05:00
import { has, values } from 'lodash'
import {
REQUEST_ENDPOINTS,
REQUEST_ENDPOINT_ACTIONS,
REQUEST_VIDEO_EVENT_TYPES
} from '../../../initializers'
import { isArray } from '../misc'
import {
isVideoAuthorValid,
isVideoThumbnailDataValid,
isVideoUUIDValid,
2017-05-15 15:22:03 -05:00
isVideoAbuseReasonValid,
isVideoAbuseReporterUsernameValid,
isVideoViewsValid,
isVideoLikesValid,
isVideoDislikesValid,
isVideoEventCountValid,
isVideoDateValid,
isVideoCategoryValid,
isVideoLicenceValid,
isVideoLanguageValid,
isVideoNSFWValid,
isVideoDescriptionValid,
isVideoDurationValid,
isVideoFileInfoHashValid,
2017-05-15 15:22:03 -05:00
isVideoNameValid,
isVideoTagsValid,
isVideoFileExtnameValid,
isVideoFileResolutionValid
2017-05-15 15:22:03 -05:00
} from '../videos'
const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS]
2017-06-10 15:15:25 -05:00
function isEachRemoteRequestVideosValid (requests: any[]) {
2017-05-15 15:22:03 -05:00
return isArray(requests) &&
2017-07-11 10:04:57 -05:00
requests.every(request => {
2017-05-15 15:22:03 -05:00
const video = request.data
if (!video) return false
return (
isRequestTypeAddValid(request.type) &&
isCommonVideoAttributesValid(video) &&
isVideoAuthorValid(video.author) &&
isVideoThumbnailDataValid(video.thumbnailData)
) ||
(
isRequestTypeUpdateValid(request.type) &&
isCommonVideoAttributesValid(video)
) ||
(
isRequestTypeRemoveValid(request.type) &&
isVideoUUIDValid(video.uuid)
2017-05-15 15:22:03 -05:00
) ||
(
isRequestTypeReportAbuseValid(request.type) &&
isVideoUUIDValid(request.data.videoUUID) &&
2017-05-15 15:22:03 -05:00
isVideoAbuseReasonValid(request.data.reportReason) &&
isVideoAbuseReporterUsernameValid(request.data.reporterUsername)
)
})
}
2017-06-10 15:15:25 -05:00
function isEachRemoteRequestVideosQaduValid (requests: any[]) {
2017-05-15 15:22:03 -05:00
return isArray(requests) &&
2017-07-11 10:04:57 -05:00
requests.every(request => {
2017-05-15 15:22:03 -05:00
const video = request.data
if (!video) return false
return (
isVideoUUIDValid(video.uuid) &&
2017-06-10 15:15:25 -05:00
(has(video, 'views') === false || isVideoViewsValid(video.views)) &&
(has(video, 'likes') === false || isVideoLikesValid(video.likes)) &&
(has(video, 'dislikes') === false || isVideoDislikesValid(video.dislikes))
2017-05-15 15:22:03 -05:00
)
})
}
2017-06-10 15:15:25 -05:00
function isEachRemoteRequestVideosEventsValid (requests: any[]) {
2017-05-15 15:22:03 -05:00
return isArray(requests) &&
2017-07-11 10:04:57 -05:00
requests.every(request => {
2017-05-15 15:22:03 -05:00
const eventData = request.data
if (!eventData) return false
return (
isVideoUUIDValid(eventData.uuid) &&
2017-05-15 15:22:03 -05:00
values(REQUEST_VIDEO_EVENT_TYPES).indexOf(eventData.eventType) !== -1 &&
isVideoEventCountValid(eventData.count)
)
})
}
// ---------------------------------------------------------------------------
export {
isEachRemoteRequestVideosValid,
isEachRemoteRequestVideosQaduValid,
isEachRemoteRequestVideosEventsValid
}
2017-08-25 11:36:49 -05:00
declare module 'express-validator' {
export interface Validator {
isEachRemoteRequestVideosValid,
isEachRemoteRequestVideosQaduValid,
isEachRemoteRequestVideosEventsValid
2017-06-10 15:15:25 -05:00
}
}
2017-05-15 15:22:03 -05:00
// ---------------------------------------------------------------------------
2017-06-10 15:15:25 -05:00
function isCommonVideoAttributesValid (video: any) {
2017-05-15 15:22:03 -05:00
return isVideoDateValid(video.createdAt) &&
isVideoDateValid(video.updatedAt) &&
isVideoCategoryValid(video.category) &&
isVideoLicenceValid(video.licence) &&
isVideoLanguageValid(video.language) &&
isVideoNSFWValid(video.nsfw) &&
isVideoDescriptionValid(video.description) &&
isVideoDurationValid(video.duration) &&
isVideoNameValid(video.name) &&
isVideoTagsValid(video.tags) &&
isVideoUUIDValid(video.uuid) &&
2017-05-15 15:22:03 -05: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 15:22:03 -05:00
}
2017-06-10 15:15:25 -05:00
function isRequestTypeAddValid (value: string) {
2017-05-15 15:22:03 -05:00
return value === ENDPOINT_ACTIONS.ADD
}
2017-06-10 15:15:25 -05:00
function isRequestTypeUpdateValid (value: string) {
2017-05-15 15:22:03 -05:00
return value === ENDPOINT_ACTIONS.UPDATE
}
2017-06-10 15:15:25 -05:00
function isRequestTypeRemoveValid (value: string) {
2017-05-15 15:22:03 -05:00
return value === ENDPOINT_ACTIONS.REMOVE
}
2017-06-10 15:15:25 -05:00
function isRequestTypeReportAbuseValid (value: string) {
2017-05-15 15:22:03 -05:00
return value === ENDPOINT_ACTIONS.REPORT_ABUSE
}