2017-06-05 15:53:49 -04:00
|
|
|
import * as express from 'express'
|
2017-10-31 11:31:24 -04:00
|
|
|
import { UserVideoRateUpdate } from '../../../../shared'
|
2017-12-28 05:16:08 -05:00
|
|
|
import { logger } from '../../../helpers/logger'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { sequelizeTypescript, VIDEO_RATE_TYPES } from '../../../initializers'
|
2018-03-27 07:33:56 -04:00
|
|
|
import { sendVideoRateChange } from '../../../lib/activitypub'
|
2018-06-13 08:27:40 -04:00
|
|
|
import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoRateValidator } from '../../../middlewares'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { AccountModel } from '../../../models/account/account'
|
|
|
|
import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
|
|
|
|
import { VideoModel } from '../../../models/video/video'
|
2017-05-15 16:22:03 -04:00
|
|
|
|
|
|
|
const rateVideoRouter = express.Router()
|
|
|
|
|
|
|
|
rateVideoRouter.put('/:id/rate',
|
|
|
|
authenticate,
|
2017-11-27 11:30:46 -05:00
|
|
|
asyncMiddleware(videoRateValidator),
|
2018-06-13 08:27:40 -04:00
|
|
|
asyncRetryTransactionMiddleware(rateVideo)
|
2017-05-05 10:53:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
export {
|
|
|
|
rateVideoRouter
|
|
|
|
}
|
2017-05-05 10:53:35 -04:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
async function rateVideo (req: express.Request, res: express.Response) {
|
2017-07-10 13:43:21 -04:00
|
|
|
const body: UserVideoRateUpdate = req.body
|
|
|
|
const rateType = body.rating
|
2017-12-12 11:53:50 -05:00
|
|
|
const videoInstance: VideoModel = res.locals.video
|
2017-05-05 10:53:35 -04:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
await sequelizeTypescript.transaction(async t => {
|
2017-10-25 05:55:06 -04:00
|
|
|
const sequelizeOptions = { transaction: t }
|
2018-09-20 04:13:13 -04:00
|
|
|
|
|
|
|
const accountInstance = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
|
2017-12-12 11:53:50 -05:00
|
|
|
const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t)
|
2017-10-25 05:55:06 -04: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
|
|
|
|
if (previousRate.type === VIDEO_RATE_TYPES.LIKE) likesToIncrement--
|
|
|
|
else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement--
|
|
|
|
|
|
|
|
if (rateType === 'none') { // Destroy previous rate
|
2018-09-20 04:13:13 -04:00
|
|
|
await previousRate.destroy(sequelizeOptions)
|
2017-10-25 05:55:06 -04:00
|
|
|
} else { // Update previous rate
|
2017-10-31 11:31:24 -04:00
|
|
|
previousRate.type = rateType
|
2018-09-20 04:13:13 -04:00
|
|
|
await previousRate.save(sequelizeOptions)
|
2017-10-25 05:55:06 -04: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 11:27:49 -05:00
|
|
|
accountId: accountInstance.id,
|
2017-10-25 05:55:06 -04:00
|
|
|
videoId: videoInstance.id,
|
|
|
|
type: rateType
|
|
|
|
}
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
await AccountVideoRateModel.create(query, sequelizeOptions)
|
2017-10-25 05:55:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const incrementQuery = {
|
|
|
|
likes: likesToIncrement,
|
|
|
|
dislikes: dislikesToIncrement
|
|
|
|
}
|
|
|
|
|
|
|
|
await videoInstance.increment(incrementQuery, sequelizeOptions)
|
|
|
|
|
2018-03-27 07:33:56 -04:00
|
|
|
await sendVideoRateChange(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
|
2017-10-25 05:55:06 -04:00
|
|
|
|
2018-09-20 04:13:13 -04:00
|
|
|
logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name)
|
|
|
|
})
|
2018-06-13 08:27:40 -04:00
|
|
|
|
|
|
|
return res.type('json').status(204).end()
|
2017-05-05 10:53:35 -04:00
|
|
|
}
|