From 72c7248b6fdcdb2175e726ff51b42e7555f2bd84 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 24 Oct 2017 19:41:09 +0200 Subject: Add video channels --- server/helpers/custom-validators/index.ts | 2 + server/helpers/custom-validators/misc.ts | 24 ++++- server/helpers/custom-validators/remote/videos.ts | 104 +++++++++++++-------- server/helpers/custom-validators/video-authors.ts | 45 +++++++++ server/helpers/custom-validators/video-channels.ts | 57 +++++++++++ server/helpers/custom-validators/videos.ts | 20 ---- 6 files changed, 193 insertions(+), 59 deletions(-) create mode 100644 server/helpers/custom-validators/video-authors.ts create mode 100644 server/helpers/custom-validators/video-channels.ts (limited to 'server/helpers') diff --git a/server/helpers/custom-validators/index.ts b/server/helpers/custom-validators/index.ts index 1dcab624a..c79982660 100644 --- a/server/helpers/custom-validators/index.ts +++ b/server/helpers/custom-validators/index.ts @@ -3,4 +3,6 @@ export * from './misc' export * from './pods' export * from './pods' export * from './users' +export * from './video-authors' +export * from './video-channels' export * from './videos' diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts index 60fcdd5bb..160ec91f3 100644 --- a/server/helpers/custom-validators/misc.ts +++ b/server/helpers/custom-validators/misc.ts @@ -1,4 +1,4 @@ -import 'express-validator' +import * as validator from 'validator' function exists (value: any) { return value !== undefined && value !== null @@ -8,9 +8,29 @@ function isArray (value: any) { return Array.isArray(value) } +function isDateValid (value: string) { + return exists(value) && validator.isISO8601(value) +} + +function isIdValid (value: string) { + return exists(value) && validator.isInt('' + value) +} + +function isUUIDValid (value: string) { + return exists(value) && validator.isUUID('' + value, 4) +} + +function isIdOrUUIDValid (value: string) { + return isIdValid(value) || isUUIDValid(value) +} + // --------------------------------------------------------------------------- export { exists, - isArray + isArray, + isIdValid, + isUUIDValid, + isIdOrUUIDValid, + isDateValid } diff --git a/server/helpers/custom-validators/remote/videos.ts b/server/helpers/custom-validators/remote/videos.ts index e261e05a8..057996f1c 100644 --- a/server/helpers/custom-validators/remote/videos.ts +++ b/server/helpers/custom-validators/remote/videos.ts @@ -6,18 +6,15 @@ import { REQUEST_ENDPOINT_ACTIONS, REQUEST_VIDEO_EVENT_TYPES } from '../../../initializers' -import { isArray } from '../misc' +import { isArray, isDateValid, isUUIDValid } from '../misc' import { - isVideoAuthorValid, isVideoThumbnailDataValid, - isVideoUUIDValid, isVideoAbuseReasonValid, isVideoAbuseReporterUsernameValid, isVideoViewsValid, isVideoLikesValid, isVideoDislikesValid, isVideoEventCountValid, - isVideoDateValid, isVideoCategoryValid, isVideoLicenceValid, isVideoLanguageValid, @@ -30,9 +27,22 @@ import { isVideoFileExtnameValid, isVideoFileResolutionValid } from '../videos' +import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../video-channels' +import { isVideoAuthorNameValid } from '../video-authors' const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] +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 isEachRemoteRequestVideosValid (requests: any[]) { return isArray(requests) && requests.every(request => { @@ -40,26 +50,11 @@ function isEachRemoteRequestVideosValid (requests: any[]) { 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) - ) || - ( - isRequestTypeReportAbuseValid(request.type) && - isVideoUUIDValid(request.data.videoUUID) && - isVideoAbuseReasonValid(request.data.reportReason) && - isVideoAbuseReporterUsernameValid(request.data.reporterUsername) - ) + const checker = checkers[request.type] + // We don't know the request type + if (checker === undefined) return false + + return checker(video) }) } @@ -71,7 +66,7 @@ function isEachRemoteRequestVideosQaduValid (requests: any[]) { if (!video) return false return ( - isVideoUUIDValid(video.uuid) && + isUUIDValid(video.uuid) && (has(video, 'views') === false || isVideoViewsValid(video.views)) && (has(video, 'likes') === false || isVideoLikesValid(video.likes)) && (has(video, 'dislikes') === false || isVideoDislikesValid(video.dislikes)) @@ -87,7 +82,7 @@ function isEachRemoteRequestVideosEventsValid (requests: any[]) { if (!eventData) return false return ( - isVideoUUIDValid(eventData.uuid) && + isUUIDValid(eventData.uuid) && values(REQUEST_VIDEO_EVENT_TYPES).indexOf(eventData.eventType) !== -1 && isVideoEventCountValid(eventData.count) ) @@ -105,8 +100,8 @@ export { // --------------------------------------------------------------------------- function isCommonVideoAttributesValid (video: any) { - return isVideoDateValid(video.createdAt) && - isVideoDateValid(video.updatedAt) && + return isDateValid(video.createdAt) && + isDateValid(video.updatedAt) && isVideoCategoryValid(video.category) && isVideoLicenceValid(video.licence) && isVideoLanguageValid(video.language) && @@ -115,7 +110,7 @@ function isCommonVideoAttributesValid (video: any) { isVideoDurationValid(video.duration) && isVideoNameValid(video.name) && isVideoTagsValid(video.tags) && - isVideoUUIDValid(video.uuid) && + isUUIDValid(video.uuid) && isVideoViewsValid(video.views) && isVideoLikesValid(video.likes) && isVideoDislikesValid(video.dislikes) && @@ -131,18 +126,53 @@ function isCommonVideoAttributesValid (video: any) { }) } -function isRequestTypeAddValid (value: string) { - return value === ENDPOINT_ACTIONS.ADD +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) } -function isRequestTypeUpdateValid (value: string) { - return value === ENDPOINT_ACTIONS.UPDATE +function checkRemoveVideoChannel (videoChannel: any) { + return isUUIDValid(videoChannel.uuid) } -function isRequestTypeRemoveValid (value: string) { - return value === ENDPOINT_ACTIONS.REMOVE +function checkAddAuthor (author: any) { + return isUUIDValid(author.uuid) && + isVideoAuthorNameValid(author.name) } -function isRequestTypeReportAbuseValid (value: string) { - return value === ENDPOINT_ACTIONS.REPORT_ABUSE +function checkRemoveAuthor (author: any) { + return isUUIDValid(author.uuid) } diff --git a/server/helpers/custom-validators/video-authors.ts b/server/helpers/custom-validators/video-authors.ts new file mode 100644 index 000000000..48ca9b200 --- /dev/null +++ b/server/helpers/custom-validators/video-authors.ts @@ -0,0 +1,45 @@ +import * as Promise from 'bluebird' +import * as validator from 'validator' +import * as express from 'express' +import 'express-validator' + +import { database as db } from '../../initializers' +import { AuthorInstance } from '../../models' +import { logger } from '../logger' + +import { isUserUsernameValid } from './users' + +function isVideoAuthorNameValid (value: string) { + return isUserUsernameValid(value) +} + +function checkVideoAuthorExists (id: string, res: express.Response, callback: () => void) { + let promise: Promise + if (validator.isInt(id)) { + promise = db.Author.load(+id) + } else { // UUID + promise = db.Author.loadByUUID(id) + } + + promise.then(author => { + if (!author) { + return res.status(404) + .json({ error: 'Video author not found' }) + .end() + } + + res.locals.author = author + callback() + }) + .catch(err => { + logger.error('Error in video author request validator.', err) + return res.sendStatus(500) + }) +} + +// --------------------------------------------------------------------------- + +export { + checkVideoAuthorExists, + isVideoAuthorNameValid +} diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts new file mode 100644 index 000000000..b6be557e6 --- /dev/null +++ b/server/helpers/custom-validators/video-channels.ts @@ -0,0 +1,57 @@ +import * as Promise from 'bluebird' +import * as validator from 'validator' +import * as express from 'express' +import 'express-validator' +import 'multer' + +import { database as db, CONSTRAINTS_FIELDS } from '../../initializers' +import { VideoChannelInstance } from '../../models' +import { logger } from '../logger' +import { exists } from './misc' + +const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS + +function isVideoChannelDescriptionValid (value: string) { + return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION) +} + +function isVideoChannelNameValid (value: string) { + return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME) +} + +function isVideoChannelUUIDValid (value: string) { + return exists(value) && validator.isUUID('' + value, 4) +} + +function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) { + let promise: Promise + if (validator.isInt(id)) { + promise = db.VideoChannel.loadAndPopulateAuthor(+id) + } else { // UUID + promise = db.VideoChannel.loadByUUIDAndPopulateAuthor(id) + } + + promise.then(videoChannel => { + if (!videoChannel) { + return res.status(404) + .json({ error: 'Video channel not found' }) + .end() + } + + res.locals.videoChannel = videoChannel + callback() + }) + .catch(err => { + logger.error('Error in video channel request validator.', err) + return res.sendStatus(500) + }) +} + +// --------------------------------------------------------------------------- + +export { + isVideoChannelDescriptionValid, + isVideoChannelNameValid, + isVideoChannelUUIDValid, + checkVideoChannelExists +} diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index 05d1dc607..4e441fe5f 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts @@ -23,18 +23,6 @@ const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS -function isVideoIdOrUUIDValid (value: string) { - return validator.isInt(value) || isVideoUUIDValid(value) -} - -function isVideoAuthorValid (value: string) { - return isUserUsernameValid(value) -} - -function isVideoDateValid (value: string) { - return exists(value) && validator.isISO8601(value) -} - function isVideoCategoryValid (value: number) { return VIDEO_CATEGORIES[value] !== undefined } @@ -79,10 +67,6 @@ function isVideoThumbnailDataValid (value: string) { return exists(value) && validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA) } -function isVideoUUIDValid (value: string) { - return exists(value) && validator.isUUID('' + value, 4) -} - function isVideoAbuseReasonValid (value: string) { return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON) } @@ -170,9 +154,6 @@ function checkVideoExists (id: string, res: express.Response, callback: () => vo // --------------------------------------------------------------------------- export { - isVideoIdOrUUIDValid, - isVideoAuthorValid, - isVideoDateValid, isVideoCategoryValid, isVideoLicenceValid, isVideoLanguageValid, @@ -185,7 +166,6 @@ export { isVideoThumbnailValid, isVideoThumbnailDataValid, isVideoFileExtnameValid, - isVideoUUIDValid, isVideoAbuseReasonValid, isVideoAbuseReporterUsernameValid, isVideoFile, -- cgit v1.2.3