diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-07-10 19:43:21 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-07-10 19:43:21 +0200 |
commit | 4771e0008dd26eadbb7eaff64255a6ec914fdadb (patch) | |
tree | 4fd58f8a3f3c2d674b936c99817b4f5fb958c5d8 /server/controllers/api | |
parent | 7a214f746bf420defbf17fa218d90d6233551bf8 (diff) | |
download | PeerTube-4771e0008dd26eadbb7eaff64255a6ec914fdadb.tar.gz PeerTube-4771e0008dd26eadbb7eaff64255a6ec914fdadb.tar.zst PeerTube-4771e0008dd26eadbb7eaff64255a6ec914fdadb.zip |
Better typescript typing for a better world
Diffstat (limited to 'server/controllers/api')
-rw-r--r-- | server/controllers/api/remote/pods.ts | 8 | ||||
-rw-r--r-- | server/controllers/api/remote/videos.ts | 43 | ||||
-rw-r--r-- | server/controllers/api/users.ts | 16 | ||||
-rw-r--r-- | server/controllers/api/videos/abuse.ts | 4 | ||||
-rw-r--r-- | server/controllers/api/videos/index.ts | 5 | ||||
-rw-r--r-- | server/controllers/api/videos/rate.ts | 23 |
6 files changed, 65 insertions, 34 deletions
diff --git a/server/controllers/api/remote/pods.ts b/server/controllers/api/remote/pods.ts index 6319957d3..69bbd4378 100644 --- a/server/controllers/api/remote/pods.ts +++ b/server/controllers/api/remote/pods.ts | |||
@@ -2,6 +2,7 @@ import * as express from 'express' | |||
2 | 2 | ||
3 | import { database as db } from '../../../initializers/database' | 3 | import { database as db } from '../../../initializers/database' |
4 | import { checkSignature, signatureValidator } from '../../../middlewares' | 4 | import { checkSignature, signatureValidator } from '../../../middlewares' |
5 | import { PodSignature } from '../../../../shared' | ||
5 | 6 | ||
6 | const remotePodsRouter = express.Router() | 7 | const remotePodsRouter = express.Router() |
7 | 8 | ||
@@ -21,12 +22,11 @@ export { | |||
21 | // --------------------------------------------------------------------------- | 22 | // --------------------------------------------------------------------------- |
22 | 23 | ||
23 | function removePods (req: express.Request, res: express.Response, next: express.NextFunction) { | 24 | function removePods (req: express.Request, res: express.Response, next: express.NextFunction) { |
24 | const host = req.body.signature.host | 25 | const signature: PodSignature = req.body.signature |
26 | const host = signature.host | ||
25 | 27 | ||
26 | db.Pod.loadByHost(host) | 28 | db.Pod.loadByHost(host) |
27 | .then(pod => { | 29 | .then(pod => pod.destroy()) |
28 | return pod.destroy() | ||
29 | }) | ||
30 | .then(() => res.type('json').status(204).end()) | 30 | .then(() => res.type('json').status(204).end()) |
31 | .catch(err => next(err)) | 31 | .catch(err => next(err)) |
32 | } | 32 | } |
diff --git a/server/controllers/api/remote/videos.ts b/server/controllers/api/remote/videos.ts index fac85c3a8..96eab6d52 100644 --- a/server/controllers/api/remote/videos.ts +++ b/server/controllers/api/remote/videos.ts | |||
@@ -18,6 +18,17 @@ import { | |||
18 | import { logger, retryTransactionWrapper } from '../../../helpers' | 18 | import { logger, retryTransactionWrapper } from '../../../helpers' |
19 | import { quickAndDirtyUpdatesVideoToFriends } from '../../../lib' | 19 | import { quickAndDirtyUpdatesVideoToFriends } from '../../../lib' |
20 | import { PodInstance, VideoInstance } from '../../../models' | 20 | import { PodInstance, VideoInstance } from '../../../models' |
21 | import { | ||
22 | RemoteVideoRequest, | ||
23 | RemoteVideoCreateData, | ||
24 | RemoteVideoUpdateData, | ||
25 | RemoteVideoRemoveData, | ||
26 | RemoteVideoReportAbuseData, | ||
27 | RemoteQaduVideoRequest, | ||
28 | RemoteQaduVideoData, | ||
29 | RemoteVideoEventRequest, | ||
30 | RemoteVideoEventData | ||
31 | } from '../../../../shared' | ||
21 | 32 | ||
22 | const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] | 33 | const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] |
23 | 34 | ||
@@ -60,11 +71,11 @@ export { | |||
60 | // --------------------------------------------------------------------------- | 71 | // --------------------------------------------------------------------------- |
61 | 72 | ||
62 | function remoteVideos (req: express.Request, res: express.Response, next: express.NextFunction) { | 73 | function remoteVideos (req: express.Request, res: express.Response, next: express.NextFunction) { |
63 | const requests = req.body.data | 74 | const requests: RemoteVideoRequest[] = req.body.data |
64 | const fromPod = res.locals.secure.pod | 75 | const fromPod = res.locals.secure.pod |
65 | 76 | ||
66 | // We need to process in the same order to keep consistency | 77 | // We need to process in the same order to keep consistency |
67 | Promise.each(requests, (request: any) => { | 78 | Promise.each(requests, request => { |
68 | const data = request.data | 79 | const data = request.data |
69 | 80 | ||
70 | // Get the function we need to call in order to process the request | 81 | // Get the function we need to call in order to process the request |
@@ -83,10 +94,10 @@ function remoteVideos (req: express.Request, res: express.Response, next: expres | |||
83 | } | 94 | } |
84 | 95 | ||
85 | function remoteVideosQadu (req: express.Request, res: express.Response, next: express.NextFunction) { | 96 | function remoteVideosQadu (req: express.Request, res: express.Response, next: express.NextFunction) { |
86 | const requests = req.body.data | 97 | const requests: RemoteQaduVideoRequest[] = req.body.data |
87 | const fromPod = res.locals.secure.pod | 98 | const fromPod = res.locals.secure.pod |
88 | 99 | ||
89 | Promise.each(requests, (request: any) => { | 100 | Promise.each(requests, request => { |
90 | const videoData = request.data | 101 | const videoData = request.data |
91 | 102 | ||
92 | return quickAndDirtyUpdateVideoRetryWrapper(videoData, fromPod) | 103 | return quickAndDirtyUpdateVideoRetryWrapper(videoData, fromPod) |
@@ -97,10 +108,10 @@ function remoteVideosQadu (req: express.Request, res: express.Response, next: ex | |||
97 | } | 108 | } |
98 | 109 | ||
99 | function remoteVideosEvents (req: express.Request, res: express.Response, next: express.NextFunction) { | 110 | function remoteVideosEvents (req: express.Request, res: express.Response, next: express.NextFunction) { |
100 | const requests = req.body.data | 111 | const requests: RemoteVideoEventRequest[] = req.body.data |
101 | const fromPod = res.locals.secure.pod | 112 | const fromPod = res.locals.secure.pod |
102 | 113 | ||
103 | Promise.each(requests, (request: any) => { | 114 | Promise.each(requests, request => { |
104 | const eventData = request.data | 115 | const eventData = request.data |
105 | 116 | ||
106 | return processVideosEventsRetryWrapper(eventData, fromPod) | 117 | return processVideosEventsRetryWrapper(eventData, fromPod) |
@@ -110,7 +121,7 @@ function remoteVideosEvents (req: express.Request, res: express.Response, next: | |||
110 | return res.type('json').status(204).end() | 121 | return res.type('json').status(204).end() |
111 | } | 122 | } |
112 | 123 | ||
113 | function processVideosEventsRetryWrapper (eventData: any, fromPod: PodInstance) { | 124 | function processVideosEventsRetryWrapper (eventData: RemoteVideoEventData, fromPod: PodInstance) { |
114 | const options = { | 125 | const options = { |
115 | arguments: [ eventData, fromPod ], | 126 | arguments: [ eventData, fromPod ], |
116 | errorMessage: 'Cannot process videos events with many retries.' | 127 | errorMessage: 'Cannot process videos events with many retries.' |
@@ -119,7 +130,7 @@ function processVideosEventsRetryWrapper (eventData: any, fromPod: PodInstance) | |||
119 | return retryTransactionWrapper(processVideosEvents, options) | 130 | return retryTransactionWrapper(processVideosEvents, options) |
120 | } | 131 | } |
121 | 132 | ||
122 | function processVideosEvents (eventData: any, fromPod: PodInstance) { | 133 | function processVideosEvents (eventData: RemoteVideoEventData, fromPod: PodInstance) { |
123 | 134 | ||
124 | return db.sequelize.transaction(t => { | 135 | return db.sequelize.transaction(t => { |
125 | return fetchOwnedVideo(eventData.remoteId) | 136 | return fetchOwnedVideo(eventData.remoteId) |
@@ -172,7 +183,7 @@ function processVideosEvents (eventData: any, fromPod: PodInstance) { | |||
172 | }) | 183 | }) |
173 | } | 184 | } |
174 | 185 | ||
175 | function quickAndDirtyUpdateVideoRetryWrapper (videoData: any, fromPod: PodInstance) { | 186 | function quickAndDirtyUpdateVideoRetryWrapper (videoData: RemoteQaduVideoData, fromPod: PodInstance) { |
176 | const options = { | 187 | const options = { |
177 | arguments: [ videoData, fromPod ], | 188 | arguments: [ videoData, fromPod ], |
178 | errorMessage: 'Cannot update quick and dirty the remote video with many retries.' | 189 | errorMessage: 'Cannot update quick and dirty the remote video with many retries.' |
@@ -181,7 +192,7 @@ function quickAndDirtyUpdateVideoRetryWrapper (videoData: any, fromPod: PodInsta | |||
181 | return retryTransactionWrapper(quickAndDirtyUpdateVideo, options) | 192 | return retryTransactionWrapper(quickAndDirtyUpdateVideo, options) |
182 | } | 193 | } |
183 | 194 | ||
184 | function quickAndDirtyUpdateVideo (videoData: any, fromPod: PodInstance) { | 195 | function quickAndDirtyUpdateVideo (videoData: RemoteQaduVideoData, fromPod: PodInstance) { |
185 | let videoName | 196 | let videoName |
186 | 197 | ||
187 | return db.sequelize.transaction(t => { | 198 | return db.sequelize.transaction(t => { |
@@ -211,7 +222,7 @@ function quickAndDirtyUpdateVideo (videoData: any, fromPod: PodInstance) { | |||
211 | } | 222 | } |
212 | 223 | ||
213 | // Handle retries on fail | 224 | // Handle retries on fail |
214 | function addRemoteVideoRetryWrapper (videoToCreateData: any, fromPod: PodInstance) { | 225 | function addRemoteVideoRetryWrapper (videoToCreateData: RemoteVideoCreateData, fromPod: PodInstance) { |
215 | const options = { | 226 | const options = { |
216 | arguments: [ videoToCreateData, fromPod ], | 227 | arguments: [ videoToCreateData, fromPod ], |
217 | errorMessage: 'Cannot insert the remote video with many retries.' | 228 | errorMessage: 'Cannot insert the remote video with many retries.' |
@@ -220,7 +231,7 @@ function addRemoteVideoRetryWrapper (videoToCreateData: any, fromPod: PodInstanc | |||
220 | return retryTransactionWrapper(addRemoteVideo, options) | 231 | return retryTransactionWrapper(addRemoteVideo, options) |
221 | } | 232 | } |
222 | 233 | ||
223 | function addRemoteVideo (videoToCreateData: any, fromPod: PodInstance) { | 234 | function addRemoteVideo (videoToCreateData: RemoteVideoCreateData, fromPod: PodInstance) { |
224 | logger.debug('Adding remote video "%s".', videoToCreateData.remoteId) | 235 | logger.debug('Adding remote video "%s".', videoToCreateData.remoteId) |
225 | 236 | ||
226 | return db.sequelize.transaction(t => { | 237 | return db.sequelize.transaction(t => { |
@@ -293,7 +304,7 @@ function addRemoteVideo (videoToCreateData: any, fromPod: PodInstance) { | |||
293 | } | 304 | } |
294 | 305 | ||
295 | // Handle retries on fail | 306 | // Handle retries on fail |
296 | function updateRemoteVideoRetryWrapper (videoAttributesToUpdate: any, fromPod: PodInstance) { | 307 | function updateRemoteVideoRetryWrapper (videoAttributesToUpdate: RemoteVideoUpdateData, fromPod: PodInstance) { |
297 | const options = { | 308 | const options = { |
298 | arguments: [ videoAttributesToUpdate, fromPod ], | 309 | arguments: [ videoAttributesToUpdate, fromPod ], |
299 | errorMessage: 'Cannot update the remote video with many retries' | 310 | errorMessage: 'Cannot update the remote video with many retries' |
@@ -302,7 +313,7 @@ function updateRemoteVideoRetryWrapper (videoAttributesToUpdate: any, fromPod: P | |||
302 | return retryTransactionWrapper(updateRemoteVideo, options) | 313 | return retryTransactionWrapper(updateRemoteVideo, options) |
303 | } | 314 | } |
304 | 315 | ||
305 | function updateRemoteVideo (videoAttributesToUpdate: any, fromPod: PodInstance) { | 316 | function updateRemoteVideo (videoAttributesToUpdate: RemoteVideoUpdateData, fromPod: PodInstance) { |
306 | logger.debug('Updating remote video "%s".', videoAttributesToUpdate.remoteId) | 317 | logger.debug('Updating remote video "%s".', videoAttributesToUpdate.remoteId) |
307 | 318 | ||
308 | return db.sequelize.transaction(t => { | 319 | return db.sequelize.transaction(t => { |
@@ -346,7 +357,7 @@ function updateRemoteVideo (videoAttributesToUpdate: any, fromPod: PodInstance) | |||
346 | }) | 357 | }) |
347 | } | 358 | } |
348 | 359 | ||
349 | function removeRemoteVideo (videoToRemoveData: any, fromPod: PodInstance) { | 360 | function removeRemoteVideo (videoToRemoveData: RemoteVideoRemoveData, fromPod: PodInstance) { |
350 | // We need the instance because we have to remove some other stuffs (thumbnail etc) | 361 | // We need the instance because we have to remove some other stuffs (thumbnail etc) |
351 | return fetchRemoteVideo(fromPod.host, videoToRemoveData.remoteId) | 362 | return fetchRemoteVideo(fromPod.host, videoToRemoveData.remoteId) |
352 | .then(video => { | 363 | .then(video => { |
@@ -358,7 +369,7 @@ function removeRemoteVideo (videoToRemoveData: any, fromPod: PodInstance) { | |||
358 | }) | 369 | }) |
359 | } | 370 | } |
360 | 371 | ||
361 | function reportAbuseRemoteVideo (reportData: any, fromPod: PodInstance) { | 372 | function reportAbuseRemoteVideo (reportData: RemoteVideoReportAbuseData, fromPod: PodInstance) { |
362 | return fetchOwnedVideo(reportData.videoRemoteId) | 373 | return fetchOwnedVideo(reportData.videoRemoteId) |
363 | .then(video => { | 374 | .then(video => { |
364 | logger.debug('Reporting remote abuse for video %s.', video.id) | 375 | logger.debug('Reporting remote abuse for video %s.', video.id) |
diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts index 845facd55..e79480521 100644 --- a/server/controllers/api/users.ts +++ b/server/controllers/api/users.ts | |||
@@ -17,7 +17,7 @@ import { | |||
17 | setUsersSort, | 17 | setUsersSort, |
18 | token | 18 | token |
19 | } from '../../middlewares' | 19 | } from '../../middlewares' |
20 | import { UserVideoRate as FormatedUserVideoRate } from '../../../shared' | 20 | import { UserVideoRate as FormatedUserVideoRate, UserCreate, UserUpdate } from '../../../shared' |
21 | 21 | ||
22 | const usersRouter = express.Router() | 22 | const usersRouter = express.Router() |
23 | 23 | ||
@@ -78,10 +78,12 @@ export { | |||
78 | // --------------------------------------------------------------------------- | 78 | // --------------------------------------------------------------------------- |
79 | 79 | ||
80 | function createUser (req: express.Request, res: express.Response, next: express.NextFunction) { | 80 | function createUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
81 | const body: UserCreate = req.body | ||
82 | |||
81 | const user = db.User.build({ | 83 | const user = db.User.build({ |
82 | username: req.body.username, | 84 | username: body.username, |
83 | password: req.body.password, | 85 | password: body.password, |
84 | email: req.body.email, | 86 | email: body.email, |
85 | displayNSFW: false, | 87 | displayNSFW: false, |
86 | role: USER_ROLES.USER | 88 | role: USER_ROLES.USER |
87 | }) | 89 | }) |
@@ -132,10 +134,12 @@ function removeUser (req: express.Request, res: express.Response, next: express. | |||
132 | } | 134 | } |
133 | 135 | ||
134 | function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) { | 136 | function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
137 | const body: UserUpdate = req.body | ||
138 | |||
135 | db.User.loadByUsername(res.locals.oauth.token.user.username) | 139 | db.User.loadByUsername(res.locals.oauth.token.user.username) |
136 | .then(user => { | 140 | .then(user => { |
137 | if (req.body.password) user.password = req.body.password | 141 | if (body.password) user.password = body.password |
138 | if (req.body.displayNSFW !== undefined) user.displayNSFW = req.body.displayNSFW | 142 | if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW |
139 | 143 | ||
140 | return user.save() | 144 | return user.save() |
141 | }) | 145 | }) |
diff --git a/server/controllers/api/videos/abuse.ts b/server/controllers/api/videos/abuse.ts index 3b1b7f58e..7d2e3bcfb 100644 --- a/server/controllers/api/videos/abuse.ts +++ b/server/controllers/api/videos/abuse.ts | |||
@@ -17,6 +17,7 @@ import { | |||
17 | setPagination | 17 | setPagination |
18 | } from '../../../middlewares' | 18 | } from '../../../middlewares' |
19 | import { VideoInstance } from '../../../models' | 19 | import { VideoInstance } from '../../../models' |
20 | import { VideoAbuseCreate } from '../../../../shared' | ||
20 | 21 | ||
21 | const abuseVideoRouter = express.Router() | 22 | const abuseVideoRouter = express.Router() |
22 | 23 | ||
@@ -63,10 +64,11 @@ function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Respon | |||
63 | function reportVideoAbuse (req: express.Request, res: express.Response) { | 64 | function reportVideoAbuse (req: express.Request, res: express.Response) { |
64 | const videoInstance = res.locals.video | 65 | const videoInstance = res.locals.video |
65 | const reporterUsername = res.locals.oauth.token.User.username | 66 | const reporterUsername = res.locals.oauth.token.User.username |
67 | const body: VideoAbuseCreate = req.body | ||
66 | 68 | ||
67 | const abuse = { | 69 | const abuse = { |
68 | reporterUsername, | 70 | reporterUsername, |
69 | reason: req.body.reason, | 71 | reason: body.reason, |
70 | videoId: videoInstance.id, | 72 | videoId: videoInstance.id, |
71 | reporterPodId: null // This is our pod that reported this abuse | 73 | reporterPodId: null // This is our pod that reported this abuse |
72 | } | 74 | } |
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 3532c753e..4ae7ea2ed 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts | |||
@@ -39,6 +39,7 @@ import { | |||
39 | renamePromise | 39 | renamePromise |
40 | } from '../../../helpers' | 40 | } from '../../../helpers' |
41 | import { TagInstance } from '../../../models' | 41 | import { TagInstance } from '../../../models' |
42 | import { VideoCreate, VideoUpdate } from '../../../../shared' | ||
42 | 43 | ||
43 | import { abuseVideoRouter } from './abuse' | 44 | import { abuseVideoRouter } from './abuse' |
44 | import { blacklistRouter } from './blacklist' | 45 | import { blacklistRouter } from './blacklist' |
@@ -155,7 +156,7 @@ function addVideoRetryWrapper (req: express.Request, res: express.Response, next | |||
155 | } | 156 | } |
156 | 157 | ||
157 | function addVideo (req: express.Request, res: express.Response, videoFile: Express.Multer.File) { | 158 | function addVideo (req: express.Request, res: express.Response, videoFile: Express.Multer.File) { |
158 | const videoInfos = req.body | 159 | const videoInfos: VideoCreate = req.body |
159 | 160 | ||
160 | return db.sequelize.transaction(t => { | 161 | return db.sequelize.transaction(t => { |
161 | const user = res.locals.oauth.token.User | 162 | const user = res.locals.oauth.token.User |
@@ -257,7 +258,7 @@ function updateVideoRetryWrapper (req: express.Request, res: express.Response, n | |||
257 | function updateVideo (req: express.Request, res: express.Response) { | 258 | function updateVideo (req: express.Request, res: express.Response) { |
258 | const videoInstance = res.locals.video | 259 | const videoInstance = res.locals.video |
259 | const videoFieldsSave = videoInstance.toJSON() | 260 | const videoFieldsSave = videoInstance.toJSON() |
260 | const videoInfosToUpdate = req.body | 261 | const videoInfosToUpdate: VideoUpdate = req.body |
261 | 262 | ||
262 | return db.sequelize.transaction(t => { | 263 | return db.sequelize.transaction(t => { |
263 | let tagsPromise: Promise<TagInstance[]> | 264 | let tagsPromise: Promise<TagInstance[]> |
diff --git a/server/controllers/api/videos/rate.ts b/server/controllers/api/videos/rate.ts index dfb5a450f..8456cbaf2 100644 --- a/server/controllers/api/videos/rate.ts +++ b/server/controllers/api/videos/rate.ts | |||
@@ -1,4 +1,5 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import * as Promise from 'bluebird' | ||
2 | 3 | ||
3 | import { database as db } from '../../../initializers/database' | 4 | import { database as db } from '../../../initializers/database' |
4 | import { | 5 | import { |
@@ -18,6 +19,7 @@ import { | |||
18 | authenticate, | 19 | authenticate, |
19 | videoRateValidator | 20 | videoRateValidator |
20 | } from '../../../middlewares' | 21 | } from '../../../middlewares' |
22 | import { UserVideoRateUpdate, VideoRateType } from '../../../../shared' | ||
21 | 23 | ||
22 | const rateVideoRouter = express.Router() | 24 | const rateVideoRouter = express.Router() |
23 | 25 | ||
@@ -47,7 +49,8 @@ function rateVideoRetryWrapper (req: express.Request, res: express.Response, nex | |||
47 | } | 49 | } |
48 | 50 | ||
49 | function rateVideo (req: express.Request, res: express.Response) { | 51 | function rateVideo (req: express.Request, res: express.Response) { |
50 | const rateType = req.body.rating | 52 | const body: UserVideoRateUpdate = req.body |
53 | const rateType = body.rating | ||
51 | const videoInstance = res.locals.video | 54 | const videoInstance = res.locals.video |
52 | const userInstance = res.locals.oauth.token.User | 55 | const userInstance = res.locals.oauth.token.User |
53 | 56 | ||
@@ -62,24 +65,34 @@ function rateVideo (req: express.Request, res: express.Response) { | |||
62 | if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++ | 65 | if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++ |
63 | else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++ | 66 | else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++ |
64 | 67 | ||
68 | let promise: Promise<any> | ||
69 | |||
65 | // There was a previous rate, update it | 70 | // There was a previous rate, update it |
66 | if (previousRate) { | 71 | if (previousRate) { |
67 | // We will remove the previous rate, so we will need to remove it from the video attribute | 72 | // We will remove the previous rate, so we will need to remove it from the video attribute |
68 | if (previousRate.type === VIDEO_RATE_TYPES.LIKE) likesToIncrement-- | 73 | if (previousRate.type === VIDEO_RATE_TYPES.LIKE) likesToIncrement-- |
69 | else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement-- | 74 | else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement-- |
70 | 75 | ||
71 | previousRate.type = rateType | 76 | if (rateType === 'none') { // Destroy previous rate |
77 | promise = previousRate.destroy() | ||
78 | } else { // Update previous rate | ||
79 | previousRate.type = rateType as VideoRateType | ||
72 | 80 | ||
73 | return previousRate.save(options).then(() => ({ t, likesToIncrement, dislikesToIncrement })) | 81 | promise = previousRate.save() |
74 | } else { // There was not a previous rate, insert a new one | 82 | } |
83 | } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate | ||
75 | const query = { | 84 | const query = { |
76 | userId: userInstance.id, | 85 | userId: userInstance.id, |
77 | videoId: videoInstance.id, | 86 | videoId: videoInstance.id, |
78 | type: rateType | 87 | type: rateType |
79 | } | 88 | } |
80 | 89 | ||
81 | return db.UserVideoRate.create(query, options).then(() => ({ likesToIncrement, dislikesToIncrement })) | 90 | promise = db.UserVideoRate.create(query, options) |
91 | } else { | ||
92 | promise = Promise.resolve() | ||
82 | } | 93 | } |
94 | |||
95 | return promise.then(() => ({ likesToIncrement, dislikesToIncrement })) | ||
83 | }) | 96 | }) |
84 | .then(({ likesToIncrement, dislikesToIncrement }) => { | 97 | .then(({ likesToIncrement, dislikesToIncrement }) => { |
85 | const options = { transaction: t } | 98 | const options = { transaction: t } |