From 3fd3ab2d34d512b160a5e6084d7609be7b4f4452 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 12 Dec 2017 17:53:50 +0100 Subject: Move models to typescript-sequelize --- server/middlewares/activitypub.ts | 8 ++++---- server/middlewares/sort.ts | 6 ++---- server/middlewares/user-right.ts | 7 +++---- server/middlewares/validators/account.ts | 4 ++-- server/middlewares/validators/activitypub/activity.ts | 3 ++- .../middlewares/validators/activitypub/signature.ts | 4 +++- server/middlewares/validators/follows.ts | 11 +++++------ server/middlewares/validators/oembed.ts | 5 +++-- server/middlewares/validators/sort.ts | 1 - server/middlewares/validators/users.ts | 15 +++++++-------- server/middlewares/validators/utils.ts | 1 - server/middlewares/validators/video-blacklist.ts | 13 +++++++------ server/middlewares/validators/video-channels.ts | 19 +++++++++---------- server/middlewares/validators/videos.ts | 17 +++++++++-------- server/middlewares/validators/webfinger.ts | 6 +++--- 15 files changed, 59 insertions(+), 61 deletions(-) (limited to 'server/middlewares') diff --git a/server/middlewares/activitypub.ts b/server/middlewares/activitypub.ts index c2ad18195..489396447 100644 --- a/server/middlewares/activitypub.ts +++ b/server/middlewares/activitypub.ts @@ -2,16 +2,16 @@ import { eachSeries } from 'async' import { NextFunction, Request, RequestHandler, Response } from 'express' import { ActivityPubSignature } from '../../shared' import { isSignatureVerified, logger } from '../helpers' -import { database as db } from '../initializers' -import { ACCEPT_HEADERS, ACTIVITY_PUB } from '../initializers/constants' -import { fetchRemoteAccount, saveAccountAndServerIfNotExist } from '../lib/activitypub/account' +import { ACCEPT_HEADERS, ACTIVITY_PUB } from '../initializers' +import { fetchRemoteAccount, saveAccountAndServerIfNotExist } from '../lib/activitypub' +import { AccountModel } from '../models/account/account' async function checkSignature (req: Request, res: Response, next: NextFunction) { const signatureObject: ActivityPubSignature = req.body.signature logger.debug('Checking signature of account %s...', signatureObject.creator) - let account = await db.Account.loadByUrl(signatureObject.creator) + let account = await AccountModel.loadByUrl(signatureObject.creator) // We don't have this account in our database, fetch it on remote if (!account) { diff --git a/server/middlewares/sort.ts b/server/middlewares/sort.ts index 7b60920de..5d2a43acc 100644 --- a/server/middlewares/sort.ts +++ b/server/middlewares/sort.ts @@ -1,8 +1,6 @@ -import 'express-validator' import * as express from 'express' - +import 'express-validator' import { SortType } from '../helpers' -import { database } from '../initializers' function setUsersSort (req: express.Request, res: express.Response, next: express.NextFunction) { if (!req.query.sort) req.query.sort = '-createdAt' @@ -57,7 +55,7 @@ function setBlacklistSort (req: express.Request, res: express.Response, next: ex // If we want to sort onto the BlacklistedVideos relation, we won't specify it in the query parameter ... newSort.sortModel = undefined } else { - newSort.sortModel = database.Video + newSort.sortModel = 'Video' } newSort.sortValue = req.query.sort diff --git a/server/middlewares/user-right.ts b/server/middlewares/user-right.ts index bcebe9d7f..5d63ebaf4 100644 --- a/server/middlewares/user-right.ts +++ b/server/middlewares/user-right.ts @@ -1,13 +1,12 @@ -import 'express-validator' import * as express from 'express' - -import { UserInstance } from '../models' +import 'express-validator' import { UserRight } from '../../shared' import { logger } from '../helpers' +import { UserModel } from '../models/account/user' function ensureUserHasRight (userRight: UserRight) { return function (req: express.Request, res: express.Response, next: express.NextFunction) { - const user: UserInstance = res.locals.oauth.token.user + const user = res.locals.oauth.token.user as UserModel if (user.hasRight(userRight) === false) { logger.info('User %s does not have right %s to access to %s.', user.username, UserRight[userRight], req.path) return res.sendStatus(403) diff --git a/server/middlewares/validators/account.ts b/server/middlewares/validators/account.ts index 70f4e4d3b..6951dfc80 100644 --- a/server/middlewares/validators/account.ts +++ b/server/middlewares/validators/account.ts @@ -1,7 +1,7 @@ import * as express from 'express' import { param } from 'express-validator/check' -import { logger, isLocalAccountNameExist } from '../../helpers' -import { isAccountNameValid } from '../../helpers/custom-validators/accounts' +import { logger } from '../../helpers' +import { isAccountNameValid, isLocalAccountNameExist } from '../../helpers/custom-validators/accounts' import { areValidationErrors } from './utils' const localAccountValidator = [ diff --git a/server/middlewares/validators/activitypub/activity.ts b/server/middlewares/validators/activitypub/activity.ts index c63be5979..e0225f30c 100644 --- a/server/middlewares/validators/activitypub/activity.ts +++ b/server/middlewares/validators/activitypub/activity.ts @@ -1,6 +1,7 @@ import * as express from 'express' import { body } from 'express-validator/check' -import { isRootActivityValid, logger } from '../../../helpers' +import { logger } from '../../../helpers' +import { isRootActivityValid } from '../../../helpers/custom-validators/activitypub' import { areValidationErrors } from '../utils' const activityPubValidator = [ diff --git a/server/middlewares/validators/activitypub/signature.ts b/server/middlewares/validators/activitypub/signature.ts index 360685512..d41bb6a8d 100644 --- a/server/middlewares/validators/activitypub/signature.ts +++ b/server/middlewares/validators/activitypub/signature.ts @@ -1,6 +1,8 @@ import * as express from 'express' import { body } from 'express-validator/check' -import { isDateValid, isSignatureCreatorValid, isSignatureTypeValid, isSignatureValueValid, logger } from '../../../helpers' +import { logger } from '../../../helpers' +import { isSignatureCreatorValid, isSignatureTypeValid, isSignatureValueValid } from '../../../helpers/custom-validators/activitypub' +import { isDateValid } from '../../../helpers/custom-validators/misc' import { areValidationErrors } from '../utils' const signatureValidator = [ diff --git a/server/middlewares/validators/follows.ts b/server/middlewares/validators/follows.ts index 605872ecf..10482e5d0 100644 --- a/server/middlewares/validators/follows.ts +++ b/server/middlewares/validators/follows.ts @@ -1,12 +1,11 @@ import * as express from 'express' import { body, param } from 'express-validator/check' -import { isTestInstance } from '../../helpers/core-utils' +import { getServerAccount, isTestInstance, logger } from '../../helpers' +import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' import { isEachUniqueHostValid } from '../../helpers/custom-validators/servers' -import { logger } from '../../helpers/logger' -import { CONFIG, database as db } from '../../initializers' +import { CONFIG } from '../../initializers' +import { AccountFollowModel } from '../../models/account/account-follow' import { areValidationErrors } from './utils' -import { getServerAccount } from '../../helpers/utils' -import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' const followValidator = [ body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'), @@ -38,7 +37,7 @@ const removeFollowingValidator = [ if (areValidationErrors(req, res)) return const serverAccount = await getServerAccount() - const follow = await db.AccountFollow.loadByAccountAndTarget(serverAccount.id, req.params.accountId) + const follow = await AccountFollowModel.loadByAccountAndTarget(serverAccount.id, req.params.accountId) if (!follow) { return res.status(404) diff --git a/server/middlewares/validators/oembed.ts b/server/middlewares/validators/oembed.ts index 31f06dc65..fb7b726e5 100644 --- a/server/middlewares/validators/oembed.ts +++ b/server/middlewares/validators/oembed.ts @@ -1,10 +1,11 @@ import * as express from 'express' import { query } from 'express-validator/check' import { join } from 'path' -import { isIdOrUUIDValid, isTestInstance, logger } from '../../helpers' +import { isTestInstance, logger } from '../../helpers' +import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' +import { isVideoExist } from '../../helpers/custom-validators/videos' import { CONFIG } from '../../initializers' import { areValidationErrors } from './utils' -import { isVideoExist } from '../../helpers/custom-validators/videos' const urlShouldStartWith = CONFIG.WEBSERVER.SCHEME + '://' + join(CONFIG.WEBSERVER.HOST, 'videos', 'watch') + '/' const videoWatchRegex = new RegExp('([^/]+)$') diff --git a/server/middlewares/validators/sort.ts b/server/middlewares/validators/sort.ts index d5822ac81..38184fefa 100644 --- a/server/middlewares/validators/sort.ts +++ b/server/middlewares/validators/sort.ts @@ -1,6 +1,5 @@ import { query } from 'express-validator/check' import * as express from 'express' - import { logger } from '../../helpers' import { SORTABLE_COLUMNS } from '../../initializers' import { areValidationErrors } from './utils' diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index ac7435b7d..920176d07 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -1,18 +1,17 @@ import * as express from 'express' import 'express-validator' import { body, param } from 'express-validator/check' +import { isSignupAllowed, logger } from '../../helpers' +import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' import { - isIdOrUUIDValid, - isSignupAllowed, isUserDisplayNSFWValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid, - isUserVideoQuotaValid, - logger -} from '../../helpers' + isUserVideoQuotaValid +} from '../../helpers/custom-validators/users' import { isVideoExist } from '../../helpers/custom-validators/videos' -import { database as db } from '../../initializers/database' +import { UserModel } from '../../models/account/user' import { areValidationErrors } from './utils' const usersAddValidator = [ @@ -153,7 +152,7 @@ export { // --------------------------------------------------------------------------- async function checkUserIdExist (id: number, res: express.Response) { - const user = await db.User.loadById(id) + const user = await UserModel.loadById(id) if (!user) { res.status(404) @@ -168,7 +167,7 @@ async function checkUserIdExist (id: number, res: express.Response) { } async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) { - const user = await db.User.loadByUsernameOrEmail(username, email) + const user = await UserModel.loadByUsernameOrEmail(username, email) if (user) { res.status(409) diff --git a/server/middlewares/validators/utils.ts b/server/middlewares/validators/utils.ts index ca80acf29..61f76b457 100644 --- a/server/middlewares/validators/utils.ts +++ b/server/middlewares/validators/utils.ts @@ -1,6 +1,5 @@ import * as express from 'express' import { validationResult } from 'express-validator/check' - import { logger } from '../../helpers' function areValidationErrors (req: express.Request, res: express.Response) { diff --git a/server/middlewares/validators/video-blacklist.ts b/server/middlewares/validators/video-blacklist.ts index f1cc04950..98099fe3f 100644 --- a/server/middlewares/validators/video-blacklist.ts +++ b/server/middlewares/validators/video-blacklist.ts @@ -1,9 +1,10 @@ import * as express from 'express' import { param } from 'express-validator/check' -import { isIdOrUUIDValid, logger } from '../../helpers' +import { logger } from '../../helpers' +import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' import { isVideoExist } from '../../helpers/custom-validators/videos' -import { database as db } from '../../initializers/database' -import { VideoInstance } from '../../models/video/video-interface' +import { VideoModel } from '../../models/video/video' +import { VideoBlacklistModel } from '../../models/video/video-blacklist' import { areValidationErrors } from './utils' const videosBlacklistRemoveValidator = [ @@ -42,7 +43,7 @@ export { } // --------------------------------------------------------------------------- -function checkVideoIsBlacklistable (video: VideoInstance, res: express.Response) { +function checkVideoIsBlacklistable (video: VideoModel, res: express.Response) { if (video.isOwned() === true) { res.status(403) .json({ error: 'Cannot blacklist a local video' }) @@ -54,8 +55,8 @@ function checkVideoIsBlacklistable (video: VideoInstance, res: express.Response) return true } -async function checkVideoIsBlacklisted (video: VideoInstance, res: express.Response) { - const blacklistedVideo = await db.BlacklistedVideo.loadByVideoId(video.id) +async function checkVideoIsBlacklisted (video: VideoModel, res: express.Response) { + const blacklistedVideo = await VideoBlacklistModel.loadByVideoId(video.id) if (!blacklistedVideo) { res.status(404) .send('Blacklisted video not found') diff --git a/server/middlewares/validators/video-channels.ts b/server/middlewares/validators/video-channels.ts index 3d31a7e52..660390080 100644 --- a/server/middlewares/validators/video-channels.ts +++ b/server/middlewares/validators/video-channels.ts @@ -1,19 +1,18 @@ import * as express from 'express' import { body, param } from 'express-validator/check' import { UserRight } from '../../../shared' -import { isIdValid } from '../../helpers/custom-validators/misc' +import { logger } from '../../helpers' +import { isAccountIdExist } from '../../helpers/custom-validators/accounts' +import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc' import { isVideoChannelDescriptionValid, isVideoChannelExist, isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels' -import { isIdOrUUIDValid } from '../../helpers/index' -import { logger } from '../../helpers/logger' -import { database as db } from '../../initializers' -import { UserInstance } from '../../models' +import { UserModel } from '../../models/account/user' +import { VideoChannelModel } from '../../models/video/video-channel' +import { VideoChannelShareModel } from '../../models/video/video-channel-share' import { areValidationErrors } from './utils' -import { isAccountIdExist } from '../../helpers/custom-validators/accounts' -import { VideoChannelInstance } from '../../models/video/video-channel-interface' const listVideoAccountChannelsValidator = [ param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'), @@ -109,7 +108,7 @@ const videoChannelsShareValidator = [ if (areValidationErrors(req, res)) return if (!await isVideoChannelExist(req.params.id, res)) return - const share = await db.VideoChannelShare.load(res.locals.video.id, req.params.accountId, undefined) + const share = await VideoChannelShareModel.load(res.locals.video.id, req.params.accountId, undefined) if (!share) { return res.status(404) .end() @@ -134,7 +133,7 @@ export { // --------------------------------------------------------------------------- -function checkUserCanDeleteVideoChannel (user: UserInstance, videoChannel: VideoChannelInstance, res: express.Response) { +function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) { // Retrieve the user who did the request if (videoChannel.isOwned() === false) { res.status(403) @@ -159,7 +158,7 @@ function checkUserCanDeleteVideoChannel (user: UserInstance, videoChannel: Video } async function checkVideoChannelIsNotTheLastOne (res: express.Response) { - const count = await db.VideoChannel.countByAccount(res.locals.oauth.token.User.Account.id) + const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id) if (count <= 1) { res.status(409) diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts index 10625e41d..b52d5f285 100644 --- a/server/middlewares/validators/videos.ts +++ b/server/middlewares/validators/videos.ts @@ -1,6 +1,8 @@ import * as express from 'express' +import 'express-validator' import { body, param, query } from 'express-validator/check' import { UserRight, VideoPrivacy } from '../../../shared' +import { getDurationFromVideoFile, logger } from '../../helpers' import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc' import { isVideoAbuseReasonValid, @@ -16,12 +18,11 @@ import { isVideoRatingTypeValid, isVideoTagsValid } from '../../helpers/custom-validators/videos' -import { getDurationFromVideoFile } from '../../helpers/ffmpeg-utils' -import { logger } from '../../helpers/logger' import { CONSTRAINTS_FIELDS } from '../../initializers' -import { database as db } from '../../initializers/database' -import { UserInstance } from '../../models/account/user-interface' -import { VideoInstance } from '../../models/video/video-interface' +import { UserModel } from '../../models/account/user' +import { VideoModel } from '../../models/video/video' +import { VideoChannelModel } from '../../models/video/video-channel' +import { VideoShareModel } from '../../models/video/video-share' import { authenticate } from '../oauth' import { areValidationErrors } from './utils' @@ -48,7 +49,7 @@ const videosAddValidator = [ const videoFile: Express.Multer.File = req.files['videofile'][0] const user = res.locals.oauth.token.User - const videoChannel = await db.VideoChannel.loadByIdAndAccount(req.body.channelId, user.Account.id) + const videoChannel = await VideoChannelModel.loadByIdAndAccount(req.body.channelId, user.Account.id) if (!videoChannel) { res.status(400) .json({ error: 'Unknown video video channel for this account.' }) @@ -221,7 +222,7 @@ const videosShareValidator = [ if (areValidationErrors(req, res)) return if (!await isVideoExist(req.params.id, res)) return - const share = await db.VideoShare.load(req.params.accountId, res.locals.video.id, undefined) + const share = await VideoShareModel.load(req.params.accountId, res.locals.video.id, undefined) if (!share) { return res.status(404) .end() @@ -249,7 +250,7 @@ export { // --------------------------------------------------------------------------- -function checkUserCanDeleteVideo (user: UserInstance, video: VideoInstance, res: express.Response) { +function checkUserCanDeleteVideo (user: UserModel, video: VideoModel, res: express.Response) { // Retrieve the user who did the request if (video.isOwned() === false) { res.status(403) diff --git a/server/middlewares/validators/webfinger.ts b/server/middlewares/validators/webfinger.ts index 34e62c66d..7903c7400 100644 --- a/server/middlewares/validators/webfinger.ts +++ b/server/middlewares/validators/webfinger.ts @@ -1,8 +1,8 @@ import * as express from 'express' import { query } from 'express-validator/check' +import { logger } from '../../helpers' import { isWebfingerResourceValid } from '../../helpers/custom-validators/webfinger' -import { logger } from '../../helpers/logger' -import { database as db } from '../../initializers' +import { AccountModel } from '../../models/account/account' import { areValidationErrors } from './utils' const webfingerValidator = [ @@ -17,7 +17,7 @@ const webfingerValidator = [ const nameWithHost = req.query.resource.substr(5) const [ name ] = nameWithHost.split('@') - const account = await db.Account.loadLocalByName(name) + const account = await AccountModel.loadLocalByName(name) if (!account) { return res.status(404) .send({ error: 'Account not found' }) -- cgit v1.2.3