isVideoLikesValid,
isVideoDislikesValid,
isVideoEventCountValid,
- isVideoCategoryValid,
- isVideoLicenceValid,
- isVideoLanguageValid,
+ isRemoteVideoCategoryValid,
+ isRemoteVideoLicenceValid,
+ isRemoteVideoLanguageValid,
isVideoNSFWValid,
isVideoDescriptionValid,
isVideoDurationValid,
checkers[ENDPOINT_ACTIONS.ADD_AUTHOR] = checkAddAuthor
checkers[ENDPOINT_ACTIONS.REMOVE_AUTHOR] = checkRemoveAuthor
-function isEachRemoteRequestVideosValid (requests: any[]) {
- return isArray(requests) &&
- requests.every(request => {
- const video = request.data
-
- if (!video) return false
-
- const checker = checkers[request.type]
- // We don't know the request type
- if (checker === undefined) return false
-
- return checker(video)
- })
+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)
+ }
+ }
}
-function isEachRemoteRequestVideosQaduValid (requests: any[]) {
- return isArray(requests) &&
- requests.every(request => {
- const video = request.data
+function removeBadRequestVideosQadu (requests: any[]) {
+ for (let i = requests.length - 1; i >= 0 ; i--) {
+ const request = requests[i]
+ const video = request.data
- if (!video) return false
-
- return (
+ if (
+ !video ||
+ (
isUUIDValid(video.uuid) &&
(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)
+ }
+ }
}
-function isEachRemoteRequestVideosEventsValid (requests: any[]) {
- return isArray(requests) &&
- requests.every(request => {
- const eventData = request.data
-
- if (!eventData) return false
+function removeBadRequestVideosEvents (requests: any[]) {
+ for (let i = requests.length - 1; i >= 0 ; i--) {
+ const request = requests[i]
+ const eventData = request.data
- return (
+ if (
+ !eventData ||
+ (
isUUIDValid(eventData.uuid) &&
values(REQUEST_VIDEO_EVENT_TYPES).indexOf(eventData.eventType) !== -1 &&
isVideoEventCountValid(eventData.count)
- )
- })
+ ) === false
+ ) {
+ requests.splice(i, 1)
+ }
+ }
}
// ---------------------------------------------------------------------------
export {
- isEachRemoteRequestVideosValid,
- isEachRemoteRequestVideosQaduValid,
- isEachRemoteRequestVideosEventsValid
+ removeBadRequestVideos,
+ removeBadRequestVideosQadu,
+ removeBadRequestVideosEvents
}
// ---------------------------------------------------------------------------
function isCommonVideoAttributesValid (video: any) {
return isDateValid(video.createdAt) &&
isDateValid(video.updatedAt) &&
- isVideoCategoryValid(video.category) &&
- isVideoLicenceValid(video.licence) &&
- isVideoLanguageValid(video.language) &&
+ isRemoteVideoCategoryValid(video.category) &&
+ isRemoteVideoLicenceValid(video.licence) &&
+ isRemoteVideoLanguageValid(video.language) &&
isVideoNSFWValid(video.nsfw) &&
isVideoDescriptionValid(video.description) &&
isVideoDurationValid(video.duration) &&
return VIDEO_CATEGORIES[value] !== undefined
}
+// Maybe we don't know the remote category, but that doesn't matter
+function isRemoteVideoCategoryValid (value: string) {
+ return validator.isInt('' + value)
+}
+
function isVideoLicenceValid (value: number) {
return VIDEO_LICENCES[value] !== undefined
}
+// Maybe we don't know the remote licence, but that doesn't matter
+function isRemoteVideoLicenceValid (value: string) {
+ return validator.isInt('' + value)
+}
+
function isVideoLanguageValid (value: number) {
return value === null || VIDEO_LANGUAGES[value] !== undefined
}
+// Maybe we don't know the remote language, but that doesn't matter
+function isRemoteVideoLanguageValid (value: string) {
+ return validator.isInt('' + value)
+}
+
function isVideoNSFWValid (value: any) {
return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
}
isVideoEventCountValid,
isVideoFileSizeValid,
isVideoFileResolutionValid,
- checkVideoExists
+ checkVideoExists,
+ isRemoteVideoCategoryValid,
+ isRemoteVideoLicenceValid,
+ isRemoteVideoLanguageValid
}
// The function fire some useful callbacks
try {
const { response } = await makeSecureRequest(params)
- if (response.statusCode !== 200 && response.statusCode !== 201 && response.statusCode !== 204) {
- throw new Error('Status code not 20x : ' + response.statusCode)
+
+ // 400 because if the other pod is not up to date, it may not understand our request
+ if ([ 200, 201, 204, 400 ].indexOf(response.statusCode) === -1) {
+ throw new Error('Status code not 20x or 400 : ' + response.statusCode)
}
} catch (err) {
logger.error('Error sending secure request to %s pod.', toPod.host, err)
import {
logger,
- isEachRemoteRequestVideosValid,
- isEachRemoteRequestVideosQaduValid,
- isEachRemoteRequestVideosEventsValid
+ isArray,
+ removeBadRequestVideos,
+ removeBadRequestVideosQadu,
+ removeBadRequestVideosEvents
} from '../../../helpers'
import { checkErrors } from '../utils'
const remoteVideosValidator = [
- body('data').custom(isEachRemoteRequestVideosValid),
+ body('data').custom(isArray),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking remoteVideos parameters', { parameters: req.body })
- checkErrors(req, res, next)
+ checkErrors(req, res, () => {
+ removeBadRequestVideos(req.body.data)
+
+ return next()
+ })
}
]
const remoteQaduVideosValidator = [
- body('data').custom(isEachRemoteRequestVideosQaduValid),
+ body('data').custom(isArray),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking remoteQaduVideos parameters', { parameters: req.body })
- checkErrors(req, res, next)
+ checkErrors(req, res, () => {
+ removeBadRequestVideosQadu(req.body.data)
+
+ return next()
+ })
}
]
const remoteEventsVideosValidator = [
- body('data').custom(isEachRemoteRequestVideosEventsValid),
+ body('data').custom(isArray),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking remoteEventsVideos parameters', { parameters: req.body })
- checkErrors(req, res, next)
+ checkErrors(req, res, () => {
+ removeBadRequestVideosEvents(req.body.data)
+
+ return next()
+ })
}
]