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/controllers/api/videos/abuse.ts | 17 +++++----- server/controllers/api/videos/blacklist.ts | 12 +++---- server/controllers/api/videos/channel.ts | 27 +++++++-------- server/controllers/api/videos/index.ts | 53 ++++++++++++++++-------------- server/controllers/api/videos/rate.ts | 20 +++++------ 5 files changed, 67 insertions(+), 62 deletions(-) (limited to 'server/controllers/api/videos') diff --git a/server/controllers/api/videos/abuse.ts b/server/controllers/api/videos/abuse.ts index 29e1175c5..08cc4d0b4 100644 --- a/server/controllers/api/videos/abuse.ts +++ b/server/controllers/api/videos/abuse.ts @@ -1,11 +1,10 @@ import * as express from 'express' - -import { database as db } from '../../../initializers/database' import { logger, getFormattedObjects, retryTransactionWrapper } from '../../../helpers' +import { sequelizeTypescript } from '../../../initializers' import { authenticate, ensureUserHasRight, @@ -16,9 +15,11 @@ import { setPagination, asyncMiddleware } from '../../../middlewares' -import { VideoInstance } from '../../../models' import { VideoAbuseCreate, UserRight } from '../../../../shared' import { sendVideoAbuse } from '../../../lib/index' +import { AccountModel } from '../../../models/account/account' +import { VideoModel } from '../../../models/video/video' +import { VideoAbuseModel } from '../../../models/video/video-abuse' const abuseVideoRouter = express.Router() @@ -46,7 +47,7 @@ export { // --------------------------------------------------------------------------- async function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) { - const resultList = await db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort) + const resultList = await VideoAbuseModel.listForApi(req.query.start, req.query.count, req.query.sort) return res.json(getFormattedObjects(resultList.data, resultList.total)) } @@ -63,8 +64,8 @@ async function reportVideoAbuseRetryWrapper (req: express.Request, res: express. } async function reportVideoAbuse (req: express.Request, res: express.Response) { - const videoInstance = res.locals.video as VideoInstance - const reporterAccount = res.locals.oauth.token.User.Account + const videoInstance = res.locals.video as VideoModel + const reporterAccount = res.locals.oauth.token.User.Account as AccountModel const body: VideoAbuseCreate = req.body const abuseToCreate = { @@ -73,8 +74,8 @@ async function reportVideoAbuse (req: express.Request, res: express.Response) { videoId: videoInstance.id } - await db.sequelize.transaction(async t => { - const videoAbuseInstance = await db.VideoAbuse.create(abuseToCreate, { transaction: t }) + await sequelizeTypescript.transaction(async t => { + const videoAbuseInstance = await VideoAbuseModel.create(abuseToCreate, { transaction: t }) videoAbuseInstance.Video = videoInstance // We send the video abuse to the origin server diff --git a/server/controllers/api/videos/blacklist.ts b/server/controllers/api/videos/blacklist.ts index 06333c271..d08c6e13f 100644 --- a/server/controllers/api/videos/blacklist.ts +++ b/server/controllers/api/videos/blacklist.ts @@ -1,6 +1,4 @@ import * as express from 'express' - -import { database as db } from '../../../initializers' import { logger, getFormattedObjects } from '../../../helpers' import { authenticate, @@ -13,8 +11,8 @@ import { setPagination, asyncMiddleware } from '../../../middlewares' -import { BlacklistedVideoInstance } from '../../../models' import { BlacklistedVideo, UserRight } from '../../../../shared' +import { VideoBlacklistModel } from '../../../models/video/video-blacklist' const blacklistRouter = express.Router() @@ -57,18 +55,18 @@ async function addVideoToBlacklist (req: express.Request, res: express.Response, videoId: videoInstance.id } - await db.BlacklistedVideo.create(toCreate) + await VideoBlacklistModel.create(toCreate) return res.type('json').status(204).end() } async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { - const resultList = await db.BlacklistedVideo.listForApi(req.query.start, req.query.count, req.query.sort) + const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort) - return res.json(getFormattedObjects(resultList.data, resultList.total)) + return res.json(getFormattedObjects(resultList.data, resultList.total)) } async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) { - const blacklistedVideo = res.locals.blacklistedVideo as BlacklistedVideoInstance + const blacklistedVideo = res.locals.blacklistedVideo as VideoBlacklistModel try { await blacklistedVideo.destroy() diff --git a/server/controllers/api/videos/channel.ts b/server/controllers/api/videos/channel.ts index d99f47c32..683b0448d 100644 --- a/server/controllers/api/videos/channel.ts +++ b/server/controllers/api/videos/channel.ts @@ -1,7 +1,7 @@ import * as express from 'express' import { VideoChannelCreate, VideoChannelUpdate } from '../../../../shared' import { getFormattedObjects, logger, resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers' -import { database as db } from '../../../initializers' +import { sequelizeTypescript } from '../../../initializers' import { createVideoChannel } from '../../../lib' import { sendUpdateVideoChannel } from '../../../lib/activitypub/send/send-update' import { @@ -17,7 +17,8 @@ import { videoChannelsSortValidator, videoChannelsUpdateValidator } from '../../../middlewares' -import { AccountInstance, VideoChannelInstance } from '../../../models' +import { AccountModel } from '../../../models/account/account' +import { VideoChannelModel } from '../../../models/video/video-channel' const videoChannelRouter = express.Router() @@ -66,13 +67,13 @@ export { // --------------------------------------------------------------------------- async function listVideoChannels (req: express.Request, res: express.Response, next: express.NextFunction) { - const resultList = await db.VideoChannel.listForApi(req.query.start, req.query.count, req.query.sort) + const resultList = await VideoChannelModel.listForApi(req.query.start, req.query.count, req.query.sort) return res.json(getFormattedObjects(resultList.data, resultList.total)) } async function listVideoAccountChannels (req: express.Request, res: express.Response, next: express.NextFunction) { - const resultList = await db.VideoChannel.listByAccount(res.locals.account.id) + const resultList = await VideoChannelModel.listByAccount(res.locals.account.id) return res.json(getFormattedObjects(resultList.data, resultList.total)) } @@ -93,10 +94,10 @@ async function addVideoChannelRetryWrapper (req: express.Request, res: express.R async function addVideoChannel (req: express.Request, res: express.Response) { const videoChannelInfo: VideoChannelCreate = req.body - const account: AccountInstance = res.locals.oauth.token.User.Account - let videoChannelCreated: VideoChannelInstance + const account: AccountModel = res.locals.oauth.token.User.Account + let videoChannelCreated: VideoChannelModel - await db.sequelize.transaction(async t => { + await sequelizeTypescript.transaction(async t => { videoChannelCreated = await createVideoChannel(videoChannelInfo, account, t) }) @@ -115,12 +116,12 @@ async function updateVideoChannelRetryWrapper (req: express.Request, res: expres } async function updateVideoChannel (req: express.Request, res: express.Response) { - const videoChannelInstance: VideoChannelInstance = res.locals.videoChannel + const videoChannelInstance = res.locals.videoChannel as VideoChannelModel const videoChannelFieldsSave = videoChannelInstance.toJSON() - const videoChannelInfoToUpdate: VideoChannelUpdate = req.body + const videoChannelInfoToUpdate = req.body as VideoChannelUpdate try { - await db.sequelize.transaction(async t => { + await sequelizeTypescript.transaction(async t => { const sequelizeOptions = { transaction: t } @@ -158,9 +159,9 @@ async function removeVideoChannelRetryWrapper (req: express.Request, res: expres } async function removeVideoChannel (req: express.Request, res: express.Response) { - const videoChannelInstance: VideoChannelInstance = res.locals.videoChannel + const videoChannelInstance: VideoChannelModel = res.locals.videoChannel - await db.sequelize.transaction(async t => { + await sequelizeTypescript.transaction(async t => { await videoChannelInstance.destroy({ transaction: t }) }) @@ -168,7 +169,7 @@ async function removeVideoChannel (req: express.Request, res: express.Response) } async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) { - const videoChannelWithVideos = await db.VideoChannel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id) + const videoChannelWithVideos = await VideoChannelModel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id) return res.json(videoChannelWithVideos.toFormattedJSON()) } diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 63de662a7..91ab8c66a 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts @@ -12,16 +12,19 @@ import { retryTransactionWrapper } from '../../../helpers' import { getServerAccount } from '../../../helpers/utils' -import { CONFIG, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_MIMETYPE_EXT, VIDEO_PRIVACIES } from '../../../initializers' -import { database as db } from '../../../initializers/database' -import { sendAddVideo } from '../../../lib/activitypub/send/send-add' -import { sendCreateViewToOrigin } from '../../../lib/activitypub/send/send-create' -import { sendUpdateVideo } from '../../../lib/activitypub/send/send-update' -import { shareVideoByServer } from '../../../lib/activitypub/share' -import { getVideoActivityPubUrl } from '../../../lib/activitypub/url' -import { fetchRemoteVideoDescription } from '../../../lib/activitypub/videos' +import { + CONFIG, + sequelizeTypescript, + VIDEO_CATEGORIES, + VIDEO_LANGUAGES, + VIDEO_LICENCES, + VIDEO_MIMETYPE_EXT, + VIDEO_PRIVACIES +} from '../../../initializers' +import { fetchRemoteVideoDescription, getVideoActivityPubUrl, shareVideoByServer } from '../../../lib/activitypub' +import { sendAddVideo, sendCreateViewToOrigin, sendUpdateVideo } from '../../../lib/activitypub/send' import { sendCreateViewToVideoFollowers } from '../../../lib/index' -import { transcodingJobScheduler } from '../../../lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler' +import { transcodingJobScheduler } from '../../../lib/jobs/transcoding-job-scheduler' import { asyncMiddleware, authenticate, @@ -35,7 +38,9 @@ import { videosSortValidator, videosUpdateValidator } from '../../../middlewares' -import { VideoInstance } from '../../../models' +import { TagModel } from '../../../models/video/tag' +import { VideoModel } from '../../../models/video/video' +import { VideoFileModel } from '../../../models/video/video-file' import { abuseVideoRouter } from './abuse' import { blacklistRouter } from './blacklist' import { videoChannelRouter } from './channel' @@ -99,7 +104,7 @@ videosRouter.put('/:id', videosRouter.post('/upload', authenticate, reqFiles, - videosAddValidator, + asyncMiddleware(videosAddValidator), asyncMiddleware(addVideoRetryWrapper) ) @@ -181,7 +186,7 @@ async function addVideo (req: express.Request, res: express.Response, videoPhysi duration: videoPhysicalFile['duration'], // duration was added by a previous middleware channelId: res.locals.videoChannel.id } - const video = db.Video.build(videoData) + const video = new VideoModel(videoData) video.url = getVideoActivityPubUrl(video) const videoFilePath = join(CONFIG.STORAGE.VIDEOS_DIR, videoPhysicalFile.filename) @@ -192,7 +197,7 @@ async function addVideo (req: express.Request, res: express.Response, videoPhysi resolution: videoFileHeight, size: videoPhysicalFile.size } - const videoFile = db.VideoFile.build(videoFileData) + const videoFile = new VideoFileModel(videoFileData) const videoDir = CONFIG.STORAGE.VIDEOS_DIR const source = join(videoDir, videoPhysicalFile.filename) const destination = join(videoDir, video.getVideoFilename(videoFile)) @@ -210,7 +215,7 @@ async function addVideo (req: express.Request, res: express.Response, videoPhysi ) await Promise.all(tasks) - return db.sequelize.transaction(async t => { + return sequelizeTypescript.transaction(async t => { const sequelizeOptions = { transaction: t } if (CONFIG.TRANSCODING.ENABLED === true) { @@ -232,9 +237,9 @@ async function addVideo (req: express.Request, res: express.Response, videoPhysi video.VideoFiles = [ videoFile ] if (videoInfo.tags) { - const tagInstances = await db.Tag.findOrCreateTags(videoInfo.tags, t) + const tagInstances = await TagModel.findOrCreateTags(videoInfo.tags, t) - await video.setTags(tagInstances, sequelizeOptions) + await video.$set('Tags', tagInstances, sequelizeOptions) video.Tags = tagInstances } @@ -264,13 +269,13 @@ async function updateVideoRetryWrapper (req: express.Request, res: express.Respo } async function updateVideo (req: express.Request, res: express.Response) { - const videoInstance: VideoInstance = res.locals.video + const videoInstance: VideoModel = res.locals.video const videoFieldsSave = videoInstance.toJSON() const videoInfoToUpdate: VideoUpdate = req.body const wasPrivateVideo = videoInstance.privacy === VideoPrivacy.PRIVATE try { - await db.sequelize.transaction(async t => { + await sequelizeTypescript.transaction(async t => { const sequelizeOptions = { transaction: t } @@ -286,9 +291,9 @@ async function updateVideo (req: express.Request, res: express.Response) { const videoInstanceUpdated = await videoInstance.save(sequelizeOptions) if (videoInfoToUpdate.tags) { - const tagInstances = await db.Tag.findOrCreateTags(videoInfoToUpdate.tags, t) + const tagInstances = await TagModel.findOrCreateTags(videoInfoToUpdate.tags, t) - await videoInstance.setTags(tagInstances, sequelizeOptions) + await videoInstance.$set('Tags', tagInstances, sequelizeOptions) videoInstance.Tags = tagInstances } @@ -350,7 +355,7 @@ async function getVideoDescription (req: express.Request, res: express.Response) } async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) { - const resultList = await db.Video.listForApi(req.query.start, req.query.count, req.query.sort) + const resultList = await VideoModel.listForApi(req.query.start, req.query.count, req.query.sort) return res.json(getFormattedObjects(resultList.data, resultList.total)) } @@ -367,9 +372,9 @@ async function removeVideoRetryWrapper (req: express.Request, res: express.Respo } async function removeVideo (req: express.Request, res: express.Response) { - const videoInstance: VideoInstance = res.locals.video + const videoInstance: VideoModel = res.locals.video - await db.sequelize.transaction(async t => { + await sequelizeTypescript.transaction(async t => { await videoInstance.destroy({ transaction: t }) }) @@ -377,7 +382,7 @@ async function removeVideo (req: express.Request, res: express.Response) { } async function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) { - const resultList = await db.Video.searchAndPopulateAccountAndServerAndTags( + const resultList = await VideoModel.searchAndPopulateAccountAndServerAndTags( req.query.search, req.query.start, req.query.count, diff --git a/server/controllers/api/videos/rate.ts b/server/controllers/api/videos/rate.ts index c27c61116..48b744b0c 100644 --- a/server/controllers/api/videos/rate.ts +++ b/server/controllers/api/videos/rate.ts @@ -1,12 +1,12 @@ import * as express from 'express' import { UserVideoRateUpdate } from '../../../../shared' import { logger, retryTransactionWrapper } from '../../../helpers' -import { VIDEO_RATE_TYPES } from '../../../initializers' -import { database as db } from '../../../initializers/database' -import { sendVideoRateChangeToFollowers, sendVideoRateChangeToOrigin } from '../../../lib/activitypub/videos' +import { sequelizeTypescript, VIDEO_RATE_TYPES } from '../../../initializers' +import { sendVideoRateChangeToFollowers, sendVideoRateChangeToOrigin } from '../../../lib/activitypub' import { asyncMiddleware, authenticate, videoRateValidator } from '../../../middlewares' -import { AccountInstance } from '../../../models/account/account-interface' -import { VideoInstance } from '../../../models/video/video-interface' +import { AccountModel } from '../../../models/account/account' +import { AccountVideoRateModel } from '../../../models/account/account-video-rate' +import { VideoModel } from '../../../models/video/video' const rateVideoRouter = express.Router() @@ -38,12 +38,12 @@ async function rateVideoRetryWrapper (req: express.Request, res: express.Respons async function rateVideo (req: express.Request, res: express.Response) { const body: UserVideoRateUpdate = req.body const rateType = body.rating - const videoInstance: VideoInstance = res.locals.video - const accountInstance: AccountInstance = res.locals.oauth.token.User.Account + const videoInstance: VideoModel = res.locals.video + const accountInstance: AccountModel = res.locals.oauth.token.User.Account - await db.sequelize.transaction(async t => { + await sequelizeTypescript.transaction(async t => { const sequelizeOptions = { transaction: t } - const previousRate = await db.AccountVideoRate.load(accountInstance.id, videoInstance.id, t) + const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t) let likesToIncrement = 0 let dislikesToIncrement = 0 @@ -71,7 +71,7 @@ async function rateVideo (req: express.Request, res: express.Response) { type: rateType } - await db.AccountVideoRate.create(query, sequelizeOptions) + await AccountVideoRateModel.create(query, sequelizeOptions) } const incrementQuery = { -- cgit v1.2.3