aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/rate.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/videos/rate.ts')
-rw-r--r--server/controllers/api/videos/rate.ts11
1 files changed, 6 insertions, 5 deletions
diff --git a/server/controllers/api/videos/rate.ts b/server/controllers/api/videos/rate.ts
index b1732837d..dc322bb0c 100644
--- a/server/controllers/api/videos/rate.ts
+++ b/server/controllers/api/videos/rate.ts
@@ -28,10 +28,11 @@ async function rateVideo (req: express.Request, res: express.Response) {
28 const body: UserVideoRateUpdate = req.body 28 const body: UserVideoRateUpdate = req.body
29 const rateType = body.rating 29 const rateType = body.rating
30 const videoInstance: VideoModel = res.locals.video 30 const videoInstance: VideoModel = res.locals.video
31 const accountInstance: AccountModel = res.locals.oauth.token.User.Account
32 31
33 await sequelizeTypescript.transaction(async t => { 32 await sequelizeTypescript.transaction(async t => {
34 const sequelizeOptions = { transaction: t } 33 const sequelizeOptions = { transaction: t }
34
35 const accountInstance = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
35 const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t) 36 const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t)
36 37
37 let likesToIncrement = 0 38 let likesToIncrement = 0
@@ -47,10 +48,10 @@ async function rateVideo (req: express.Request, res: express.Response) {
47 else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement-- 48 else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement--
48 49
49 if (rateType === 'none') { // Destroy previous rate 50 if (rateType === 'none') { // Destroy previous rate
50 await previousRate.destroy({ transaction: t }) 51 await previousRate.destroy(sequelizeOptions)
51 } else { // Update previous rate 52 } else { // Update previous rate
52 previousRate.type = rateType 53 previousRate.type = rateType
53 await previousRate.save({ transaction: t }) 54 await previousRate.save(sequelizeOptions)
54 } 55 }
55 } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate 56 } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate
56 const query = { 57 const query = {
@@ -70,9 +71,9 @@ async function rateVideo (req: express.Request, res: express.Response) {
70 await videoInstance.increment(incrementQuery, sequelizeOptions) 71 await videoInstance.increment(incrementQuery, sequelizeOptions)
71 72
72 await sendVideoRateChange(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t) 73 await sendVideoRateChange(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
73 })
74 74
75 logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name) 75 logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name)
76 })
76 77
77 return res.type('json').status(204).end() 78 return res.type('json').status(204).end()
78} 79}