]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/remote/videos.js
Server: transaction refractoring
[github/Chocobozzz/PeerTube.git] / server / controllers / api / remote / videos.js
CommitLineData
528a9efa
C
1'use strict'
2
1a42c9e2 3const eachSeries = require('async/eachSeries')
528a9efa 4const express = require('express')
feb4bdfd 5const waterfall = require('async/waterfall')
528a9efa 6
a6fd2b30
C
7const db = require('../../../initializers/database')
8const middlewares = require('../../../middlewares')
528a9efa 9const secureMiddleware = middlewares.secure
55fa55a9
C
10const videosValidators = middlewares.validators.remote.videos
11const signatureValidators = middlewares.validators.remote.signature
a6fd2b30 12const logger = require('../../../helpers/logger')
4df023f2 13const databaseUtils = require('../../../helpers/database-utils')
528a9efa
C
14
15const router = express.Router()
16
a6fd2b30 17router.post('/',
55fa55a9 18 signatureValidators.signature,
0eb78d53 19 secureMiddleware.checkSignature,
55fa55a9 20 videosValidators.remoteVideos,
528a9efa
C
21 remoteVideos
22)
23
24// ---------------------------------------------------------------------------
25
26module.exports = router
27
28// ---------------------------------------------------------------------------
29
30function 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
68function 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
4df023f2 74 databaseUtils.retryTransactionWrapper(addRemoteVideo, options, finalCallback)
ed04d94f
C
75}
76
4ff0d862 77function addRemoteVideo (videoToCreateData, fromPod, finalCallback) {
ed04d94f 78 logger.debug('Adding remote video "%s".', videoToCreateData.remoteId)
6666aad4 79
feb4bdfd
C
80 waterfall([
81
4df023f2 82 databaseUtils.startSerializableTransaction,
7920c273 83
4ff0d862
C
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
feb4bdfd 89
4ff0d862
C
90 db.Author.findOrCreateAuthor(name, podId, userId, t, function (err, authorInstance) {
91 return callback(err, t, authorInstance)
feb4bdfd
C
92 })
93 },
94
7920c273
C
95 function findOrCreateTags (t, author, callback) {
96 const tags = videoToCreateData.tags
7920c273 97
4ff0d862 98 db.Tag.findOrCreateTags(tags, t, function (err, tagInstances) {
7920c273
C
99 return callback(err, t, author, tagInstances)
100 })
101 },
102
103 function createVideoObject (t, author, tagInstances, callback) {
feb4bdfd
C
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,
124648d7 111 duration: videoToCreateData.duration,
79066fdf 112 createdAt: videoToCreateData.createdAt,
ed04d94f 113 // FIXME: updatedAt does not seems to be considered by Sequelize
79066fdf 114 updatedAt: videoToCreateData.updatedAt
feb4bdfd
C
115 }
116
117 const video = db.Video.build(videoData)
118
7920c273 119 return callback(null, t, tagInstances, video)
feb4bdfd
C
120 },
121
7920c273 122 function generateThumbnail (t, tagInstances, video, callback) {
4d324488 123 db.Video.generateThumbnailFromData(video, videoToCreateData.thumbnailData, function (err) {
feb4bdfd 124 if (err) {
4d324488 125 logger.error('Cannot generate thumbnail from data.', { error: err })
feb4bdfd
C
126 return callback(err)
127 }
128
7920c273 129 return callback(err, t, tagInstances, video)
feb4bdfd
C
130 })
131 },
132
7920c273
C
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 })
4145c1c6
C
149 },
150
151 databaseUtils.commitTransaction
c77fa067 152
7920c273
C
153 ], function (err, t) {
154 if (err) {
ed04d94f
C
155 // This is just a debug because we will retry the insert
156 logger.debug('Cannot insert the remote video.', { error: err })
4145c1c6 157 return databaseUtils.rollbackTransaction(err, t, finalCallback)
7920c273
C
158 }
159
4145c1c6
C
160 logger.info('Remote video %s inserted.', videoToCreateData.name)
161 return finalCallback(null)
7920c273 162 })
528a9efa
C
163}
164
ed04d94f
C
165// Handle retries on fail
166function updateRemoteVideoRetryWrapper (videoAttributesToUpdate, fromPod, finalCallback) {
d6a5b018 167 const options = {
fbc22d79 168 arguments: [ videoAttributesToUpdate, fromPod ],
d6a5b018
C
169 errorMessage: 'Cannot update the remote video with many retries'
170 }
ed04d94f 171
fbc22d79 172 databaseUtils.retryTransactionWrapper(updateRemoteVideo, options, finalCallback)
ed04d94f
C
173}
174
3d118fb5 175function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) {
ed04d94f 176 logger.debug('Updating remote video "%s".', videoAttributesToUpdate.remoteId)
feb4bdfd 177
3d118fb5
C
178 waterfall([
179
4df023f2 180 databaseUtils.startSerializableTransaction,
3d118fb5
C
181
182 function findVideo (t, callback) {
55fa55a9
C
183 fetchVideo(fromPod.host, videoAttributesToUpdate.remoteId, function (err, videoInstance) {
184 return callback(err, t, videoInstance)
3d118fb5
C
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)
79066fdf 204 videoInstance.set('updatedAt', videoAttributesToUpdate.updatedAt)
3d118fb5
C
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 })
4145c1c6
C
218 },
219
220 databaseUtils.commitTransaction
528a9efa 221
3d118fb5
C
222 ], function (err, t) {
223 if (err) {
ed04d94f
C
224 // This is just a debug because we will retry the insert
225 logger.debug('Cannot update the remote video.', { error: err })
4145c1c6 226 return databaseUtils.rollbackTransaction(err, t, finalCallback)
6666aad4
C
227 }
228
4145c1c6
C
229 logger.info('Remote video %s updated', videoAttributesToUpdate.name)
230 return finalCallback(null)
3d118fb5
C
231 })
232}
233
234function removeRemoteVideo (videoToRemoveData, fromPod, callback) {
235 // We need the instance because we have to remove some other stuffs (thumbnail etc)
55fa55a9 236 fetchVideo(fromPod.host, videoToRemoveData.remoteId, function (err, video) {
d8cc063e
C
237 // Do not return the error, continue the process
238 if (err) return callback(null)
55fa55a9
C
239
240 logger.debug('Removing remote video %s.', video.remoteId)
d8cc063e
C
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 })
55fa55a9
C
249 })
250}
251
252function reportAbuseRemoteVideo (reportData, fromPod, callback) {
253 db.Video.load(reportData.videoRemoteId, function (err, video) {
3d118fb5 254 if (err || !video) {
55fa55a9
C
255 if (!err) err = new Error('video not found')
256
ed04d94f 257 logger.error('Cannot load video from id.', { error: err, id: reportData.videoRemoteId })
d8cc063e
C
258 // Do not return the error, continue the process
259 return callback(null)
3d118fb5 260 }
6666aad4 261
55fa55a9
C
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
d8cc063e
C
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 })
55fa55a9
C
278 })
279}
280
281function 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
ed04d94f 286 logger.error('Cannot load video from host and remote id.', { error: err, podHost, remoteId })
55fa55a9
C
287 return callback(err)
288 }
289
290 return callback(null, video)
528a9efa
C
291 })
292}