diff options
Diffstat (limited to 'server/controllers/api')
-rw-r--r-- | server/controllers/api/videos/rate.ts | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/server/controllers/api/videos/rate.ts b/server/controllers/api/videos/rate.ts index dc322bb0c..53952a0a2 100644 --- a/server/controllers/api/videos/rate.ts +++ b/server/controllers/api/videos/rate.ts | |||
@@ -2,8 +2,8 @@ import * as express from 'express' | |||
2 | import { UserVideoRateUpdate } from '../../../../shared' | 2 | import { UserVideoRateUpdate } from '../../../../shared' |
3 | import { logger } from '../../../helpers/logger' | 3 | import { logger } from '../../../helpers/logger' |
4 | import { sequelizeTypescript, VIDEO_RATE_TYPES } from '../../../initializers' | 4 | import { sequelizeTypescript, VIDEO_RATE_TYPES } from '../../../initializers' |
5 | import { sendVideoRateChange } from '../../../lib/activitypub' | 5 | import { getRateUrl, sendVideoRateChange } from '../../../lib/activitypub' |
6 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoRateValidator } from '../../../middlewares' | 6 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoUpdateRateValidator } from '../../../middlewares' |
7 | import { AccountModel } from '../../../models/account/account' | 7 | import { AccountModel } from '../../../models/account/account' |
8 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | 8 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' |
9 | import { VideoModel } from '../../../models/video/video' | 9 | import { VideoModel } from '../../../models/video/video' |
@@ -12,7 +12,7 @@ const rateVideoRouter = express.Router() | |||
12 | 12 | ||
13 | rateVideoRouter.put('/:id/rate', | 13 | rateVideoRouter.put('/:id/rate', |
14 | authenticate, | 14 | authenticate, |
15 | asyncMiddleware(videoRateValidator), | 15 | asyncMiddleware(videoUpdateRateValidator), |
16 | asyncRetryTransactionMiddleware(rateVideo) | 16 | asyncRetryTransactionMiddleware(rateVideo) |
17 | ) | 17 | ) |
18 | 18 | ||
@@ -28,11 +28,12 @@ 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 userAccount: AccountModel = res.locals.oauth.token.User.Account | ||
31 | 32 | ||
32 | await sequelizeTypescript.transaction(async t => { | 33 | await sequelizeTypescript.transaction(async t => { |
33 | const sequelizeOptions = { transaction: t } | 34 | const sequelizeOptions = { transaction: t } |
34 | 35 | ||
35 | const accountInstance = await AccountModel.load(res.locals.oauth.token.User.Account.id, t) | 36 | const accountInstance = await AccountModel.load(userAccount.id, t) |
36 | const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t) | 37 | const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t) |
37 | 38 | ||
38 | let likesToIncrement = 0 | 39 | let likesToIncrement = 0 |
@@ -44,20 +45,22 @@ async function rateVideo (req: express.Request, res: express.Response) { | |||
44 | // There was a previous rate, update it | 45 | // There was a previous rate, update it |
45 | if (previousRate) { | 46 | if (previousRate) { |
46 | // We will remove the previous rate, so we will need to update the video count attribute | 47 | // We will remove the previous rate, so we will need to update the video count attribute |
47 | if (previousRate.type === VIDEO_RATE_TYPES.LIKE) likesToIncrement-- | 48 | if (previousRate.type === 'like') likesToIncrement-- |
48 | else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement-- | 49 | else if (previousRate.type === 'dislike') dislikesToIncrement-- |
49 | 50 | ||
50 | if (rateType === 'none') { // Destroy previous rate | 51 | if (rateType === 'none') { // Destroy previous rate |
51 | await previousRate.destroy(sequelizeOptions) | 52 | await previousRate.destroy(sequelizeOptions) |
52 | } else { // Update previous rate | 53 | } else { // Update previous rate |
53 | previousRate.type = rateType | 54 | previousRate.type = rateType |
55 | previousRate.url = getRateUrl(rateType, userAccount.Actor, videoInstance) | ||
54 | await previousRate.save(sequelizeOptions) | 56 | await previousRate.save(sequelizeOptions) |
55 | } | 57 | } |
56 | } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate | 58 | } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate |
57 | const query = { | 59 | const query = { |
58 | accountId: accountInstance.id, | 60 | accountId: accountInstance.id, |
59 | videoId: videoInstance.id, | 61 | videoId: videoInstance.id, |
60 | type: rateType | 62 | type: rateType, |
63 | url: getRateUrl(rateType, userAccount.Actor, videoInstance) | ||
61 | } | 64 | } |
62 | 65 | ||
63 | await AccountVideoRateModel.create(query, sequelizeOptions) | 66 | await AccountVideoRateModel.create(query, sequelizeOptions) |