From 4e50b6a1c9a3eb261e04ede73241648e6edf21d6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 27 Nov 2017 14:44:51 +0100 Subject: Add shares forward and collection on videos/video channels --- server/helpers/custom-validators/accounts.ts | 38 ++++++++++++++-------- server/helpers/custom-validators/video-channels.ts | 33 +++++++++++++++---- server/helpers/custom-validators/videos.ts | 24 +++++++++++++- 3 files changed, 75 insertions(+), 20 deletions(-) (limited to 'server/helpers/custom-validators') diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts index fe0fc650a..a6d7f2b82 100644 --- a/server/helpers/custom-validators/accounts.ts +++ b/server/helpers/custom-validators/accounts.ts @@ -1,4 +1,4 @@ -import * as Promise from 'bluebird' +import * as Bluebird from 'bluebird' import * as express from 'express' import 'express-validator' import * as validator from 'validator' @@ -11,33 +11,45 @@ function isAccountNameValid (value: string) { return isUserUsernameValid(value) } -function checkVideoAccountExists (id: string, res: express.Response, callback: () => void) { - let promise: Promise - if (validator.isInt(id)) { +function checkAccountIdExists (id: number | string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { + let promise: Bluebird + + if (validator.isInt('' + id)) { promise = db.Account.load(+id) } else { // UUID - promise = db.Account.loadByUUID(id) + promise = db.Account.loadByUUID('' + id) } - promise.then(account => { + return checkAccountExists(promise, res, callback) +} + +function checkLocalAccountNameExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { + const p = db.Account.loadLocalByName(name) + + return checkAccountExists(p, res, callback) +} + +function checkAccountExists (p: Bluebird, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { + p.then(account => { if (!account) { return res.status(404) - .json({ error: 'Video account not found' }) + .send({ error: 'Account not found' }) .end() } res.locals.account = account - callback() - }) - .catch(err => { - logger.error('Error in video account request validator.', err) - return res.sendStatus(500) + return callback(null, account) }) + .catch(err => { + logger.error('Error in account request validator.', err) + return res.sendStatus(500) + }) } // --------------------------------------------------------------------------- export { - checkVideoAccountExists, + checkAccountIdExists, + checkLocalAccountNameExists, isAccountNameValid } diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts index 5de01f74b..267d987fc 100644 --- a/server/helpers/custom-validators/video-channels.ts +++ b/server/helpers/custom-validators/video-channels.ts @@ -1,14 +1,14 @@ -import * as Promise from 'bluebird' -import * as validator from 'validator' +import * as Bluebird from 'bluebird' import * as express from 'express' import 'express-validator' import 'multer' +import * as validator from 'validator' -import { database as db, CONSTRAINTS_FIELDS } from '../../initializers' +import { CONSTRAINTS_FIELDS, database as db } from '../../initializers' import { VideoChannelInstance } from '../../models' import { logger } from '../logger' -import { exists } from './misc' import { isActivityPubUrlValid } from './index' +import { exists } from './misc' const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS @@ -25,7 +25,7 @@ function isVideoChannelNameValid (value: string) { } function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) { - let promise: Promise + let promise: Bluebird if (validator.isInt(id)) { promise = db.VideoChannel.loadAndPopulateAccount(+id) } else { // UUID @@ -48,11 +48,32 @@ function checkVideoChannelExists (id: string, res: express.Response, callback: ( }) } +async function isVideoChannelExistsPromise (id: string, res: express.Response) { + let videoChannel: VideoChannelInstance + if (validator.isInt(id)) { + videoChannel = await db.VideoChannel.loadAndPopulateAccount(+id) + } else { // UUID + videoChannel = await db.VideoChannel.loadByUUIDAndPopulateAccount(id) + } + + if (!videoChannel) { + res.status(404) + .json({ error: 'Video channel not found' }) + .end() + + return false + } + + res.locals.videoChannel = videoChannel + return true +} + // --------------------------------------------------------------------------- export { isVideoChannelDescriptionValid, - isVideoChannelNameValid, checkVideoChannelExists, + isVideoChannelNameValid, + isVideoChannelExistsPromise, isVideoChannelUrlValid } diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index 205d8c62f..276354626 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts @@ -130,6 +130,27 @@ function checkVideoExists (id: string, res: Response, callback: () => void) { }) } +async function isVideoExistsPromise (id: string, res: Response) { + let video: VideoInstance + + if (validator.isInt(id)) { + video = await db.Video.loadAndPopulateAccountAndServerAndTags(+id) + } else { // UUID + video = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(id) + } + + if (!video) { + res.status(404) + .json({ error: 'Video not found' }) + .end() + + return false + } + + res.locals.video = video + return true +} + // --------------------------------------------------------------------------- export { @@ -152,5 +173,6 @@ export { isVideoPrivacyValid, isVideoFileResolutionValid, isVideoFileSizeValid, - checkVideoExists + checkVideoExists, + isVideoExistsPromise } -- cgit v1.2.3