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/jobs.ts | 14 ++++---- server/controllers/api/oauth-clients.ts | 4 +-- server/controllers/api/server/follows.ts | 54 +++++++++++++++--------------- server/controllers/api/users.ts | 28 +++++++++------- 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 +++++------ 9 files changed, 117 insertions(+), 112 deletions(-) (limited to 'server/controllers/api') diff --git a/server/controllers/api/jobs.ts b/server/controllers/api/jobs.ts index f6fbff369..4e7cd1ee3 100644 --- a/server/controllers/api/jobs.ts +++ b/server/controllers/api/jobs.ts @@ -1,11 +1,9 @@ import * as express from 'express' -import { asyncMiddleware, jobsSortValidator, setJobsSort, setPagination } from '../../middlewares' -import { paginationValidator } from '../../middlewares/validators/pagination' -import { database as db } from '../../initializers' -import { getFormattedObjects } from '../../helpers/utils' -import { authenticate } from '../../middlewares/oauth' -import { ensureUserHasRight } from '../../middlewares/user-right' -import { UserRight } from '../../../shared/models/users/user-right.enum' +import { UserRight } from '../../../shared/models/users' +import { getFormattedObjects } from '../../helpers' +import { asyncMiddleware, authenticate, ensureUserHasRight, jobsSortValidator, setJobsSort, setPagination } from '../../middlewares' +import { paginationValidator } from '../../middlewares/validators' +import { JobModel } from '../../models/job/job' const jobsRouter = express.Router() @@ -28,7 +26,7 @@ export { // --------------------------------------------------------------------------- async function listJobs (req: express.Request, res: express.Response, next: express.NextFunction) { - const resultList = await db.Job.listForApi(req.query.start, req.query.count, req.query.sort) + const resultList = await JobModel.listForApi(req.query.start, req.query.count, req.query.sort) return res.json(getFormattedObjects(resultList.data, resultList.total)) } diff --git a/server/controllers/api/oauth-clients.ts b/server/controllers/api/oauth-clients.ts index ac1ee9e36..bc02fce90 100644 --- a/server/controllers/api/oauth-clients.ts +++ b/server/controllers/api/oauth-clients.ts @@ -3,8 +3,8 @@ import * as express from 'express' import { CONFIG } from '../../initializers' import { logger } from '../../helpers' import { asyncMiddleware } from '../../middlewares' -import { database as db } from '../../initializers/database' import { OAuthClientLocal } from '../../../shared' +import { OAuthClientModel } from '../../models/oauth/oauth-client' const oauthClientsRouter = express.Router() @@ -27,7 +27,7 @@ async function getLocalClient (req: express.Request, res: express.Response, next return res.type('json').status(403).end() } - const client = await db.OAuthClient.loadFirstClient() + const client = await OAuthClientModel.loadFirstClient() if (!client) throw new Error('No client available.') const json: OAuthClientLocal = { diff --git a/server/controllers/api/server/follows.ts b/server/controllers/api/server/follows.ts index c2fb37c39..913998e3a 100644 --- a/server/controllers/api/server/follows.ts +++ b/server/controllers/api/server/follows.ts @@ -1,24 +1,24 @@ import * as express from 'express' -import { UserRight } from '../../../../shared/models/users/user-right.enum' -import { getFormattedObjects } from '../../../helpers' -import { retryTransactionWrapper } from '../../../helpers/database-utils' -import { logger } from '../../../helpers/logger' -import { getServerAccount } from '../../../helpers/utils' -import { getAccountFromWebfinger } from '../../../helpers/webfinger' -import { SERVER_ACCOUNT_NAME } from '../../../initializers/constants' -import { database as db } from '../../../initializers/database' -import { saveAccountAndServerIfNotExist } from '../../../lib/activitypub/account' -import { sendUndoFollow } from '../../../lib/activitypub/send/send-undo' +import { UserRight } from '../../../../shared/models/users' +import { getAccountFromWebfinger, getFormattedObjects, getServerAccount, logger, retryTransactionWrapper } from '../../../helpers' +import { sequelizeTypescript, SERVER_ACCOUNT_NAME } from '../../../initializers' +import { saveAccountAndServerIfNotExist } from '../../../lib/activitypub' +import { sendUndoFollow } from '../../../lib/activitypub/send' import { sendFollow } from '../../../lib/index' -import { asyncMiddleware, paginationValidator, removeFollowingValidator, setFollowersSort, setPagination } from '../../../middlewares' -import { authenticate } from '../../../middlewares/oauth' -import { setBodyHostsPort } from '../../../middlewares/servers' -import { setFollowingSort } from '../../../middlewares/sort' -import { ensureUserHasRight } from '../../../middlewares/user-right' -import { followValidator } from '../../../middlewares/validators/follows' -import { followersSortValidator, followingSortValidator } from '../../../middlewares/validators/sort' -import { AccountInstance } from '../../../models/account/account-interface' -import { AccountFollowInstance } from '../../../models/index' +import { + asyncMiddleware, + authenticate, + ensureUserHasRight, + paginationValidator, + removeFollowingValidator, + setBodyHostsPort, + setFollowersSort, + setFollowingSort, + setPagination +} from '../../../middlewares' +import { followersSortValidator, followingSortValidator, followValidator } from '../../../middlewares/validators' +import { AccountModel } from '../../../models/account/account' +import { AccountFollowModel } from '../../../models/account/account-follow' const serverFollowsRouter = express.Router() @@ -63,14 +63,14 @@ export { async function listFollowing (req: express.Request, res: express.Response, next: express.NextFunction) { const serverAccount = await getServerAccount() - const resultList = await db.AccountFollow.listFollowingForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort) + const resultList = await AccountFollowModel.listFollowingForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort) return res.json(getFormattedObjects(resultList.data, resultList.total)) } async function listFollowers (req: express.Request, res: express.Response, next: express.NextFunction) { const serverAccount = await getServerAccount() - const resultList = await db.AccountFollow.listFollowersForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort) + const resultList = await AccountFollowModel.listFollowersForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort) return res.json(getFormattedObjects(resultList.data, resultList.total)) } @@ -110,14 +110,14 @@ async function followRetry (req: express.Request, res: express.Response, next: e return res.status(204).end() } -async function follow (fromAccount: AccountInstance, targetAccount: AccountInstance, targetAlreadyInDB: boolean) { +async function follow (fromAccount: AccountModel, targetAccount: AccountModel, targetAlreadyInDB: boolean) { try { - await db.sequelize.transaction(async t => { + await sequelizeTypescript.transaction(async t => { if (targetAlreadyInDB === false) { await saveAccountAndServerIfNotExist(targetAccount, t) } - const [ accountFollow ] = await db.AccountFollow.findOrCreate({ + const [ accountFollow ] = await AccountFollowModel.findOrCreate({ where: { accountId: fromAccount.id, targetAccountId: targetAccount.id @@ -145,9 +145,9 @@ async function follow (fromAccount: AccountInstance, targetAccount: AccountInsta } async function removeFollow (req: express.Request, res: express.Response, next: express.NextFunction) { - const follow: AccountFollowInstance = res.locals.follow + const follow: AccountFollowModel = res.locals.follow - await db.sequelize.transaction(async t => { + await sequelizeTypescript.transaction(async t => { if (follow.state === 'accepted') await sendUndoFollow(follow, t) await follow.destroy({ transaction: t }) @@ -164,7 +164,7 @@ async function removeFollow (req: express.Request, res: express.Response, next: async function loadLocalOrGetAccountFromWebfinger (name: string, host: string) { let loadedFromDB = true - let account = await db.Account.loadByNameAndHost(name, host) + let account = await AccountModel.loadByNameAndHost(name, host) if (!account) { const nameWithDomain = name + '@' + host diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts index f9b871724..d6c0e67f9 100644 --- a/server/controllers/api/users.ts +++ b/server/controllers/api/users.ts @@ -1,7 +1,7 @@ import * as express from 'express' import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared' import { getFormattedObjects, logger, retryTransactionWrapper } from '../../helpers' -import { CONFIG, database as db } from '../../initializers' +import { CONFIG } from '../../initializers' import { createUserAccountAndChannel } from '../../lib' import { asyncMiddleware, @@ -11,6 +11,7 @@ import { paginationValidator, setPagination, setUsersSort, + setVideosSort, token, usersAddValidator, usersGetValidator, @@ -21,9 +22,10 @@ import { usersUpdateValidator, usersVideoRatingValidator } from '../../middlewares' -import { setVideosSort } from '../../middlewares/sort' -import { videosSortValidator } from '../../middlewares/validators/sort' -import { UserInstance } from '../../models' +import { videosSortValidator } from '../../middlewares/validators' +import { AccountVideoRateModel } from '../../models/account/account-video-rate' +import { UserModel } from '../../models/account/user' +import { VideoModel } from '../../models/video/video' const usersRouter = express.Router() @@ -107,8 +109,8 @@ export { // --------------------------------------------------------------------------- async function getUserVideos (req: express.Request, res: express.Response, next: express.NextFunction) { - const user = res.locals.oauth.token.User - const resultList = await db.Video.listUserVideosForApi(user.id ,req.query.start, req.query.count, req.query.sort) + const user = res.locals.oauth.token.User as UserModel + const resultList = await VideoModel.listUserVideosForApi(user.id ,req.query.start, req.query.count, req.query.sort) return res.json(getFormattedObjects(resultList.data, resultList.total)) } @@ -127,7 +129,7 @@ async function createUserRetryWrapper (req: express.Request, res: express.Respon async function createUser (req: express.Request) { const body: UserCreate = req.body - const user = db.User.build({ + const user = new UserModel({ username: body.username, password: body.password, email: body.email, @@ -155,7 +157,7 @@ async function registerUserRetryWrapper (req: express.Request, res: express.Resp async function registerUser (req: express.Request) { const body: UserCreate = req.body - const user = db.User.build({ + const user = new UserModel({ username: body.username, password: body.password, email: body.email, @@ -171,7 +173,7 @@ async function registerUser (req: express.Request) { async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) { // We did not load channels in res.locals.user - const user = await db.User.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username) + const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username) return res.json(user.toFormattedJSON()) } @@ -184,7 +186,7 @@ async function getUserVideoRating (req: express.Request, res: express.Response, const videoId = +req.params.videoId const accountId = +res.locals.oauth.token.User.Account.id - const ratingObj = await db.AccountVideoRate.load(accountId, videoId, null) + const ratingObj = await AccountVideoRateModel.load(accountId, videoId, null) const rating = ratingObj ? ratingObj.type : 'none' const json: FormattedUserVideoRate = { @@ -195,13 +197,13 @@ async function getUserVideoRating (req: express.Request, res: express.Response, } async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) { - const resultList = await db.User.listForApi(req.query.start, req.query.count, req.query.sort) + const resultList = await UserModel.listForApi(req.query.start, req.query.count, req.query.sort) return res.json(getFormattedObjects(resultList.data, resultList.total)) } async function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) { - const user = await db.User.loadById(req.params.id) + const user = await UserModel.loadById(req.params.id) await user.destroy() @@ -225,7 +227,7 @@ async function updateMe (req: express.Request, res: express.Response, next: expr async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) { const body: UserUpdate = req.body - const user: UserInstance = res.locals.user + const user = res.locals.user as UserModel if (body.email !== undefined) user.email = body.email if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota 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