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