3 const eachSeries
= require('async/eachSeries')
4 const express
= require('express')
5 const waterfall
= require('async/waterfall')
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')
15 const router
= express
.Router()
18 signatureValidators
.signature
,
19 secureMiddleware
.checkSignature
,
20 videosValidators
.remoteVideos
,
24 // ---------------------------------------------------------------------------
26 module
.exports
= router
28 // ---------------------------------------------------------------------------
30 function remoteVideos (req
, res
, next
) {
31 const requests
= req
.body
.data
32 const fromPod
= res
.locals
.secure
.pod
34 // We need to process in the same order to keep consistency
36 eachSeries(requests
, function (request
, callbackEach
) {
37 const data
= request
.data
39 switch (request
.type
) {
41 addRemoteVideoRetryWrapper(data
, fromPod
, callbackEach
)
45 updateRemoteVideoRetryWrapper(data
, fromPod
, callbackEach
)
49 removeRemoteVideo(data
, fromPod
, callbackEach
)
53 reportAbuseRemoteVideo(data
, fromPod
, callbackEach
)
57 logger
.error('Unkown remote request type %s.', request
.type
)
60 if (err
) logger
.error('Error managing remote videos.', { error: err
})
63 // We don't need to keep the other pod waiting
64 return res
.type('json').status(204).end()
67 // Handle retries on fail
68 function addRemoteVideoRetryWrapper (videoToCreateData
, fromPod
, finalCallback
) {
70 arguments: [ videoToCreateData
, fromPod
],
71 errorMessage: 'Cannot insert the remote video with many retries.'
74 databaseUtils
.retryTransactionWrapper(addRemoteVideo
, options
, finalCallback
)
77 function addRemoteVideo (videoToCreateData
, fromPod
, finalCallback
) {
78 logger
.debug('Adding remote video "%s".', videoToCreateData
.remoteId
)
82 databaseUtils
.startSerializableTransaction
,
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
90 db
.Author
.findOrCreateAuthor(name
, podId
, userId
, t
, function (err
, authorInstance
) {
91 return callback(err
, t
, authorInstance
)
95 function findOrCreateTags (t
, author
, callback
) {
96 const tags
= videoToCreateData
.tags
98 db
.Tag
.findOrCreateTags(tags
, t
, function (err
, tagInstances
) {
99 return callback(err
, t
, author
, tagInstances
)
103 function createVideoObject (t
, author
, tagInstances
, callback
) {
105 name: videoToCreateData
.name
,
106 remoteId: videoToCreateData
.remoteId
,
107 extname: videoToCreateData
.extname
,
108 infoHash: videoToCreateData
.infoHash
,
109 description: videoToCreateData
.description
,
111 duration: videoToCreateData
.duration
,
112 createdAt: videoToCreateData
.createdAt
,
113 // FIXME: updatedAt does not seems to be considered by Sequelize
114 updatedAt: videoToCreateData
.updatedAt
117 const video
= db
.Video
.build(videoData
)
119 return callback(null, t
, tagInstances
, video
)
122 function generateThumbnail (t
, tagInstances
, video
, callback
) {
123 db
.Video
.generateThumbnailFromData(video
, videoToCreateData
.thumbnailData
, function (err
) {
125 logger
.error('Cannot generate thumbnail from data.', { error: err
})
129 return callback(err
, t
, tagInstances
, video
)
133 function insertVideoIntoDB (t
, tagInstances
, video
, callback
) {
138 video
.save(options
).asCallback(function (err
, videoCreated
) {
139 return callback(err
, t
, tagInstances
, videoCreated
)
143 function associateTagsToVideo (t
, tagInstances
, video
, callback
) {
144 const options
= { transaction: t
}
146 video
.setTags(tagInstances
, options
).asCallback(function (err
) {
147 return callback(err
, t
)
151 ], function (err
, t
) {
153 // This is just a debug because we will retry the insert
154 logger
.debug('Cannot insert the remote video.', { error: err
})
156 // Abort transaction?
159 return finalCallback(err
)
162 // Commit transaction
163 t
.commit().asCallback(function (err
) {
164 if (err
) return finalCallback(err
)
166 logger
.info('Remote video %s inserted.', videoToCreateData
.name
)
167 return finalCallback(null)
172 // Handle retries on fail
173 function updateRemoteVideoRetryWrapper (videoAttributesToUpdate
, fromPod
, finalCallback
) {
175 arguments: [ fromPod
, videoAttributesToUpdate
],
176 errorMessage: 'Cannot update the remote video with many retries'
179 databaseUtils
.retryWrapper(updateRemoteVideo
, options
, finalCallback
)
182 function updateRemoteVideo (videoAttributesToUpdate
, fromPod
, finalCallback
) {
183 logger
.debug('Updating remote video "%s".', videoAttributesToUpdate
.remoteId
)
187 databaseUtils
.startSerializableTransaction
,
189 function findVideo (t
, callback
) {
190 fetchVideo(fromPod
.host
, videoAttributesToUpdate
.remoteId
, function (err
, videoInstance
) {
191 return callback(err
, t
, videoInstance
)
195 function findOrCreateTags (t
, videoInstance
, callback
) {
196 const tags
= videoAttributesToUpdate
.tags
198 db
.Tag
.findOrCreateTags(tags
, t
, function (err
, tagInstances
) {
199 return callback(err
, t
, videoInstance
, tagInstances
)
203 function updateVideoIntoDB (t
, videoInstance
, tagInstances
, callback
) {
204 const options
= { transaction: t
}
206 videoInstance
.set('name', videoAttributesToUpdate
.name
)
207 videoInstance
.set('description', videoAttributesToUpdate
.description
)
208 videoInstance
.set('infoHash', videoAttributesToUpdate
.infoHash
)
209 videoInstance
.set('duration', videoAttributesToUpdate
.duration
)
210 videoInstance
.set('createdAt', videoAttributesToUpdate
.createdAt
)
211 videoInstance
.set('updatedAt', videoAttributesToUpdate
.updatedAt
)
212 videoInstance
.set('extname', videoAttributesToUpdate
.extname
)
214 videoInstance
.save(options
).asCallback(function (err
) {
215 return callback(err
, t
, videoInstance
, tagInstances
)
219 function associateTagsToVideo (t
, videoInstance
, tagInstances
, callback
) {
220 const options
= { transaction: t
}
222 videoInstance
.setTags(tagInstances
, options
).asCallback(function (err
) {
223 return callback(err
, t
)
227 ], function (err
, t
) {
229 // This is just a debug because we will retry the insert
230 logger
.debug('Cannot update the remote video.', { error: err
})
232 // Abort transaction?
235 return finalCallback(err
)
238 // Commit transaction
239 t
.commit().asCallback(function (err
) {
240 if (err
) return finalCallback(err
)
242 logger
.info('Remote video %s updated', videoAttributesToUpdate
.name
)
243 return finalCallback(null)
248 function removeRemoteVideo (videoToRemoveData
, fromPod
, callback
) {
249 // We need the instance because we have to remove some other stuffs (thumbnail etc)
250 fetchVideo(fromPod
.host
, videoToRemoveData
.remoteId
, function (err
, video
) {
251 // Do not return the error, continue the process
252 if (err
) return callback(null)
254 logger
.debug('Removing remote video %s.', video
.remoteId
)
255 video
.destroy().asCallback(function (err
) {
256 // Do not return the error, continue the process
258 logger
.error('Cannot remove remote video with id %s.', videoToRemoveData
.remoteId
, { error: err
})
261 return callback(null)
266 function reportAbuseRemoteVideo (reportData
, fromPod
, callback
) {
267 db
.Video
.load(reportData
.videoRemoteId
, function (err
, video
) {
269 if (!err
) err
= new Error('video not found')
271 logger
.error('Cannot load video from id.', { error: err
, id: reportData
.videoRemoteId
})
272 // Do not return the error, continue the process
273 return callback(null)
276 logger
.debug('Reporting remote abuse for video %s.', video
.id
)
278 const videoAbuseData
= {
279 reporterUsername: reportData
.reporterUsername
,
280 reason: reportData
.reportReason
,
281 reporterPodId: fromPod
.id
,
285 db
.VideoAbuse
.create(videoAbuseData
).asCallback(function (err
) {
287 logger
.error('Cannot create remote abuse video.', { error: err
})
290 return callback(null)
295 function fetchVideo (podHost
, remoteId
, callback
) {
296 db
.Video
.loadByHostAndRemoteId(podHost
, remoteId
, function (err
, video
) {
298 if (!err
) err
= new Error('video not found')
300 logger
.error('Cannot load video from host and remote id.', { error: err
, podHost
, remoteId
})
304 return callback(null, video
)