From: Chocobozzz Date: Tue, 17 Jan 2017 19:59:16 +0000 (+0100) Subject: Server: remote video validators refractoring X-Git-Tag: v0.0.1-alpha~559 X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=da691c46df2f3c542d4fffaaa99ef18b6ef71f53;p=github%2FChocobozzz%2FPeerTube.git Server: remote video validators refractoring --- diff --git a/server/controllers/api/videos.js b/server/controllers/api/videos.js index 75a661bcc..c936105e7 100644 --- a/server/controllers/api/videos.js +++ b/server/controllers/api/videos.js @@ -389,11 +389,7 @@ function reportVideoAbuse (req, res, finalCallback) { waterfall([ - function startTransaction (callback) { - db.sequelize.transaction().asCallback(function (err, t) { - return callback(err, t) - }) - }, + databaseUtils.startSerializableTransaction, function createAbuse (t, callback) { db.VideoAbuse.create(abuse).asCallback(function (err, abuse) { diff --git a/server/helpers/custom-validators/remote/videos.js b/server/helpers/custom-validators/remote/videos.js index 7c27b9dbb..ee68ebc10 100644 --- a/server/helpers/custom-validators/remote/videos.js +++ b/server/helpers/custom-validators/remote/videos.js @@ -1,8 +1,11 @@ 'use strict' +const constants = require('../../../initializers/constants') const videosValidators = require('../videos') const miscValidators = require('../misc') +const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS] + const remoteVideosValidators = { isEachRemoteRequestVideosValid } @@ -13,29 +16,13 @@ function isEachRemoteRequestVideosValid (requests) { const video = request.data return ( isRequestTypeAddValid(request.type) && + isCommonVideoAttrbiutesValid(video) && videosValidators.isVideoAuthorValid(video.author) && - videosValidators.isVideoDateValid(video.createdAt) && - videosValidators.isVideoDateValid(video.updatedAt) && - videosValidators.isVideoDescriptionValid(video.description) && - videosValidators.isVideoDurationValid(video.duration) && - videosValidators.isVideoInfoHashValid(video.infoHash) && - videosValidators.isVideoNameValid(video.name) && - videosValidators.isVideoTagsValid(video.tags) && - videosValidators.isVideoThumbnailDataValid(video.thumbnailData) && - videosValidators.isVideoRemoteIdValid(video.remoteId) && - videosValidators.isVideoExtnameValid(video.extname) + videosValidators.isVideoThumbnailDataValid(video.thumbnailData) ) || ( isRequestTypeUpdateValid(request.type) && - videosValidators.isVideoDateValid(video.createdAt) && - videosValidators.isVideoDateValid(video.updatedAt) && - videosValidators.isVideoDescriptionValid(video.description) && - videosValidators.isVideoDurationValid(video.duration) && - videosValidators.isVideoInfoHashValid(video.infoHash) && - videosValidators.isVideoNameValid(video.name) && - videosValidators.isVideoTagsValid(video.tags) && - videosValidators.isVideoRemoteIdValid(video.remoteId) && - videosValidators.isVideoExtnameValid(video.extname) + isCommonVideoAttrbiutesValid(video) ) || ( isRequestTypeRemoveValid(request.type) && @@ -56,18 +43,30 @@ module.exports = remoteVideosValidators // --------------------------------------------------------------------------- +function isCommonVideoAttrbiutesValid (video) { + return videosValidators.isVideoDateValid(video.createdAt) && + videosValidators.isVideoDateValid(video.updatedAt) && + videosValidators.isVideoDescriptionValid(video.description) && + videosValidators.isVideoDurationValid(video.duration) && + videosValidators.isVideoInfoHashValid(video.infoHash) && + videosValidators.isVideoNameValid(video.name) && + videosValidators.isVideoTagsValid(video.tags) && + videosValidators.isVideoRemoteIdValid(video.remoteId) && + videosValidators.isVideoExtnameValid(video.extname) +} + function isRequestTypeAddValid (value) { - return value === 'add' + return value === ENDPOINT_ACTIONS.ADD } function isRequestTypeUpdateValid (value) { - return value === 'update' + return value === ENDPOINT_ACTIONS.UPDATE } function isRequestTypeRemoveValid (value) { - return value === 'remove' + return value === ENDPOINT_ACTIONS.REMOVE } function isRequestTypeReportAbuseValid (value) { - return value === 'report-abuse' + return value === ENDPOINT_ACTIONS.REPORT_ABUSE } diff --git a/server/lib/friends.js b/server/lib/friends.js index f0575ff2f..f634aedbb 100644 --- a/server/lib/friends.js +++ b/server/lib/friends.js @@ -12,6 +12,8 @@ const db = require('../initializers/database') const logger = require('../helpers/logger') const requests = require('../helpers/requests') +const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS] + const friends = { addVideoToFriends, updateVideoToFriends, @@ -26,7 +28,7 @@ const friends = { function addVideoToFriends (videoData, transaction, callback) { const options = { - type: 'add', + type: ENDPOINT_ACTIONS.ADD, endpoint: constants.REQUEST_ENDPOINTS.VIDEOS, data: videoData, transaction @@ -36,7 +38,7 @@ function addVideoToFriends (videoData, transaction, callback) { function updateVideoToFriends (videoData, transaction, callback) { const options = { - type: 'update', + type: ENDPOINT_ACTIONS.UPDATE, endpoint: constants.REQUEST_ENDPOINTS.VIDEOS, data: videoData, transaction @@ -46,7 +48,7 @@ function updateVideoToFriends (videoData, transaction, callback) { function removeVideoToFriends (videoParams) { const options = { - type: 'remove', + type: ENDPOINT_ACTIONS.REMOVE, endpoint: constants.REQUEST_ENDPOINTS.VIDEOS, data: videoParams } @@ -55,7 +57,7 @@ function removeVideoToFriends (videoParams) { function reportAbuseVideoToFriend (reportData, video) { const options = { - type: 'report-abuse', + type: ENDPOINT_ACTIONS.REPORT_ABUSE, endpoint: constants.REQUEST_ENDPOINTS.VIDEOS, data: reportData, toIds: [ video.Author.podId ]