Cleanup express locals typings

This commit is contained in:
Chocobozzz
2019-03-19 10:35:15 +01:00
parent e65c0c5b1f
commit dae86118ed
45 changed files with 270 additions and 234 deletions

View File

@@ -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()

View File

@@ -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()

View File

@@ -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.' })

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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)

View File

@@ -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)

View File

@@ -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) {

View File

@@ -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()

View File

@@ -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.' })