]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
53abc4c2 | 2 | import { UserVideoRateUpdate } from '../../../../shared' |
da854ddd | 3 | import { logger } from '../../../helpers/logger' |
74dc3bca | 4 | import { VIDEO_RATE_TYPES } from '../../../initializers/constants' |
de94ac86 | 5 | import { getLocalRateUrl, sendVideoRateChange } from '../../../lib/activitypub/video-rates' |
5c6d985f | 6 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoUpdateRateValidator } from '../../../middlewares' |
3fd3ab2d C |
7 | import { AccountModel } from '../../../models/account/account' |
8 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | |
74dc3bca | 9 | import { sequelizeTypescript } from '../../../initializers/database' |
2d53be02 | 10 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' |
65fcc311 C |
11 | |
12 | const rateVideoRouter = express.Router() | |
13 | ||
14 | rateVideoRouter.put('/:id/rate', | |
15 | authenticate, | |
5c6d985f | 16 | asyncMiddleware(videoUpdateRateValidator), |
90d4bb81 | 17 | asyncRetryTransactionMiddleware(rateVideo) |
d33242b0 C |
18 | ) |
19 | ||
20 | // --------------------------------------------------------------------------- | |
21 | ||
65fcc311 C |
22 | export { |
23 | rateVideoRouter | |
24 | } | |
d33242b0 C |
25 | |
26 | // --------------------------------------------------------------------------- | |
27 | ||
eb080476 | 28 | async function rateVideo (req: express.Request, res: express.Response) { |
4771e000 C |
29 | const body: UserVideoRateUpdate = req.body |
30 | const rateType = body.rating | |
453e83ea | 31 | const videoInstance = res.locals.videoAll |
dae86118 | 32 | const userAccount = res.locals.oauth.token.User.Account |
d33242b0 | 33 | |
3fd3ab2d | 34 | await sequelizeTypescript.transaction(async t => { |
eb080476 | 35 | const sequelizeOptions = { transaction: t } |
91411dba | 36 | |
5c6d985f | 37 | const accountInstance = await AccountModel.load(userAccount.id, t) |
3fd3ab2d | 38 | const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t) |
eb080476 | 39 | |
285981f4 C |
40 | // Same rate, nothing do to |
41 | if (rateType === 'none' && !previousRate || previousRate?.type === rateType) return | |
42 | ||
eb080476 C |
43 | let likesToIncrement = 0 |
44 | let dislikesToIncrement = 0 | |
45 | ||
46 | if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++ | |
47 | else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++ | |
48 | ||
49 | // There was a previous rate, update it | |
50 | if (previousRate) { | |
51 | // We will remove the previous rate, so we will need to update the video count attribute | |
5c6d985f C |
52 | if (previousRate.type === 'like') likesToIncrement-- |
53 | else if (previousRate.type === 'dislike') dislikesToIncrement-- | |
eb080476 C |
54 | |
55 | if (rateType === 'none') { // Destroy previous rate | |
91411dba | 56 | await previousRate.destroy(sequelizeOptions) |
eb080476 | 57 | } else { // Update previous rate |
53abc4c2 | 58 | previousRate.type = rateType |
de94ac86 | 59 | previousRate.url = getLocalRateUrl(rateType, userAccount.Actor, videoInstance) |
91411dba | 60 | await previousRate.save(sequelizeOptions) |
eb080476 C |
61 | } |
62 | } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate | |
63 | const query = { | |
571389d4 | 64 | accountId: accountInstance.id, |
eb080476 | 65 | videoId: videoInstance.id, |
5c6d985f | 66 | type: rateType, |
de94ac86 | 67 | url: getLocalRateUrl(rateType, userAccount.Actor, videoInstance) |
eb080476 C |
68 | } |
69 | ||
3fd3ab2d | 70 | await AccountVideoRateModel.create(query, sequelizeOptions) |
eb080476 C |
71 | } |
72 | ||
73 | const incrementQuery = { | |
74 | likes: likesToIncrement, | |
75 | dislikes: dislikesToIncrement | |
76 | } | |
77 | ||
eb080476 C |
78 | await videoInstance.increment(incrementQuery, sequelizeOptions) |
79 | ||
07197db4 | 80 | await sendVideoRateChange(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t) |
eb080476 | 81 | |
91411dba C |
82 | logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name) |
83 | }) | |
90d4bb81 | 84 | |
2d53be02 RK |
85 | return res.type('json') |
86 | .status(HttpStatusCode.NO_CONTENT_204) | |
87 | .end() | |
d33242b0 | 88 | } |