diff options
Diffstat (limited to 'server/controllers/api/videos/rate.ts')
-rw-r--r-- | server/controllers/api/videos/rate.ts | 80 |
1 files changed, 15 insertions, 65 deletions
diff --git a/server/controllers/api/videos/rate.ts b/server/controllers/api/videos/rate.ts index 727984506..955277d25 100644 --- a/server/controllers/api/videos/rate.ts +++ b/server/controllers/api/videos/rate.ts | |||
@@ -1,25 +1,11 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | |||
3 | import { database as db } from '../../../initializers/database' | ||
4 | import { | ||
5 | logger, | ||
6 | retryTransactionWrapper | ||
7 | } from '../../../helpers' | ||
8 | import { | ||
9 | VIDEO_RATE_TYPES, | ||
10 | REQUEST_VIDEO_EVENT_TYPES, | ||
11 | REQUEST_VIDEO_QADU_TYPES | ||
12 | } from '../../../initializers' | ||
13 | import { | ||
14 | addEventsToRemoteVideo, | ||
15 | quickAndDirtyUpdatesVideoToFriends | ||
16 | } from '../../../lib' | ||
17 | import { | ||
18 | authenticate, | ||
19 | videoRateValidator, | ||
20 | asyncMiddleware | ||
21 | } from '../../../middlewares' | ||
22 | import { UserVideoRateUpdate } from '../../../../shared' | 2 | import { UserVideoRateUpdate } from '../../../../shared' |
3 | import { logger, retryTransactionWrapper } from '../../../helpers' | ||
4 | import { VIDEO_RATE_TYPES } from '../../../initializers' | ||
5 | import { database as db } from '../../../initializers/database' | ||
6 | import { asyncMiddleware, authenticate, videoRateValidator } from '../../../middlewares' | ||
7 | import { AccountInstance } from '../../../models/account/account-interface' | ||
8 | import { VideoInstance } from '../../../models/video/video-interface' | ||
23 | 9 | ||
24 | const rateVideoRouter = express.Router() | 10 | const rateVideoRouter = express.Router() |
25 | 11 | ||
@@ -51,12 +37,12 @@ async function rateVideoRetryWrapper (req: express.Request, res: express.Respons | |||
51 | async function rateVideo (req: express.Request, res: express.Response) { | 37 | async function rateVideo (req: express.Request, res: express.Response) { |
52 | const body: UserVideoRateUpdate = req.body | 38 | const body: UserVideoRateUpdate = req.body |
53 | const rateType = body.rating | 39 | const rateType = body.rating |
54 | const videoInstance = res.locals.video | 40 | const videoInstance: VideoInstance = res.locals.video |
55 | const userInstance = res.locals.oauth.token.User | 41 | const accountInstance: AccountInstance = res.locals.oauth.token.User.Account |
56 | 42 | ||
57 | await db.sequelize.transaction(async t => { | 43 | await db.sequelize.transaction(async t => { |
58 | const sequelizeOptions = { transaction: t } | 44 | const sequelizeOptions = { transaction: t } |
59 | const previousRate = await db.UserVideoRate.load(userInstance.id, videoInstance.id, t) | 45 | const previousRate = await db.AccountVideoRate.load(accountInstance.id, videoInstance.id, t) |
60 | 46 | ||
61 | let likesToIncrement = 0 | 47 | let likesToIncrement = 0 |
62 | let dislikesToIncrement = 0 | 48 | let dislikesToIncrement = 0 |
@@ -79,12 +65,12 @@ async function rateVideo (req: express.Request, res: express.Response) { | |||
79 | } | 65 | } |
80 | } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate | 66 | } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate |
81 | const query = { | 67 | const query = { |
82 | userId: userInstance.id, | 68 | accountId: accountInstance.id, |
83 | videoId: videoInstance.id, | 69 | videoId: videoInstance.id, |
84 | type: rateType | 70 | type: rateType |
85 | } | 71 | } |
86 | 72 | ||
87 | await db.UserVideoRate.create(query, sequelizeOptions) | 73 | await db.AccountVideoRate.create(query, sequelizeOptions) |
88 | } | 74 | } |
89 | 75 | ||
90 | const incrementQuery = { | 76 | const incrementQuery = { |
@@ -96,48 +82,12 @@ async function rateVideo (req: express.Request, res: express.Response) { | |||
96 | // It is useful for the user to have a feedback | 82 | // It is useful for the user to have a feedback |
97 | await videoInstance.increment(incrementQuery, sequelizeOptions) | 83 | await videoInstance.increment(incrementQuery, sequelizeOptions) |
98 | 84 | ||
99 | // Send a event to original pod | ||
100 | if (videoInstance.isOwned() === false) { | 85 | if (videoInstance.isOwned() === false) { |
101 | 86 | // TODO: Send a event to original pod | |
102 | const eventsParams = [] | 87 | } else { |
103 | 88 | // TODO: Send update to followers | |
104 | if (likesToIncrement !== 0) { | ||
105 | eventsParams.push({ | ||
106 | videoId: videoInstance.id, | ||
107 | type: REQUEST_VIDEO_EVENT_TYPES.LIKES, | ||
108 | count: likesToIncrement | ||
109 | }) | ||
110 | } | ||
111 | |||
112 | if (dislikesToIncrement !== 0) { | ||
113 | eventsParams.push({ | ||
114 | videoId: videoInstance.id, | ||
115 | type: REQUEST_VIDEO_EVENT_TYPES.DISLIKES, | ||
116 | count: dislikesToIncrement | ||
117 | }) | ||
118 | } | ||
119 | |||
120 | await addEventsToRemoteVideo(eventsParams, t) | ||
121 | } else { // We own the video, we need to send a quick and dirty update to friends to notify the counts changed | ||
122 | const qadusParams = [] | ||
123 | |||
124 | if (likesToIncrement !== 0) { | ||
125 | qadusParams.push({ | ||
126 | videoId: videoInstance.id, | ||
127 | type: REQUEST_VIDEO_QADU_TYPES.LIKES | ||
128 | }) | ||
129 | } | ||
130 | |||
131 | if (dislikesToIncrement !== 0) { | ||
132 | qadusParams.push({ | ||
133 | videoId: videoInstance.id, | ||
134 | type: REQUEST_VIDEO_QADU_TYPES.DISLIKES | ||
135 | }) | ||
136 | } | ||
137 | |||
138 | await quickAndDirtyUpdatesVideoToFriends(qadusParams, t) | ||
139 | } | 89 | } |
140 | }) | 90 | }) |
141 | 91 | ||
142 | logger.info('User video rate for video %s of user %s updated.', videoInstance.name, userInstance.username) | 92 | logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name) |
143 | } | 93 | } |