]>
Commit | Line | Data |
---|---|---|
528a9efa C |
1 | 'use strict' |
2 | ||
1a42c9e2 | 3 | const eachSeries = require('async/eachSeries') |
528a9efa | 4 | const express = require('express') |
feb4bdfd | 5 | const waterfall = require('async/waterfall') |
528a9efa | 6 | |
a6fd2b30 C |
7 | const db = require('../../../initializers/database') |
8 | const middlewares = require('../../../middlewares') | |
528a9efa | 9 | const secureMiddleware = middlewares.secure |
55fa55a9 C |
10 | const videosValidators = middlewares.validators.remote.videos |
11 | const signatureValidators = middlewares.validators.remote.signature | |
a6fd2b30 | 12 | const logger = require('../../../helpers/logger') |
ed04d94f | 13 | const utils = require('../../../helpers/utils') |
528a9efa C |
14 | |
15 | const router = express.Router() | |
16 | ||
a6fd2b30 | 17 | router.post('/', |
55fa55a9 | 18 | signatureValidators.signature, |
0eb78d53 | 19 | secureMiddleware.checkSignature, |
55fa55a9 | 20 | videosValidators.remoteVideos, |
528a9efa C |
21 | remoteVideos |
22 | ) | |
23 | ||
24 | // --------------------------------------------------------------------------- | |
25 | ||
26 | module.exports = router | |
27 | ||
28 | // --------------------------------------------------------------------------- | |
29 | ||
30 | function remoteVideos (req, res, next) { | |
31 | const requests = req.body.data | |
4ff0d862 | 32 | const fromPod = res.locals.secure.pod |
528a9efa C |
33 | |
34 | // We need to process in the same order to keep consistency | |
35 | // TODO: optimization | |
1a42c9e2 | 36 | eachSeries(requests, function (request, callbackEach) { |
55fa55a9 | 37 | const data = request.data |
528a9efa | 38 | |
3d118fb5 C |
39 | switch (request.type) { |
40 | case 'add': | |
ed04d94f | 41 | addRemoteVideoRetryWrapper(data, fromPod, callbackEach) |
3d118fb5 C |
42 | break |
43 | ||
44 | case 'update': | |
ed04d94f | 45 | updateRemoteVideoRetryWrapper(data, fromPod, callbackEach) |
3d118fb5 C |
46 | break |
47 | ||
48 | case 'remove': | |
55fa55a9 C |
49 | removeRemoteVideo(data, fromPod, callbackEach) |
50 | break | |
51 | ||
52 | case 'report-abuse': | |
53 | reportAbuseRemoteVideo(data, fromPod, callbackEach) | |
3d118fb5 C |
54 | break |
55 | ||
56 | default: | |
57 | logger.error('Unkown remote request type %s.', request.type) | |
528a9efa | 58 | } |
aaf61f38 C |
59 | }, function (err) { |
60 | if (err) logger.error('Error managing remote videos.', { error: err }) | |
528a9efa C |
61 | }) |
62 | ||
63 | // We don't need to keep the other pod waiting | |
64 | return res.type('json').status(204).end() | |
65 | } | |
66 | ||
ed04d94f C |
67 | // Handle retries on fail |
68 | function addRemoteVideoRetryWrapper (videoToCreateData, fromPod, finalCallback) { | |
d6a5b018 C |
69 | const options = { |
70 | arguments: [ videoToCreateData, fromPod ], | |
71 | errorMessage: 'Cannot insert the remote video with many retries.' | |
72 | } | |
ed04d94f | 73 | |
d6a5b018 | 74 | utils.retryWrapper(addRemoteVideo, options, finalCallback) |
ed04d94f C |
75 | } |
76 | ||
4ff0d862 | 77 | function addRemoteVideo (videoToCreateData, fromPod, finalCallback) { |
ed04d94f | 78 | logger.debug('Adding remote video "%s".', videoToCreateData.remoteId) |
6666aad4 | 79 | |
feb4bdfd C |
80 | waterfall([ |
81 | ||
7920c273 | 82 | function startTransaction (callback) { |
ed04d94f | 83 | db.sequelize.transaction({ isolationLevel: 'SERIALIZABLE' }).asCallback(function (err, t) { |
7920c273 C |
84 | return callback(err, t) |
85 | }) | |
86 | }, | |
87 | ||
4ff0d862 C |
88 | function findOrCreateAuthor (t, callback) { |
89 | const name = videoToCreateData.author | |
90 | const podId = fromPod.id | |
91 | // This author is from another pod so we do not associate a user | |
92 | const userId = null | |
feb4bdfd | 93 | |
4ff0d862 C |
94 | db.Author.findOrCreateAuthor(name, podId, userId, t, function (err, authorInstance) { |
95 | return callback(err, t, authorInstance) | |
feb4bdfd C |
96 | }) |
97 | }, | |
98 | ||
7920c273 C |
99 | function findOrCreateTags (t, author, callback) { |
100 | const tags = videoToCreateData.tags | |
7920c273 | 101 | |
4ff0d862 | 102 | db.Tag.findOrCreateTags(tags, t, function (err, tagInstances) { |
7920c273 C |
103 | return callback(err, t, author, tagInstances) |
104 | }) | |
105 | }, | |
106 | ||
107 | function createVideoObject (t, author, tagInstances, callback) { | |
feb4bdfd C |
108 | const videoData = { |
109 | name: videoToCreateData.name, | |
110 | remoteId: videoToCreateData.remoteId, | |
111 | extname: videoToCreateData.extname, | |
112 | infoHash: videoToCreateData.infoHash, | |
113 | description: videoToCreateData.description, | |
114 | authorId: author.id, | |
124648d7 | 115 | duration: videoToCreateData.duration, |
79066fdf | 116 | createdAt: videoToCreateData.createdAt, |
ed04d94f | 117 | // FIXME: updatedAt does not seems to be considered by Sequelize |
79066fdf | 118 | updatedAt: videoToCreateData.updatedAt |
feb4bdfd C |
119 | } |
120 | ||
121 | const video = db.Video.build(videoData) | |
122 | ||
7920c273 | 123 | return callback(null, t, tagInstances, video) |
feb4bdfd C |
124 | }, |
125 | ||
7920c273 | 126 | function generateThumbnail (t, tagInstances, video, callback) { |
4d324488 | 127 | db.Video.generateThumbnailFromData(video, videoToCreateData.thumbnailData, function (err) { |
feb4bdfd | 128 | if (err) { |
4d324488 | 129 | logger.error('Cannot generate thumbnail from data.', { error: err }) |
feb4bdfd C |
130 | return callback(err) |
131 | } | |
132 | ||
7920c273 | 133 | return callback(err, t, tagInstances, video) |
feb4bdfd C |
134 | }) |
135 | }, | |
136 | ||
7920c273 C |
137 | function insertVideoIntoDB (t, tagInstances, video, callback) { |
138 | const options = { | |
139 | transaction: t | |
140 | } | |
141 | ||
142 | video.save(options).asCallback(function (err, videoCreated) { | |
143 | return callback(err, t, tagInstances, videoCreated) | |
144 | }) | |
145 | }, | |
146 | ||
147 | function associateTagsToVideo (t, tagInstances, video, callback) { | |
148 | const options = { transaction: t } | |
149 | ||
150 | video.setTags(tagInstances, options).asCallback(function (err) { | |
151 | return callback(err, t) | |
152 | }) | |
c77fa067 C |
153 | } |
154 | ||
7920c273 C |
155 | ], function (err, t) { |
156 | if (err) { | |
ed04d94f C |
157 | // This is just a debug because we will retry the insert |
158 | logger.debug('Cannot insert the remote video.', { error: err }) | |
7920c273 C |
159 | |
160 | // Abort transaction? | |
161 | if (t) t.rollback() | |
162 | ||
163 | return finalCallback(err) | |
164 | } | |
165 | ||
166 | // Commit transaction | |
dea32aac C |
167 | t.commit().asCallback(function (err) { |
168 | if (err) return finalCallback(err) | |
7920c273 | 169 | |
d8cc063e | 170 | logger.info('Remote video %s inserted.', videoToCreateData.name) |
dea32aac C |
171 | return finalCallback(null) |
172 | }) | |
7920c273 | 173 | }) |
528a9efa C |
174 | } |
175 | ||
ed04d94f C |
176 | // Handle retries on fail |
177 | function updateRemoteVideoRetryWrapper (videoAttributesToUpdate, fromPod, finalCallback) { | |
d6a5b018 C |
178 | const options = { |
179 | arguments: [ fromPod, videoAttributesToUpdate ], | |
180 | errorMessage: 'Cannot update the remote video with many retries' | |
181 | } | |
ed04d94f | 182 | |
d6a5b018 | 183 | utils.retryWrapper(updateRemoteVideo, options, finalCallback) |
ed04d94f C |
184 | } |
185 | ||
3d118fb5 | 186 | function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) { |
ed04d94f | 187 | logger.debug('Updating remote video "%s".', videoAttributesToUpdate.remoteId) |
feb4bdfd | 188 | |
3d118fb5 C |
189 | waterfall([ |
190 | ||
191 | function startTransaction (callback) { | |
edc5e860 | 192 | db.sequelize.transaction({ isolationLevel: 'SERIALIZABLE' }).asCallback(function (err, t) { |
3d118fb5 C |
193 | return callback(err, t) |
194 | }) | |
195 | }, | |
196 | ||
197 | function findVideo (t, callback) { | |
55fa55a9 C |
198 | fetchVideo(fromPod.host, videoAttributesToUpdate.remoteId, function (err, videoInstance) { |
199 | return callback(err, t, videoInstance) | |
3d118fb5 C |
200 | }) |
201 | }, | |
202 | ||
203 | function findOrCreateTags (t, videoInstance, callback) { | |
204 | const tags = videoAttributesToUpdate.tags | |
205 | ||
206 | db.Tag.findOrCreateTags(tags, t, function (err, tagInstances) { | |
207 | return callback(err, t, videoInstance, tagInstances) | |
208 | }) | |
209 | }, | |
210 | ||
211 | function updateVideoIntoDB (t, videoInstance, tagInstances, callback) { | |
212 | const options = { transaction: t } | |
213 | ||
214 | videoInstance.set('name', videoAttributesToUpdate.name) | |
215 | videoInstance.set('description', videoAttributesToUpdate.description) | |
216 | videoInstance.set('infoHash', videoAttributesToUpdate.infoHash) | |
217 | videoInstance.set('duration', videoAttributesToUpdate.duration) | |
218 | videoInstance.set('createdAt', videoAttributesToUpdate.createdAt) | |
79066fdf | 219 | videoInstance.set('updatedAt', videoAttributesToUpdate.updatedAt) |
3d118fb5 C |
220 | videoInstance.set('extname', videoAttributesToUpdate.extname) |
221 | ||
222 | videoInstance.save(options).asCallback(function (err) { | |
223 | return callback(err, t, videoInstance, tagInstances) | |
224 | }) | |
225 | }, | |
226 | ||
227 | function associateTagsToVideo (t, videoInstance, tagInstances, callback) { | |
228 | const options = { transaction: t } | |
229 | ||
230 | videoInstance.setTags(tagInstances, options).asCallback(function (err) { | |
231 | return callback(err, t) | |
232 | }) | |
528a9efa C |
233 | } |
234 | ||
3d118fb5 C |
235 | ], function (err, t) { |
236 | if (err) { | |
ed04d94f C |
237 | // This is just a debug because we will retry the insert |
238 | logger.debug('Cannot update the remote video.', { error: err }) | |
3d118fb5 C |
239 | |
240 | // Abort transaction? | |
241 | if (t) t.rollback() | |
242 | ||
243 | return finalCallback(err) | |
6666aad4 C |
244 | } |
245 | ||
dea32aac C |
246 | // Commit transaction |
247 | t.commit().asCallback(function (err) { | |
248 | if (err) return finalCallback(err) | |
3d118fb5 | 249 | |
dea32aac C |
250 | logger.info('Remote video %s updated', videoAttributesToUpdate.name) |
251 | return finalCallback(null) | |
252 | }) | |
3d118fb5 C |
253 | }) |
254 | } | |
255 | ||
256 | function removeRemoteVideo (videoToRemoveData, fromPod, callback) { | |
257 | // We need the instance because we have to remove some other stuffs (thumbnail etc) | |
55fa55a9 | 258 | fetchVideo(fromPod.host, videoToRemoveData.remoteId, function (err, video) { |
d8cc063e C |
259 | // Do not return the error, continue the process |
260 | if (err) return callback(null) | |
55fa55a9 C |
261 | |
262 | logger.debug('Removing remote video %s.', video.remoteId) | |
d8cc063e C |
263 | video.destroy().asCallback(function (err) { |
264 | // Do not return the error, continue the process | |
265 | if (err) { | |
266 | logger.error('Cannot remove remote video with id %s.', videoToRemoveData.remoteId, { error: err }) | |
267 | } | |
268 | ||
269 | return callback(null) | |
270 | }) | |
55fa55a9 C |
271 | }) |
272 | } | |
273 | ||
274 | function reportAbuseRemoteVideo (reportData, fromPod, callback) { | |
275 | db.Video.load(reportData.videoRemoteId, function (err, video) { | |
3d118fb5 | 276 | if (err || !video) { |
55fa55a9 C |
277 | if (!err) err = new Error('video not found') |
278 | ||
ed04d94f | 279 | logger.error('Cannot load video from id.', { error: err, id: reportData.videoRemoteId }) |
d8cc063e C |
280 | // Do not return the error, continue the process |
281 | return callback(null) | |
3d118fb5 | 282 | } |
6666aad4 | 283 | |
55fa55a9 C |
284 | logger.debug('Reporting remote abuse for video %s.', video.id) |
285 | ||
286 | const videoAbuseData = { | |
287 | reporterUsername: reportData.reporterUsername, | |
288 | reason: reportData.reportReason, | |
289 | reporterPodId: fromPod.id, | |
290 | videoId: video.id | |
291 | } | |
292 | ||
d8cc063e C |
293 | db.VideoAbuse.create(videoAbuseData).asCallback(function (err) { |
294 | if (err) { | |
295 | logger.error('Cannot create remote abuse video.', { error: err }) | |
296 | } | |
297 | ||
298 | return callback(null) | |
299 | }) | |
55fa55a9 C |
300 | }) |
301 | } | |
302 | ||
303 | function fetchVideo (podHost, remoteId, callback) { | |
304 | db.Video.loadByHostAndRemoteId(podHost, remoteId, function (err, video) { | |
305 | if (err || !video) { | |
306 | if (!err) err = new Error('video not found') | |
307 | ||
ed04d94f | 308 | logger.error('Cannot load video from host and remote id.', { error: err, podHost, remoteId }) |
55fa55a9 C |
309 | return callback(err) |
310 | } | |
311 | ||
312 | return callback(null, video) | |
528a9efa C |
313 | }) |
314 | } |