From e4c87ec26962e359d1c70b03ed188a3f19d6a25b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 26 Feb 2017 18:57:33 +0100 Subject: Server: implement video views --- server/helpers/custom-validators/remote/videos.js | 24 ++++++++++++++++++++++- server/helpers/custom-validators/videos.js | 14 +++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) (limited to 'server/helpers/custom-validators') diff --git a/server/helpers/custom-validators/remote/videos.js b/server/helpers/custom-validators/remote/videos.js index 2e9cf822e..c1786014d 100644 --- a/server/helpers/custom-validators/remote/videos.js +++ b/server/helpers/custom-validators/remote/videos.js @@ -1,6 +1,7 @@ 'use strict' const has = require('lodash/has') +const values = require('lodash/values') const constants = require('../../../initializers/constants') const videosValidators = require('../videos') @@ -10,13 +11,17 @@ const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_EN const remoteVideosValidators = { isEachRemoteRequestVideosValid, - isEachRemoteRequestVideosQaduValid + isEachRemoteRequestVideosQaduValid, + isEachRemoteRequestVideosEventsValid } function isEachRemoteRequestVideosValid (requests) { return miscValidators.isArray(requests) && requests.every(function (request) { const video = request.data + + if (!video) return false + return ( isRequestTypeAddValid(request.type) && isCommonVideoAttributesValid(video) && @@ -45,6 +50,8 @@ function isEachRemoteRequestVideosQaduValid (requests) { requests.every(function (request) { const video = request.data + if (!video) return false + return ( videosValidators.isVideoRemoteIdValid(video.remoteId) && (has(video, 'views') === false || videosValidators.isVideoViewsValid) && @@ -54,6 +61,21 @@ function isEachRemoteRequestVideosQaduValid (requests) { }) } +function isEachRemoteRequestVideosEventsValid (requests) { + return miscValidators.isArray(requests) && + requests.every(function (request) { + const eventData = request.data + + if (!eventData) return false + + return ( + videosValidators.isVideoRemoteIdValid(eventData.remoteId) && + values(constants.REQUEST_VIDEO_EVENT_TYPES).indexOf(eventData.eventType) !== -1 && + videosValidators.isVideoEventCountValid(eventData.count) + ) + }) +} + // --------------------------------------------------------------------------- module.exports = remoteVideosValidators diff --git a/server/helpers/custom-validators/videos.js b/server/helpers/custom-validators/videos.js index 1d844118b..c5a1f3cb5 100644 --- a/server/helpers/custom-validators/videos.js +++ b/server/helpers/custom-validators/videos.js @@ -7,6 +7,7 @@ const usersValidators = require('./users') const miscValidators = require('./misc') const VIDEOS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEOS const VIDEO_ABUSES_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEO_ABUSES +const VIDEO_EVENTS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEO_EVENTS const videosValidators = { isVideoAuthorValid, @@ -25,7 +26,8 @@ const videosValidators = { isVideoFile, isVideoViewsValid, isVideoLikesValid, - isVideoDislikesValid + isVideoDislikesValid, + isVideoEventCountValid } function isVideoAuthorValid (value) { @@ -86,15 +88,19 @@ function isVideoAbuseReporterUsernameValid (value) { } function isVideoViewsValid (value) { - return validator.isInt(value, { min: 0 }) + return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS) } function isVideoLikesValid (value) { - return validator.isInt(value, { min: 0 }) + return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES) } function isVideoDislikesValid (value) { - return validator.isInt(value, { min: 0 }) + return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES) +} + +function isVideoEventCountValid (value) { + return validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT) } function isVideoFile (value, files) { -- cgit v1.2.3