2017-06-05 14:53:49 -05:00
|
|
|
import * as express from 'express'
|
2017-05-05 09:53:35 -05:00
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
import { database as db } from '../../../initializers/database'
|
2017-05-15 15:22:03 -05:00
|
|
|
import {
|
|
|
|
logger,
|
2017-07-05 06:26:25 -05:00
|
|
|
retryTransactionWrapper
|
2017-05-15 15:22:03 -05:00
|
|
|
} from '../../../helpers'
|
|
|
|
import {
|
|
|
|
VIDEO_RATE_TYPES,
|
|
|
|
REQUEST_VIDEO_EVENT_TYPES,
|
|
|
|
REQUEST_VIDEO_QADU_TYPES
|
|
|
|
} from '../../../initializers'
|
|
|
|
import {
|
|
|
|
addEventsToRemoteVideo,
|
|
|
|
quickAndDirtyUpdatesVideoToFriends
|
|
|
|
} from '../../../lib'
|
|
|
|
import {
|
|
|
|
authenticate,
|
2017-10-25 04:55:06 -05:00
|
|
|
videoRateValidator,
|
|
|
|
asyncMiddleware
|
2017-05-15 15:22:03 -05:00
|
|
|
} from '../../../middlewares'
|
2017-10-31 10:31:24 -05:00
|
|
|
import { UserVideoRateUpdate } from '../../../../shared'
|
2017-05-15 15:22:03 -05:00
|
|
|
|
|
|
|
const rateVideoRouter = express.Router()
|
|
|
|
|
|
|
|
rateVideoRouter.put('/:id/rate',
|
|
|
|
authenticate,
|
|
|
|
videoRateValidator,
|
2017-10-25 04:55:06 -05:00
|
|
|
asyncMiddleware(rateVideoRetryWrapper)
|
2017-05-05 09:53:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
export {
|
|
|
|
rateVideoRouter
|
|
|
|
}
|
2017-05-05 09:53:35 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function rateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-05-05 09:53:35 -05:00
|
|
|
const options = {
|
|
|
|
arguments: [ req, res ],
|
|
|
|
errorMessage: 'Cannot update the user video rate.'
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await retryTransactionWrapper(rateVideo, options)
|
|
|
|
|
|
|
|
return res.type('json').status(204).end()
|
2017-05-05 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function rateVideo (req: express.Request, res: express.Response) {
|
2017-07-10 12:43:21 -05:00
|
|
|
const body: UserVideoRateUpdate = req.body
|
|
|
|
const rateType = body.rating
|
2017-05-05 09:53:35 -05:00
|
|
|
const videoInstance = res.locals.video
|
|
|
|
const userInstance = res.locals.oauth.token.User
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await db.sequelize.transaction(async t => {
|
|
|
|
const sequelizeOptions = { transaction: t }
|
|
|
|
const previousRate = await db.UserVideoRate.load(userInstance.id, videoInstance.id, t)
|
|
|
|
|
|
|
|
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
|
|
|
|
await previousRate.destroy()
|
|
|
|
} else { // Update previous rate
|
2017-10-31 10:31:24 -05:00
|
|
|
previousRate.type = rateType
|
2017-10-25 04:55:06 -05:00
|
|
|
|
|
|
|
await previousRate.save()
|
|
|
|
}
|
|
|
|
} else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate
|
|
|
|
const query = {
|
|
|
|
userId: userInstance.id,
|
|
|
|
videoId: videoInstance.id,
|
|
|
|
type: rateType
|
|
|
|
}
|
|
|
|
|
|
|
|
await db.UserVideoRate.create(query, sequelizeOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
const incrementQuery = {
|
|
|
|
likes: likesToIncrement,
|
|
|
|
dislikes: dislikesToIncrement
|
|
|
|
}
|
|
|
|
|
|
|
|
// Even if we do not own the video we increment the attributes
|
|
|
|
// It is useful for the user to have a feedback
|
|
|
|
await videoInstance.increment(incrementQuery, sequelizeOptions)
|
|
|
|
|
|
|
|
// Send a event to original pod
|
|
|
|
if (videoInstance.isOwned() === false) {
|
|
|
|
|
|
|
|
const eventsParams = []
|
|
|
|
|
|
|
|
if (likesToIncrement !== 0) {
|
|
|
|
eventsParams.push({
|
|
|
|
videoId: videoInstance.id,
|
|
|
|
type: REQUEST_VIDEO_EVENT_TYPES.LIKES,
|
|
|
|
count: likesToIncrement
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dislikesToIncrement !== 0) {
|
|
|
|
eventsParams.push({
|
|
|
|
videoId: videoInstance.id,
|
|
|
|
type: REQUEST_VIDEO_EVENT_TYPES.DISLIKES,
|
|
|
|
count: dislikesToIncrement
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
await addEventsToRemoteVideo(eventsParams, t)
|
|
|
|
} else { // We own the video, we need to send a quick and dirty update to friends to notify the counts changed
|
|
|
|
const qadusParams = []
|
|
|
|
|
|
|
|
if (likesToIncrement !== 0) {
|
|
|
|
qadusParams.push({
|
|
|
|
videoId: videoInstance.id,
|
|
|
|
type: REQUEST_VIDEO_QADU_TYPES.LIKES
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dislikesToIncrement !== 0) {
|
|
|
|
qadusParams.push({
|
|
|
|
videoId: videoInstance.id,
|
|
|
|
type: REQUEST_VIDEO_QADU_TYPES.DISLIKES
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
await quickAndDirtyUpdatesVideoToFriends(qadusParams, t)
|
|
|
|
}
|
2017-05-05 09:53:35 -05:00
|
|
|
})
|
2017-10-25 04:55:06 -05:00
|
|
|
|
|
|
|
logger.info('User video rate for video %s of user %s updated.', videoInstance.name, userInstance.username)
|
2017-05-05 09:53:35 -05:00
|
|
|
}
|