From a2431b7dcbc72c05101dcdbe631ff84a823aeb51 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 27 Nov 2017 17:30:46 +0100 Subject: Refractor validators --- server/helpers/custom-validators/accounts.ts | 46 ++++++++-------- .../custom-validators/activitypub/account.ts | 63 ++++------------------ .../custom-validators/activitypub/videos.ts | 13 ++--- server/helpers/custom-validators/video-channels.ts | 38 +------------ server/helpers/custom-validators/videos.ts | 39 ++------------ 5 files changed, 41 insertions(+), 158 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts index a6d7f2b82..e3c477414 100644 --- a/server/helpers/custom-validators/accounts.ts +++ b/server/helpers/custom-validators/accounts.ts @@ -1,17 +1,16 @@ import * as Bluebird from 'bluebird' -import * as express from 'express' +import { Response } from 'express' import 'express-validator' import * as validator from 'validator' import { database as db } from '../../initializers' import { AccountInstance } from '../../models' -import { logger } from '../logger' import { isUserUsernameValid } from './users' function isAccountNameValid (value: string) { return isUserUsernameValid(value) } -function checkAccountIdExists (id: number | string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { +function isAccountIdExist (id: number | string, res: Response) { let promise: Bluebird if (validator.isInt('' + id)) { @@ -20,36 +19,35 @@ function checkAccountIdExists (id: number | string, res: express.Response, callb promise = db.Account.loadByUUID('' + id) } - return checkAccountExists(promise, res, callback) + return isAccountExist(promise, res) } -function checkLocalAccountNameExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { - const p = db.Account.loadLocalByName(name) +function isLocalAccountNameExist (name: string, res: Response) { + const promise = db.Account.loadLocalByName(name) - return checkAccountExists(p, res, callback) + return isAccountExist(promise, res) } -function checkAccountExists (p: Bluebird, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { - p.then(account => { - if (!account) { - return res.status(404) - .send({ error: 'Account not found' }) - .end() - } - - res.locals.account = account - return callback(null, account) - }) - .catch(err => { - logger.error('Error in account request validator.', err) - return res.sendStatus(500) - }) +async function isAccountExist (p: Bluebird, res: Response) { + const account = await p + + if (!account) { + res.status(404) + .send({ error: 'Account not found' }) + .end() + + return false + } + + res.locals.account = account + + return true } // --------------------------------------------------------------------------- export { - checkAccountIdExists, - checkLocalAccountNameExists, + isAccountIdExist, + isLocalAccountNameExist, isAccountNameValid } diff --git a/server/helpers/custom-validators/activitypub/account.ts b/server/helpers/custom-validators/activitypub/account.ts index 645f55a5a..cab39a654 100644 --- a/server/helpers/custom-validators/activitypub/account.ts +++ b/server/helpers/custom-validators/activitypub/account.ts @@ -5,31 +5,19 @@ import { exists, isUUIDValid } from '../misc' import { isActivityPubUrlValid, isBaseActivityValid } from './misc' function isAccountEndpointsObjectValid (endpointObject: any) { - return isAccountSharedInboxValid(endpointObject.sharedInbox) -} - -function isAccountSharedInboxValid (sharedInbox: string) { - return isActivityPubUrlValid(sharedInbox) + return isActivityPubUrlValid(endpointObject.sharedInbox) } function isAccountPublicKeyObjectValid (publicKeyObject: any) { - return isAccountPublicKeyIdValid(publicKeyObject.id) && - isAccountPublicKeyOwnerValid(publicKeyObject.owner) && + return isActivityPubUrlValid(publicKeyObject.id) && + isActivityPubUrlValid(publicKeyObject.owner) && isAccountPublicKeyValid(publicKeyObject.publicKeyPem) } -function isAccountPublicKeyIdValid (id: string) { - return isActivityPubUrlValid(id) -} - function isAccountTypeValid (type: string) { return type === 'Person' || type === 'Application' } -function isAccountPublicKeyOwnerValid (owner: string) { - return isActivityPubUrlValid(owner) -} - function isAccountPublicKeyValid (publicKey: string) { return exists(publicKey) && typeof publicKey === 'string' && @@ -38,34 +26,10 @@ function isAccountPublicKeyValid (publicKey: string) { validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY) } -function isAccountIdValid (id: string) { - return isActivityPubUrlValid(id) -} - -function isAccountFollowingValid (id: string) { - return isActivityPubUrlValid(id) -} - -function isAccountFollowersValid (id: string) { - return isActivityPubUrlValid(id) -} - -function isAccountInboxValid (inbox: string) { - return isActivityPubUrlValid(inbox) -} - -function isAccountOutboxValid (outbox: string) { - return isActivityPubUrlValid(outbox) -} - function isAccountPreferredUsernameValid (preferredUsername: string) { return isAccountNameValid(preferredUsername) } -function isAccountUrlValid (url: string) { - return isActivityPubUrlValid(url) -} - function isAccountPrivateKeyValid (privateKey: string) { return exists(privateKey) && typeof privateKey === 'string' && @@ -75,15 +39,15 @@ function isAccountPrivateKeyValid (privateKey: string) { } function isRemoteAccountValid (remoteAccount: any) { - return isAccountIdValid(remoteAccount.id) && + return isActivityPubUrlValid(remoteAccount.id) && isUUIDValid(remoteAccount.uuid) && isAccountTypeValid(remoteAccount.type) && - isAccountFollowingValid(remoteAccount.following) && - isAccountFollowersValid(remoteAccount.followers) && - isAccountInboxValid(remoteAccount.inbox) && - isAccountOutboxValid(remoteAccount.outbox) && + isActivityPubUrlValid(remoteAccount.following) && + isActivityPubUrlValid(remoteAccount.followers) && + isActivityPubUrlValid(remoteAccount.inbox) && + isActivityPubUrlValid(remoteAccount.outbox) && isAccountPreferredUsernameValid(remoteAccount.preferredUsername) && - isAccountUrlValid(remoteAccount.url) && + isActivityPubUrlValid(remoteAccount.url) && isAccountPublicKeyObjectValid(remoteAccount.publicKey) && isAccountEndpointsObjectValid(remoteAccount.endpoints) } @@ -113,19 +77,10 @@ function isAccountAcceptActivityValid (activity: any) { export { isAccountEndpointsObjectValid, - isAccountSharedInboxValid, isAccountPublicKeyObjectValid, - isAccountPublicKeyIdValid, isAccountTypeValid, - isAccountPublicKeyOwnerValid, isAccountPublicKeyValid, - isAccountIdValid, - isAccountFollowingValid, - isAccountFollowersValid, - isAccountInboxValid, - isAccountOutboxValid, isAccountPreferredUsernameValid, - isAccountUrlValid, isAccountPrivateKeyValid, isRemoteAccountValid, isAccountFollowingCountValid, diff --git a/server/helpers/custom-validators/activitypub/videos.ts b/server/helpers/custom-validators/activitypub/videos.ts index 55e79c4e8..12c672fd2 100644 --- a/server/helpers/custom-validators/activitypub/videos.ts +++ b/server/helpers/custom-validators/activitypub/videos.ts @@ -8,7 +8,6 @@ import { isVideoNSFWValid, isVideoTagValid, isVideoTruncatedDescriptionValid, - isVideoUrlValid, isVideoViewsValid } from '../videos' import { isActivityPubUrlValid, isBaseActivityValid } from './misc' @@ -77,12 +76,11 @@ export { function setValidRemoteTags (video: any) { if (Array.isArray(video.tag) === false) return false - const newTag = video.tag.filter(t => { + video.tag = video.tag.filter(t => { return t.type === 'Hashtag' && isVideoTagValid(t.name) }) - video.tag = newTag return true } @@ -96,7 +94,7 @@ function isRemoteVideoContentValid (mediaType: string, content: string) { function isRemoteVideoIconValid (icon: any) { return icon.type === 'Image' && - isVideoUrlValid(icon.url) && + isActivityPubUrlValid(icon.url) && icon.mediaType === 'image/jpeg' && validator.isInt(icon.width + '', { min: 0 }) && validator.isInt(icon.height + '', { min: 0 }) @@ -105,8 +103,7 @@ function isRemoteVideoIconValid (icon: any) { function setValidRemoteVideoUrls (video: any) { if (Array.isArray(video.url) === false) return false - const newUrl = video.url.filter(u => isRemoteVideoUrlValid(u)) - video.url = newUrl + video.url = video.url.filter(u => isRemoteVideoUrlValid(u)) return true } @@ -115,13 +112,13 @@ function isRemoteVideoUrlValid (url: any) { return url.type === 'Link' && ( ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mimeType) !== -1 && - isVideoUrlValid(url.url) && + isActivityPubUrlValid(url.url) && validator.isInt(url.width + '', { min: 0 }) && validator.isInt(url.size + '', { min: 0 }) ) || ( ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mimeType) !== -1 && - isVideoUrlValid(url.url) && + isActivityPubUrlValid(url.url) && validator.isInt(url.width + '', { min: 0 }) ) || ( diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts index 267d987fc..3de9f041b 100644 --- a/server/helpers/custom-validators/video-channels.ts +++ b/server/helpers/custom-validators/video-channels.ts @@ -1,21 +1,13 @@ -import * as Bluebird from 'bluebird' import * as express from 'express' import 'express-validator' import 'multer' import * as validator from 'validator' - import { CONSTRAINTS_FIELDS, database as db } from '../../initializers' import { VideoChannelInstance } from '../../models' -import { logger } from '../logger' -import { isActivityPubUrlValid } from './index' import { exists } from './misc' const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS -function isVideoChannelUrlValid (value: string) { - return isActivityPubUrlValid(value) -} - function isVideoChannelDescriptionValid (value: string) { return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION) } @@ -24,31 +16,7 @@ function isVideoChannelNameValid (value: string) { return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME) } -function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) { - let promise: Bluebird - if (validator.isInt(id)) { - promise = db.VideoChannel.loadAndPopulateAccount(+id) - } else { // UUID - promise = db.VideoChannel.loadByUUIDAndPopulateAccount(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) - }) -} - -async function isVideoChannelExistsPromise (id: string, res: express.Response) { +async function isVideoChannelExist (id: string, res: express.Response) { let videoChannel: VideoChannelInstance if (validator.isInt(id)) { videoChannel = await db.VideoChannel.loadAndPopulateAccount(+id) @@ -72,8 +40,6 @@ async function isVideoChannelExistsPromise (id: string, res: express.Response) { export { isVideoChannelDescriptionValid, - checkVideoChannelExists, isVideoChannelNameValid, - isVideoChannelExistsPromise, - isVideoChannelUrlValid + isVideoChannelExist } diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index 276354626..f13178c54 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts @@ -1,4 +1,3 @@ -import * as Bluebird from 'bluebird' import { Response } from 'express' import 'express-validator' import { values } from 'lodash' @@ -6,12 +5,10 @@ import 'multer' import * as validator from 'validator' import { VideoRateType } from '../../../shared' import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_RATE_TYPES } from '../../initializers' +import { VIDEO_PRIVACIES } from '../../initializers/constants' import { database as db } from '../../initializers/database' import { VideoInstance } from '../../models/video/video-interface' -import { logger } from '../logger' -import { isActivityPubUrlValid } from './activitypub/misc' import { exists, isArray } from './misc' -import { VIDEO_PRIVACIES } from '../../initializers/constants' const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES @@ -20,10 +17,6 @@ function isVideoCategoryValid (value: number) { return VIDEO_CATEGORIES[value] !== undefined } -function isVideoUrlValid (value: string) { - return isActivityPubUrlValid(value) -} - function isVideoLicenceValid (value: number) { return VIDEO_LICENCES[value] !== undefined } @@ -106,31 +99,7 @@ function isVideoFileSizeValid (value: string) { return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE) } -function checkVideoExists (id: string, res: Response, callback: () => void) { - let promise: Bluebird - if (validator.isInt(id)) { - promise = db.Video.loadAndPopulateAccountAndServerAndTags(+id) - } else { // UUID - promise = db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(id) - } - - promise.then(video => { - if (!video) { - return res.status(404) - .json({ error: 'Video not found' }) - .end() - } - - res.locals.video = video - callback() - }) - .catch(err => { - logger.error('Error in video request validator.', err) - return res.sendStatus(500) - }) -} - -async function isVideoExistsPromise (id: string, res: Response) { +async function isVideoExist (id: string, res: Response) { let video: VideoInstance if (validator.isInt(id)) { @@ -169,10 +138,8 @@ export { isVideoRatingTypeValid, isVideoDurationValid, isVideoTagValid, - isVideoUrlValid, isVideoPrivacyValid, isVideoFileResolutionValid, isVideoFileSizeValid, - checkVideoExists, - isVideoExistsPromise + isVideoExist } -- cgit v1.2.3