From 3e753302d8c911b59971c16a8018df0e1ab78465 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 23 Jul 2019 10:40:39 +0200 Subject: Refactor middleware helpers --- server/helpers/custom-validators/accounts.ts | 40 ----------- server/helpers/custom-validators/video-abuses.ts | 16 ----- .../helpers/custom-validators/video-blacklist.ts | 20 +----- server/helpers/custom-validators/video-captions.ts | 21 +----- server/helpers/custom-validators/video-channels.ts | 21 ------ .../helpers/custom-validators/video-playlists.ts | 18 ----- server/helpers/custom-validators/videos.ts | 73 ------------------- server/helpers/middlewares/accounts.ts | 46 ++++++++++++ server/helpers/middlewares/index.ts | 7 ++ server/helpers/middlewares/video-abuses.ts | 41 +++++++++++ server/helpers/middlewares/video-blacklists.ts | 23 ++++++ server/helpers/middlewares/video-captions.ts | 24 +++++++ server/helpers/middlewares/video-channels.ts | 23 ++++++ server/helpers/middlewares/video-playlists.ts | 25 +++++++ server/helpers/middlewares/videos.ts | 82 ++++++++++++++++++++++ 15 files changed, 273 insertions(+), 207 deletions(-) create mode 100644 server/helpers/middlewares/accounts.ts create mode 100644 server/helpers/middlewares/index.ts create mode 100644 server/helpers/middlewares/video-abuses.ts create mode 100644 server/helpers/middlewares/video-blacklists.ts create mode 100644 server/helpers/middlewares/video-captions.ts create mode 100644 server/helpers/middlewares/video-channels.ts create mode 100644 server/helpers/middlewares/video-playlists.ts create mode 100644 server/helpers/middlewares/videos.ts (limited to 'server/helpers') diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts index 31a2de5ca..be196d2a4 100644 --- a/server/helpers/custom-validators/accounts.ts +++ b/server/helpers/custom-validators/accounts.ts @@ -1,7 +1,4 @@ -import * as Bluebird from 'bluebird' -import { Response } from 'express' import 'express-validator' -import { AccountModel } from '../../models/account/account' import { isUserDescriptionValid, isUserUsernameValid } from './users' import { exists } from './misc' @@ -17,47 +14,10 @@ function isAccountDescriptionValid (value: string) { return isUserDescriptionValid(value) } -function doesAccountIdExist (id: number, res: Response, sendNotFound = true) { - const promise = AccountModel.load(id) - - return doesAccountExist(promise, res, sendNotFound) -} - -function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) { - const promise = AccountModel.loadLocalByName(name) - - return doesAccountExist(promise, res, sendNotFound) -} - -function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) { - return doesAccountExist(AccountModel.loadByNameWithHost(nameWithDomain), res, sendNotFound) -} - -async function doesAccountExist (p: Bluebird, res: Response, sendNotFound: boolean) { - const account = await p - - if (!account) { - if (sendNotFound === true) { - res.status(404) - .send({ error: 'Account not found' }) - .end() - } - - return false - } - - res.locals.account = account - - return true -} - // --------------------------------------------------------------------------- export { isAccountIdValid, - doesAccountIdExist, - doesLocalAccountNameExist, isAccountDescriptionValid, - doesAccountNameWithHostExist, isAccountNameValid } diff --git a/server/helpers/custom-validators/video-abuses.ts b/server/helpers/custom-validators/video-abuses.ts index a61dcee1c..e43d12be8 100644 --- a/server/helpers/custom-validators/video-abuses.ts +++ b/server/helpers/custom-validators/video-abuses.ts @@ -18,25 +18,9 @@ function isVideoAbuseStateValid (value: string) { return exists(value) && VIDEO_ABUSE_STATES[ value ] !== undefined } -async function doesVideoAbuseExist (abuseId: number, videoId: number, res: Response) { - const videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, videoId) - - if (videoAbuse === null) { - res.status(404) - .json({ error: 'Video abuse not found' }) - .end() - - return false - } - - res.locals.videoAbuse = videoAbuse - return true -} - // --------------------------------------------------------------------------- export { - doesVideoAbuseExist, isVideoAbuseStateValid, isVideoAbuseReasonValid, isVideoAbuseModerationCommentValid diff --git a/server/helpers/custom-validators/video-blacklist.ts b/server/helpers/custom-validators/video-blacklist.ts index 3743f7023..9a44332ef 100644 --- a/server/helpers/custom-validators/video-blacklist.ts +++ b/server/helpers/custom-validators/video-blacklist.ts @@ -1,8 +1,6 @@ -import { Response } from 'express' import * as validator from 'validator' import { exists } from './misc' import { CONSTRAINTS_FIELDS } from '../../initializers/constants' -import { VideoBlacklistModel } from '../../models/video/video-blacklist' import { VideoBlacklistType } from '../../../shared/models/videos' const VIDEO_BLACKLIST_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_BLACKLIST @@ -11,21 +9,6 @@ function isVideoBlacklistReasonValid (value: string) { return value === null || validator.isLength(value, VIDEO_BLACKLIST_CONSTRAINTS_FIELDS.REASON) } -async function doesVideoBlacklistExist (videoId: number, res: Response) { - const videoBlacklist = await VideoBlacklistModel.loadByVideoId(videoId) - - if (videoBlacklist === null) { - res.status(404) - .json({ error: 'Blacklisted video not found' }) - .end() - - return false - } - - res.locals.videoBlacklist = videoBlacklist - return true -} - function isVideoBlacklistTypeValid (value: any) { return exists(value) && validator.isInt('' + value) && VideoBlacklistType[value] !== undefined } @@ -34,6 +17,5 @@ function isVideoBlacklistTypeValid (value: any) { export { isVideoBlacklistReasonValid, - isVideoBlacklistTypeValid, - doesVideoBlacklistExist + isVideoBlacklistTypeValid } diff --git a/server/helpers/custom-validators/video-captions.ts b/server/helpers/custom-validators/video-captions.ts index 3b6569a8a..d06eb3695 100644 --- a/server/helpers/custom-validators/video-captions.ts +++ b/server/helpers/custom-validators/video-captions.ts @@ -1,8 +1,5 @@ import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_LANGUAGES } from '../../initializers/constants' import { exists, isFileValid } from './misc' -import { Response } from 'express' -import { VideoModel } from '../../models/video/video' -import { VideoCaptionModel } from '../../models/video/video-caption' function isVideoCaptionLanguageValid (value: any) { return exists(value) && VIDEO_LANGUAGES[ value ] !== undefined @@ -16,25 +13,9 @@ function isVideoCaptionFile (files: { [ fieldname: string ]: Express.Multer.File return isFileValid(files, videoCaptionTypesRegex, field, CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) } -async function doesVideoCaptionExist (video: VideoModel, language: string, res: Response) { - const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(video.id, language) - - if (!videoCaption) { - res.status(404) - .json({ error: 'Video caption not found' }) - .end() - - return false - } - - res.locals.videoCaption = videoCaption - return true -} - // --------------------------------------------------------------------------- export { isVideoCaptionFile, - isVideoCaptionLanguageValid, - doesVideoCaptionExist + isVideoCaptionLanguageValid } diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts index f818ce8f1..0471f6ec4 100644 --- a/server/helpers/custom-validators/video-channels.ts +++ b/server/helpers/custom-validators/video-channels.ts @@ -20,33 +20,12 @@ function isVideoChannelSupportValid (value: string) { return value === null || (exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.SUPPORT)) } -async function doesLocalVideoChannelNameExist (name: string, res: express.Response) { - const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name) - - return processVideoChannelExist(videoChannel, res) -} - -async function doesVideoChannelIdExist (id: number, res: express.Response) { - const videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id) - - return processVideoChannelExist(videoChannel, res) -} - -async function doesVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) { - const videoChannel = await VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithDomain) - - return processVideoChannelExist(videoChannel, res) -} - // --------------------------------------------------------------------------- export { - doesVideoChannelNameWithHostExist, - doesLocalVideoChannelNameExist, isVideoChannelDescriptionValid, isVideoChannelNameValid, isVideoChannelSupportValid, - doesVideoChannelIdExist } function processVideoChannelExist (videoChannel: VideoChannelModel, res: express.Response) { diff --git a/server/helpers/custom-validators/video-playlists.ts b/server/helpers/custom-validators/video-playlists.ts index 2fe426560..60125dcda 100644 --- a/server/helpers/custom-validators/video-playlists.ts +++ b/server/helpers/custom-validators/video-playlists.ts @@ -26,27 +26,9 @@ function isVideoPlaylistTypeValid (value: any) { return exists(value) && VIDEO_PLAYLIST_TYPES[ value ] !== undefined } -async function doesVideoPlaylistExist (id: number | string, res: express.Response, fetchType: 'summary' | 'all' = 'summary') { - const videoPlaylist = fetchType === 'summary' - ? await VideoPlaylistModel.loadWithAccountAndChannelSummary(id, undefined) - : await VideoPlaylistModel.loadWithAccountAndChannel(id, undefined) - - if (!videoPlaylist) { - res.status(404) - .json({ error: 'Video playlist not found' }) - .end() - - return false - } - - res.locals.videoPlaylist = videoPlaylist - return true -} - // --------------------------------------------------------------------------- export { - doesVideoPlaylistExist, isVideoPlaylistNameValid, isVideoPlaylistDescriptionValid, isVideoPlaylistPrivacyValid, diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index 214db17a1..157e1a8e3 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts @@ -15,10 +15,8 @@ import { } from '../../initializers/constants' import { VideoModel } from '../../models/video/video' import { exists, isArray, isDateValid, isFileValid } from './misc' -import { VideoChannelModel } from '../../models/video/video-channel' import { UserModel } from '../../models/account/user' import * as magnetUtil from 'magnet-uri' -import { fetchVideo, VideoFetchType } from '../video' const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS @@ -143,79 +141,10 @@ function isVideoMagnetUriValid (value: string) { return parsed && isVideoFileInfoHashValid(parsed.infoHash) } -function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: Response) { - // Retrieve the user who did the request - if (video.isOwned() === false) { - res.status(403) - .json({ error: 'Cannot manage a video of another server.' }) - .end() - return false - } - - // Check if the user can delete the video - // The user can delete it if he has the right - // Or if s/he is the video's account - const account = video.VideoChannel.Account - if (user.hasRight(right) === false && account.userId !== user.id) { - res.status(403) - .json({ error: 'Cannot manage a video of another user.' }) - .end() - return false - } - - return true -} - -async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') { - const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined - - const video = await fetchVideo(id, fetchType, userId) - - if (video === null) { - res.status(404) - .json({ error: 'Video not found' }) - .end() - - return false - } - - if (fetchType !== 'none') res.locals.video = video - return true -} - -async function doesVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) { - if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) { - const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId) - if (videoChannel === null) { - res.status(400) - .json({ error: 'Unknown video `video channel` on this instance.' }) - .end() - - return false - } - - res.locals.videoChannel = videoChannel - return true - } - - const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id) - if (videoChannel === null) { - res.status(400) - .json({ error: 'Unknown video `video channel` for this account.' }) - .end() - - return false - } - - res.locals.videoChannel = videoChannel - return true -} - // --------------------------------------------------------------------------- export { isVideoCategoryValid, - checkUserCanManageVideo, isVideoLicenceValid, isVideoLanguageValid, isVideoTruncatedDescriptionValid, @@ -237,9 +166,7 @@ export { isVideoPrivacyValid, isVideoFileResolutionValid, isVideoFileSizeValid, - doesVideoExist, isVideoImage, - doesVideoChannelOfAccountExist, isVideoSupportValid, isVideoFilterValid } diff --git a/server/helpers/middlewares/accounts.ts b/server/helpers/middlewares/accounts.ts new file mode 100644 index 000000000..791022b97 --- /dev/null +++ b/server/helpers/middlewares/accounts.ts @@ -0,0 +1,46 @@ +import { Response } from 'express' +import { AccountModel } from '../../models/account/account' +import * as Bluebird from 'bluebird' + +function doesAccountIdExist (id: number, res: Response, sendNotFound = true) { + const promise = AccountModel.load(id) + + return doesAccountExist(promise, res, sendNotFound) +} + +function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) { + const promise = AccountModel.loadLocalByName(name) + + return doesAccountExist(promise, res, sendNotFound) +} + +function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) { + return doesAccountExist(AccountModel.loadByNameWithHost(nameWithDomain), res, sendNotFound) +} + +async function doesAccountExist (p: Bluebird, res: Response, sendNotFound: boolean) { + const account = await p + + if (!account) { + if (sendNotFound === true) { + res.status(404) + .send({ error: 'Account not found' }) + .end() + } + + return false + } + + res.locals.account = account + + return true +} + +// --------------------------------------------------------------------------- + +export { + doesAccountIdExist, + doesLocalAccountNameExist, + doesAccountNameWithHostExist, + doesAccountExist +} diff --git a/server/helpers/middlewares/index.ts b/server/helpers/middlewares/index.ts new file mode 100644 index 000000000..f91aeaa12 --- /dev/null +++ b/server/helpers/middlewares/index.ts @@ -0,0 +1,7 @@ +export * from './accounts' +export * from './video-abuses' +export * from './video-blacklists' +export * from './video-captions' +export * from './video-channels' +export * from './video-playlists' +export * from './videos' diff --git a/server/helpers/middlewares/video-abuses.ts b/server/helpers/middlewares/video-abuses.ts new file mode 100644 index 000000000..b23f1f021 --- /dev/null +++ b/server/helpers/middlewares/video-abuses.ts @@ -0,0 +1,41 @@ +import * as express from 'express' +import { VideoChannelModel } from '../../models/video/video-channel' + +async function doesLocalVideoChannelNameExist (name: string, res: express.Response) { + const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name) + + return processVideoChannelExist(videoChannel, res) +} + +async function doesVideoChannelIdExist (id: number, res: express.Response) { + const videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id) + + return processVideoChannelExist(videoChannel, res) +} + +async function doesVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) { + const videoChannel = await VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithDomain) + + return processVideoChannelExist(videoChannel, res) +} + +// --------------------------------------------------------------------------- + +export { + doesLocalVideoChannelNameExist, + doesVideoChannelIdExist, + doesVideoChannelNameWithHostExist +} + +function processVideoChannelExist (videoChannel: VideoChannelModel, res: express.Response) { + if (!videoChannel) { + res.status(404) + .json({ error: 'Video channel not found' }) + .end() + + return false + } + + res.locals.videoChannel = videoChannel + return true +} diff --git a/server/helpers/middlewares/video-blacklists.ts b/server/helpers/middlewares/video-blacklists.ts new file mode 100644 index 000000000..c79420a0c --- /dev/null +++ b/server/helpers/middlewares/video-blacklists.ts @@ -0,0 +1,23 @@ +import { Response } from 'express' +import { VideoBlacklistModel } from '../../models/video/video-blacklist' + +async function doesVideoBlacklistExist (videoId: number, res: Response) { + const videoBlacklist = await VideoBlacklistModel.loadByVideoId(videoId) + + if (videoBlacklist === null) { + res.status(404) + .json({ error: 'Blacklisted video not found' }) + .end() + + return false + } + + res.locals.videoBlacklist = videoBlacklist + return true +} + +// --------------------------------------------------------------------------- + +export { + doesVideoBlacklistExist +} diff --git a/server/helpers/middlewares/video-captions.ts b/server/helpers/middlewares/video-captions.ts new file mode 100644 index 000000000..dc3d0144b --- /dev/null +++ b/server/helpers/middlewares/video-captions.ts @@ -0,0 +1,24 @@ +import { VideoModel } from '../../models/video/video' +import { Response } from 'express' +import { VideoCaptionModel } from '../../models/video/video-caption' + +async function doesVideoCaptionExist (video: VideoModel, language: string, res: Response) { + const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(video.id, language) + + if (!videoCaption) { + res.status(404) + .json({ error: 'Video caption not found' }) + .end() + + return false + } + + res.locals.videoCaption = videoCaption + return true +} + +// --------------------------------------------------------------------------- + +export { + doesVideoCaptionExist +} diff --git a/server/helpers/middlewares/video-channels.ts b/server/helpers/middlewares/video-channels.ts new file mode 100644 index 000000000..1b573ca37 --- /dev/null +++ b/server/helpers/middlewares/video-channels.ts @@ -0,0 +1,23 @@ +import { Response } from 'express' +import { VideoAbuseModel } from '../../models/video/video-abuse' + +async function doesVideoAbuseExist (abuseId: number, videoId: number, res: Response) { + const videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, videoId) + + if (videoAbuse === null) { + res.status(404) + .json({ error: 'Video abuse not found' }) + .end() + + return false + } + + res.locals.videoAbuse = videoAbuse + return true +} + +// --------------------------------------------------------------------------- + +export { + doesVideoAbuseExist +} diff --git a/server/helpers/middlewares/video-playlists.ts b/server/helpers/middlewares/video-playlists.ts new file mode 100644 index 000000000..735bf362f --- /dev/null +++ b/server/helpers/middlewares/video-playlists.ts @@ -0,0 +1,25 @@ +import * as express from 'express' +import { VideoPlaylistModel } from '../../models/video/video-playlist' + +async function doesVideoPlaylistExist (id: number | string, res: express.Response, fetchType: 'summary' | 'all' = 'summary') { + const videoPlaylist = fetchType === 'summary' + ? await VideoPlaylistModel.loadWithAccountAndChannelSummary(id, undefined) + : await VideoPlaylistModel.loadWithAccountAndChannel(id, undefined) + + if (!videoPlaylist) { + res.status(404) + .json({ error: 'Video playlist not found' }) + .end() + + return false + } + + res.locals.videoPlaylist = videoPlaylist + return true +} + +// --------------------------------------------------------------------------- + +export { + doesVideoPlaylistExist +} diff --git a/server/helpers/middlewares/videos.ts b/server/helpers/middlewares/videos.ts new file mode 100644 index 000000000..ceb1058ec --- /dev/null +++ b/server/helpers/middlewares/videos.ts @@ -0,0 +1,82 @@ +import { Response } from 'express' +import { fetchVideo, VideoFetchType } from '../video' +import { UserModel } from '../../models/account/user' +import { UserRight } from '../../../shared/models/users' +import { VideoChannelModel } from '../../models/video/video-channel' +import { VideoModel } from '../../models/video/video' + +async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') { + const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined + + const video = await fetchVideo(id, fetchType, userId) + + if (video === null) { + res.status(404) + .json({ error: 'Video not found' }) + .end() + + return false + } + + if (fetchType !== 'none') res.locals.video = video + return true +} + +async function doesVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) { + if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) { + const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId) + if (videoChannel === null) { + res.status(400) + .json({ error: 'Unknown video `video channel` on this instance.' }) + .end() + + return false + } + + res.locals.videoChannel = videoChannel + return true + } + + const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id) + if (videoChannel === null) { + res.status(400) + .json({ error: 'Unknown video `video channel` for this account.' }) + .end() + + return false + } + + res.locals.videoChannel = videoChannel + return true +} + +function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: Response) { + // Retrieve the user who did the request + if (video.isOwned() === false) { + res.status(403) + .json({ error: 'Cannot manage a video of another server.' }) + .end() + return false + } + + // Check if the user can delete the video + // The user can delete it if he has the right + // Or if s/he is the video's account + const account = video.VideoChannel.Account + if (user.hasRight(right) === false && account.userId !== user.id) { + res.status(403) + .json({ error: 'Cannot manage a video of another user.' }) + .end() + return false + } + + return true +} + +// --------------------------------------------------------------------------- + +export { + doesVideoChannelOfAccountExist, + doesVideoExist, + checkUserCanManageVideo +} -- cgit v1.2.3