2017-06-05 21:53:49 +02:00
|
|
|
import * as express from 'express'
|
2017-10-31 16:31:24 +01:00
|
|
|
import { UserVideoRateUpdate } from '../../../../shared'
|
2017-12-28 11:16:08 +01:00
|
|
|
import { logger } from '../../../helpers/logger'
|
2019-04-11 14:26:41 +02:00
|
|
|
import { VIDEO_RATE_TYPES } from '../../../initializers/constants'
|
2020-11-20 11:21:08 +01:00
|
|
|
import { getLocalRateUrl, sendVideoRateChange } from '../../../lib/activitypub/video-rates'
|
2018-11-14 15:01:28 +01:00
|
|
|
import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoUpdateRateValidator } from '../../../middlewares'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { AccountModel } from '../../../models/account/account'
|
|
|
|
import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
|
2019-04-11 14:26:41 +02:00
|
|
|
import { sequelizeTypescript } from '../../../initializers/database'
|
2020-12-07 14:32:36 +01:00
|
|
|
import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
|
2017-05-15 22:22:03 +02:00
|
|
|
|
|
|
|
const rateVideoRouter = express.Router()
|
|
|
|
|
|
|
|
rateVideoRouter.put('/:id/rate',
|
|
|
|
authenticate,
|
2018-11-14 15:01:28 +01:00
|
|
|
asyncMiddleware(videoUpdateRateValidator),
|
2018-06-13 14:27:40 +02:00
|
|
|
asyncRetryTransactionMiddleware(rateVideo)
|
2017-05-05 16:53:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 22:22:03 +02:00
|
|
|
export {
|
|
|
|
rateVideoRouter
|
|
|
|
}
|
2017-05-05 16:53:35 +02:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-10-25 11:55:06 +02:00
|
|
|
async function rateVideo (req: express.Request, res: express.Response) {
|
2017-07-10 19:43:21 +02:00
|
|
|
const body: UserVideoRateUpdate = req.body
|
|
|
|
const rateType = body.rating
|
2019-08-15 11:53:26 +02:00
|
|
|
const videoInstance = res.locals.videoAll
|
2019-03-19 10:35:15 +01:00
|
|
|
const userAccount = res.locals.oauth.token.User.Account
|
2017-05-05 16:53:35 +02:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
await sequelizeTypescript.transaction(async t => {
|
2017-10-25 11:55:06 +02:00
|
|
|
const sequelizeOptions = { transaction: t }
|
2018-09-20 10:13:13 +02:00
|
|
|
|
2018-11-14 15:01:28 +01:00
|
|
|
const accountInstance = await AccountModel.load(userAccount.id, t)
|
2017-12-12 17:53:50 +01:00
|
|
|
const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t)
|
2017-10-25 11:55:06 +02:00
|
|
|
|
2021-02-19 10:26:58 +01:00
|
|
|
// Same rate, nothing do to
|
|
|
|
if (rateType === 'none' && !previousRate || previousRate?.type === rateType) return
|
|
|
|
|
2017-10-25 11:55:06 +02:00
|
|
|
let likesToIncrement = 0
|
|
|
|
let dislikesToIncrement = 0
|
|
|
|
|
|
|
|
if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++
|
|
|
|
else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++
|
|
|
|
|
|
|
|
// There was a previous rate, update it
|
|
|
|
if (previousRate) {
|
|
|
|
// We will remove the previous rate, so we will need to update the video count attribute
|
2018-11-14 15:01:28 +01:00
|
|
|
if (previousRate.type === 'like') likesToIncrement--
|
|
|
|
else if (previousRate.type === 'dislike') dislikesToIncrement--
|
2017-10-25 11:55:06 +02:00
|
|
|
|
|
|
|
if (rateType === 'none') { // Destroy previous rate
|
2018-09-20 10:13:13 +02:00
|
|
|
await previousRate.destroy(sequelizeOptions)
|
2017-10-25 11:55:06 +02:00
|
|
|
} else { // Update previous rate
|
2017-10-31 16:31:24 +01:00
|
|
|
previousRate.type = rateType
|
2020-11-20 11:21:08 +01:00
|
|
|
previousRate.url = getLocalRateUrl(rateType, userAccount.Actor, videoInstance)
|
2018-09-20 10:13:13 +02:00
|
|
|
await previousRate.save(sequelizeOptions)
|
2017-10-25 11:55:06 +02:00
|
|
|
}
|
|
|
|
} else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate
|
|
|
|
const query = {
|
2017-11-10 17:27:49 +01:00
|
|
|
accountId: accountInstance.id,
|
2017-10-25 11:55:06 +02:00
|
|
|
videoId: videoInstance.id,
|
2018-11-14 15:01:28 +01:00
|
|
|
type: rateType,
|
2020-11-20 11:21:08 +01:00
|
|
|
url: getLocalRateUrl(rateType, userAccount.Actor, videoInstance)
|
2017-10-25 11:55:06 +02:00
|
|
|
}
|
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
await AccountVideoRateModel.create(query, sequelizeOptions)
|
2017-10-25 11:55:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const incrementQuery = {
|
|
|
|
likes: likesToIncrement,
|
|
|
|
dislikes: dislikesToIncrement
|
|
|
|
}
|
|
|
|
|
|
|
|
await videoInstance.increment(incrementQuery, sequelizeOptions)
|
|
|
|
|
2018-03-27 13:33:56 +02:00
|
|
|
await sendVideoRateChange(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
|
2017-10-25 11:55:06 +02:00
|
|
|
|
2018-09-20 10:13:13 +02:00
|
|
|
logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name)
|
|
|
|
})
|
2018-06-13 14:27:40 +02:00
|
|
|
|
2020-12-07 14:32:36 +01:00
|
|
|
return res.type('json')
|
|
|
|
.status(HttpStatusCode.NO_CONTENT_204)
|
|
|
|
.end()
|
2017-05-05 16:53:35 +02:00
|
|
|
}
|