From dae86118ed5d4026d04acb9d0e36829b9ad8eb4e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 19 Mar 2019 10:35:15 +0100 Subject: Cleanup express locals typings --- server/middlewares/validators/activitypub/activity.ts | 3 +-- server/middlewares/validators/blocklist.ts | 6 +++--- server/middlewares/validators/redundancy.ts | 10 +++------- server/middlewares/validators/user-subscriptions.ts | 3 +-- server/middlewares/validators/users.ts | 12 ++++++------ server/middlewares/validators/videos/video-blacklist.ts | 3 +-- server/middlewares/validators/videos/video-playlists.ts | 17 ++++++++--------- server/middlewares/validators/videos/video-shares.ts | 3 +-- server/middlewares/validators/videos/video-watch.ts | 3 +-- server/middlewares/validators/videos/videos.ts | 12 ++++++------ 10 files changed, 31 insertions(+), 41 deletions(-) (limited to 'server/middlewares/validators') diff --git a/server/middlewares/validators/activitypub/activity.ts b/server/middlewares/validators/activitypub/activity.ts index 3f9057c0c..7582f65e7 100644 --- a/server/middlewares/validators/activitypub/activity.ts +++ b/server/middlewares/validators/activitypub/activity.ts @@ -2,7 +2,6 @@ import * as express from 'express' import { isRootActivityValid } from '../../../helpers/custom-validators/activitypub/activity' import { logger } from '../../../helpers/logger' import { getServerActor } from '../../../helpers/utils' -import { ActorModel } from '../../../models/activitypub/actor' async function activityPubValidator (req: express.Request, res: express.Response, next: express.NextFunction) { logger.debug('Checking activity pub parameters') @@ -13,7 +12,7 @@ async function activityPubValidator (req: express.Request, res: express.Response } const serverActor = await getServerActor() - const remoteActor = res.locals.signature.actor as ActorModel + const remoteActor = res.locals.signature.actor if (serverActor.id === remoteActor.id) { logger.error('Receiving request in INBOX by ourselves!', req.body) return res.status(409).end() diff --git a/server/middlewares/validators/blocklist.ts b/server/middlewares/validators/blocklist.ts index d7ec649b6..f5b295f45 100644 --- a/server/middlewares/validators/blocklist.ts +++ b/server/middlewares/validators/blocklist.ts @@ -20,7 +20,7 @@ const blockAccountValidator = [ if (areValidationErrors(req, res)) return if (!await doesAccountNameWithHostExist(req.body.accountName, res)) return - const user = res.locals.oauth.token.User as UserModel + const user = res.locals.oauth.token.User const accountToBlock = res.locals.account if (user.Account.id === accountToBlock.id) { @@ -44,7 +44,7 @@ const unblockAccountByAccountValidator = [ if (areValidationErrors(req, res)) return if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return - const user = res.locals.oauth.token.User as UserModel + const user = res.locals.oauth.token.User const targetAccount = res.locals.account if (!await doesUnblockAccountExist(user.Account.id, targetAccount.id, res)) return @@ -106,7 +106,7 @@ const unblockServerByAccountValidator = [ if (areValidationErrors(req, res)) return - const user = res.locals.oauth.token.User as UserModel + const user = res.locals.oauth.token.User if (!await doesUnblockServerExist(user.Account.id, req.params.host, res)) return return next() diff --git a/server/middlewares/validators/redundancy.ts b/server/middlewares/validators/redundancy.ts index 419921928..76cf89c40 100644 --- a/server/middlewares/validators/redundancy.ts +++ b/server/middlewares/validators/redundancy.ts @@ -1,16 +1,12 @@ import * as express from 'express' import 'express-validator' -import { param, body } from 'express-validator/check' +import { body, param } from 'express-validator/check' import { exists, isBooleanValid, isIdOrUUIDValid, toIntOrNull } from '../../helpers/custom-validators/misc' import { doesVideoExist } from '../../helpers/custom-validators/videos' import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' -import { VideoModel } from '../../models/video/video' import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' import { isHostValid } from '../../helpers/custom-validators/servers' -import { getServerActor } from '../../helpers/utils' -import { ActorFollowModel } from '../../models/activitypub/actor-follow' -import { SERVER_ACTOR_NAME } from '../../initializers' import { ServerModel } from '../../models/server/server' const videoFileRedundancyGetValidator = [ @@ -29,7 +25,7 @@ const videoFileRedundancyGetValidator = [ if (areValidationErrors(req, res)) return if (!await doesVideoExist(req.params.videoId, res)) return - const video: VideoModel = res.locals.video + const video = res.locals.video const videoFile = video.VideoFiles.find(f => { return f.resolution === req.params.resolution && (!req.params.fps || f.fps === req.params.fps) }) @@ -55,7 +51,7 @@ const videoPlaylistRedundancyGetValidator = [ if (areValidationErrors(req, res)) return if (!await doesVideoExist(req.params.videoId, res)) return - const video: VideoModel = res.locals.video + const video = res.locals.video const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p === req.params.streamingPlaylistType) if (!videoStreamingPlaylist) return res.status(404).json({ error: 'Video playlist not found.' }) diff --git a/server/middlewares/validators/user-subscriptions.ts b/server/middlewares/validators/user-subscriptions.ts index c5f8d9d4c..fd82ed017 100644 --- a/server/middlewares/validators/user-subscriptions.ts +++ b/server/middlewares/validators/user-subscriptions.ts @@ -5,7 +5,6 @@ import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' import { ActorFollowModel } from '../../models/activitypub/actor-follow' import { areValidActorHandles, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' -import { UserModel } from '../../models/account/user' import { CONFIG } from '../../initializers' import { toArray } from '../../helpers/custom-validators/misc' @@ -46,7 +45,7 @@ const userSubscriptionGetValidator = [ let [ name, host ] = req.params.uri.split('@') if (host === CONFIG.WEBSERVER.HOST) host = null - const user: UserModel = res.locals.oauth.token.User + const user = res.locals.oauth.token.User const subscription = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(user.Account.Actor.id, name, host) if (!subscription || !subscription.ActorFollowing.VideoChannel) { diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index 5e8c8c29b..e8ade0f97 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -14,7 +14,8 @@ import { isUserRoleValid, isUserUsernameValid, isUserVideoQuotaDailyValid, - isUserVideoQuotaValid, isUserVideosHistoryEnabledValid + isUserVideoQuotaValid, + isUserVideosHistoryEnabledValid } from '../../helpers/custom-validators/users' import { doesVideoExist } from '../../helpers/custom-validators/videos' import { logger } from '../../helpers/logger' @@ -100,7 +101,7 @@ const usersBlockingValidator = [ const deleteMeValidator = [ async (req: express.Request, res: express.Response, next: express.NextFunction) => { - const user: UserModel = res.locals.oauth.token.User + const user = res.locals.oauth.token.User if (user.username === 'root') { return res.status(400) .send({ error: 'You cannot delete your root account.' }) @@ -159,8 +160,7 @@ const usersUpdateMeValidator = [ .end() } - const user: UserModel = res.locals.oauth.token.User - + const user= res.locals.oauth.token.User if (await user.isPasswordMatch(req.body.currentPassword) !== true) { return res.status(401) .send({ error: 'currentPassword is invalid.' }) @@ -257,7 +257,7 @@ const usersResetPasswordValidator = [ if (areValidationErrors(req, res)) return if (!await checkUserIdExist(req.params.id, res)) return - const user = res.locals.user as UserModel + const user = res.locals.user const redisVerificationString = await Redis.Instance.getResetPasswordLink(user.id) if (redisVerificationString !== req.body.verificationString) { @@ -299,7 +299,7 @@ const usersVerifyEmailValidator = [ if (areValidationErrors(req, res)) return if (!await checkUserIdExist(req.params.id, res)) return - const user = res.locals.user as UserModel + const user = res.locals.user const redisVerificationString = await Redis.Instance.getVerifyEmailLink(user.id) if (redisVerificationString !== req.body.verificationString) { diff --git a/server/middlewares/validators/videos/video-blacklist.ts b/server/middlewares/validators/videos/video-blacklist.ts index 77ad29cbb..db318dcdb 100644 --- a/server/middlewares/validators/videos/video-blacklist.ts +++ b/server/middlewares/validators/videos/video-blacklist.ts @@ -5,7 +5,6 @@ import { doesVideoExist } from '../../../helpers/custom-validators/videos' import { logger } from '../../../helpers/logger' import { areValidationErrors } from '../utils' import { doesVideoBlacklistExist, isVideoBlacklistReasonValid } from '../../../helpers/custom-validators/video-blacklist' -import { VideoModel } from '../../../models/video/video' const videosBlacklistRemoveValidator = [ param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), @@ -37,7 +36,7 @@ const videosBlacklistAddValidator = [ if (areValidationErrors(req, res)) return if (!await doesVideoExist(req.params.videoId, res)) return - const video: VideoModel = res.locals.video + const video = res.locals.video if (req.body.unfederate === true && video.remote === true) { return res .status(409) diff --git a/server/middlewares/validators/videos/video-playlists.ts b/server/middlewares/validators/videos/video-playlists.ts index 4bc79f433..55e09e354 100644 --- a/server/middlewares/validators/videos/video-playlists.ts +++ b/server/middlewares/validators/videos/video-playlists.ts @@ -103,7 +103,7 @@ const videoPlaylistsDeleteValidator = [ if (!await doesVideoPlaylistExist(req.params.playlistId, res)) return - const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist + const videoPlaylist = res.locals.videoPlaylist if (videoPlaylist.type === VideoPlaylistType.WATCH_LATER) { return res.status(400) .json({ error: 'Cannot delete a watch later playlist.' }) @@ -128,7 +128,7 @@ const videoPlaylistsGetValidator = [ if (!await doesVideoPlaylistExist(req.params.playlistId, res)) return - const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist + const videoPlaylist = res.locals.videoPlaylist // Video is unlisted, check we used the uuid to fetch it if (videoPlaylist.privacy === VideoPlaylistPrivacy.UNLISTED) { @@ -140,8 +140,7 @@ const videoPlaylistsGetValidator = [ if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) { await authenticatePromiseIfNeeded(req, res) - const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : null - + const user = res.locals.oauth ? res.locals.oauth.token.User : null if ( !user || (videoPlaylist.OwnerAccount.userId !== user.id && !user.hasRight(UserRight.UPDATE_ANY_VIDEO_PLAYLIST)) @@ -177,8 +176,8 @@ const videoPlaylistsAddVideoValidator = [ if (!await doesVideoPlaylistExist(req.params.playlistId, res, 'all')) return if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return - const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist - const video: VideoModel = res.locals.video + const videoPlaylist = res.locals.videoPlaylist + const video = res.locals.video const videoPlaylistElement = await VideoPlaylistElementModel.loadByPlaylistAndVideo(videoPlaylist.id, video.id) if (videoPlaylistElement) { @@ -217,8 +216,8 @@ const videoPlaylistsUpdateOrRemoveVideoValidator = [ if (!await doesVideoPlaylistExist(req.params.playlistId, res, 'all')) return if (!await doesVideoExist(req.params.videoId, res, 'id')) return - const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist - const video: VideoModel = res.locals.video + const videoPlaylist = res.locals.videoPlaylist + const video = res.locals.video const videoPlaylistElement = await VideoPlaylistElementModel.loadByPlaylistAndVideo(videoPlaylist.id, video.id) if (!videoPlaylistElement) { @@ -284,7 +283,7 @@ const videoPlaylistsReorderVideosValidator = [ if (!await doesVideoPlaylistExist(req.params.playlistId, res, 'all')) return - const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist + const videoPlaylist = res.locals.videoPlaylist if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) return const nextPosition = await VideoPlaylistElementModel.getNextPositionOf(videoPlaylist.id) diff --git a/server/middlewares/validators/videos/video-shares.ts b/server/middlewares/validators/videos/video-shares.ts index f4514f85c..d5cbdb03e 100644 --- a/server/middlewares/validators/videos/video-shares.ts +++ b/server/middlewares/validators/videos/video-shares.ts @@ -6,7 +6,6 @@ import { doesVideoExist } from '../../../helpers/custom-validators/videos' import { logger } from '../../../helpers/logger' import { VideoShareModel } from '../../../models/video/video-share' import { areValidationErrors } from '../utils' -import { VideoModel } from '../../../models/video/video' const videosShareValidator = [ param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), @@ -18,7 +17,7 @@ const videosShareValidator = [ if (areValidationErrors(req, res)) return if (!await doesVideoExist(req.params.id, res)) return - const video: VideoModel = res.locals.video + const video = res.locals.video const share = await VideoShareModel.load(req.params.actorId, video.id) if (!share) { diff --git a/server/middlewares/validators/videos/video-watch.ts b/server/middlewares/validators/videos/video-watch.ts index a3b70c0cc..a3a800d14 100644 --- a/server/middlewares/validators/videos/video-watch.ts +++ b/server/middlewares/validators/videos/video-watch.ts @@ -4,7 +4,6 @@ import { isIdOrUUIDValid } from '../../../helpers/custom-validators/misc' import { doesVideoExist } from '../../../helpers/custom-validators/videos' import { areValidationErrors } from '../utils' import { logger } from '../../../helpers/logger' -import { UserModel } from '../../../models/account/user' const videoWatchingValidator = [ param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), @@ -18,7 +17,7 @@ const videoWatchingValidator = [ if (areValidationErrors(req, res)) return if (!await doesVideoExist(req.params.videoId, res, 'id')) return - const user = res.locals.oauth.token.User as UserModel + const user = res.locals.oauth.token.User if (user.videosHistoryEnabled === false) { logger.warn('Cannot set videos to watch by user %d: videos history is disabled.', user.id) return res.status(409).end() diff --git a/server/middlewares/validators/videos/videos.ts b/server/middlewares/validators/videos/videos.ts index 92218d4b1..b70abf429 100644 --- a/server/middlewares/validators/videos/videos.ts +++ b/server/middlewares/validators/videos/videos.ts @@ -130,7 +130,7 @@ const videosUpdateValidator = getCommonVideoEditAttributes().concat([ ]) async function checkVideoFollowConstraints (req: express.Request, res: express.Response, next: express.NextFunction) { - const video: VideoModel = res.locals.video + const video = res.locals.video // Anybody can watch local videos if (video.isOwned() === true) return next() @@ -164,13 +164,13 @@ const videosCustomGetValidator = (fetchType: VideoFetchType) => { if (areValidationErrors(req, res)) return if (!await doesVideoExist(req.params.id, res, fetchType)) return - const video: VideoModel = res.locals.video + const video = res.locals.video // Video private or blacklisted if (video.privacy === VideoPrivacy.PRIVATE || video.VideoBlacklist) { await authenticatePromiseIfNeeded(req, res) - const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : null + const user = res.locals.oauth ? res.locals.oauth.token.User : null // Only the owner or a user that have blacklist rights can see the video if ( @@ -256,7 +256,7 @@ const videosTerminateChangeOwnershipValidator = [ return next() }, async (req: express.Request, res: express.Response, next: express.NextFunction) => { - const videoChangeOwnership = res.locals.videoChangeOwnership as VideoChangeOwnershipModel + const videoChangeOwnership = res.locals.videoChangeOwnership if (videoChangeOwnership.status === VideoChangeOwnershipStatus.WAITING) { return next() @@ -275,7 +275,7 @@ const videosAcceptChangeOwnershipValidator = [ if (!await doesVideoChannelOfAccountExist(body.channelId, res.locals.oauth.token.User, res)) return const user = res.locals.oauth.token.User - const videoChangeOwnership = res.locals.videoChangeOwnership as VideoChangeOwnershipModel + const videoChangeOwnership = res.locals.videoChangeOwnership const isAble = await user.isAbleToUploadVideo(videoChangeOwnership.Video.getOriginalFile()) if (isAble === false) { res.status(403) @@ -395,7 +395,7 @@ const commonVideosFiltersValidator = [ if (areValidationErrors(req, res)) return - const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : undefined + const user = res.locals.oauth ? res.locals.oauth.token.User : undefined if (req.query.filter === 'all-local' && (!user || user.hasRight(UserRight.SEE_ALL_VIDEOS) === false)) { res.status(401) .json({ error: 'You are not allowed to see all local videos.' }) -- cgit v1.2.3