aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/remote.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/remote.js')
-rw-r--r--server/controllers/api/remote.js79
1 files changed, 15 insertions, 64 deletions
diff --git a/server/controllers/api/remote.js b/server/controllers/api/remote.js
index ac850c2d2..929ade555 100644
--- a/server/controllers/api/remote.js
+++ b/server/controllers/api/remote.js
@@ -28,7 +28,7 @@ module.exports = router
28 28
29function remoteVideos (req, res, next) { 29function remoteVideos (req, res, next) {
30 const requests = req.body.data 30 const requests = req.body.data
31 const fromHost = req.body.signature.host 31 const fromPod = res.locals.secure.pod
32 32
33 // We need to process in the same order to keep consistency 33 // We need to process in the same order to keep consistency
34 // TODO: optimization 34 // TODO: optimization
@@ -36,9 +36,9 @@ function remoteVideos (req, res, next) {
36 const videoData = request.data 36 const videoData = request.data
37 37
38 if (request.type === 'add') { 38 if (request.type === 'add') {
39 addRemoteVideo(videoData, fromHost, callbackEach) 39 addRemoteVideo(videoData, fromPod, callbackEach)
40 } else if (request.type === 'remove') { 40 } else if (request.type === 'remove') {
41 removeRemoteVideo(videoData, fromHost, callbackEach) 41 removeRemoteVideo(videoData, fromPod, callbackEach)
42 } else { 42 } else {
43 logger.error('Unkown remote request type %s.', request.type) 43 logger.error('Unkown remote request type %s.', request.type)
44 } 44 }
@@ -50,7 +50,7 @@ function remoteVideos (req, res, next) {
50 return res.type('json').status(204).end() 50 return res.type('json').status(204).end()
51} 51}
52 52
53function addRemoteVideo (videoToCreateData, fromHost, finalCallback) { 53function addRemoteVideo (videoToCreateData, fromPod, finalCallback) {
54 logger.debug('Adding remote video "%s".', videoToCreateData.name) 54 logger.debug('Adding remote video "%s".', videoToCreateData.name)
55 55
56 waterfall([ 56 waterfall([
@@ -61,70 +61,21 @@ function addRemoteVideo (videoToCreateData, fromHost, finalCallback) {
61 }) 61 })
62 }, 62 },
63 63
64 function findOrCreatePod (t, callback) { 64 function findOrCreateAuthor (t, callback) {
65 const query = { 65 const name = videoToCreateData.author
66 where: { 66 const podId = fromPod.id
67 host: fromHost 67 // This author is from another pod so we do not associate a user
68 }, 68 const userId = null
69 defaults: {
70 host: fromHost
71 },
72 transaction: t
73 }
74
75 db.Pod.findOrCreate(query).asCallback(function (err, result) {
76 // [ instance, wasCreated ]
77 return callback(err, t, result[0])
78 })
79 },
80 69
81 function findOrCreateAuthor (t, pod, callback) { 70 db.Author.findOrCreateAuthor(name, podId, userId, t, function (err, authorInstance) {
82 const username = videoToCreateData.author 71 return callback(err, t, authorInstance)
83
84 const query = {
85 where: {
86 name: username,
87 podId: pod.id,
88 userId: null
89 },
90 defaults: {
91 name: username,
92 podId: pod.id,
93 userId: null
94 },
95 transaction: t
96 }
97
98 db.Author.findOrCreate(query).asCallback(function (err, result) {
99 // [ instance, wasCreated ]
100 return callback(err, t, result[0])
101 }) 72 })
102 }, 73 },
103 74
104 function findOrCreateTags (t, author, callback) { 75 function findOrCreateTags (t, author, callback) {
105 const tags = videoToCreateData.tags 76 const tags = videoToCreateData.tags
106 const tagInstances = []
107
108 each(tags, function (tag, callbackEach) {
109 const query = {
110 where: {
111 name: tag
112 },
113 defaults: {
114 name: tag
115 },
116 transaction: t
117 }
118
119 db.Tag.findOrCreate(query).asCallback(function (err, res) {
120 if (err) return callbackEach(err)
121 77
122 // res = [ tag, isCreated ] 78 db.Tag.findOrCreateTags(tags, t, function (err, tagInstances) {
123 const tag = res[0]
124 tagInstances.push(tag)
125 return callbackEach()
126 })
127 }, function (err) {
128 return callback(err, t, author, tagInstances) 79 return callback(err, t, author, tagInstances)
129 }) 80 })
130 }, 81 },
@@ -192,18 +143,18 @@ function addRemoteVideo (videoToCreateData, fromHost, finalCallback) {
192 }) 143 })
193} 144}
194 145
195function removeRemoteVideo (videoToRemoveData, fromHost, callback) { 146function removeRemoteVideo (videoToRemoveData, fromPod, callback) {
196 // TODO: use bulkDestroy? 147 // TODO: use bulkDestroy?
197 148
198 // We need the list because we have to remove some other stuffs (thumbnail etc) 149 // We need the list because we have to remove some other stuffs (thumbnail etc)
199 db.Video.listByHostAndRemoteId(fromHost, videoToRemoveData.remoteId, function (err, videosList) { 150 db.Video.listByHostAndRemoteId(fromPod.host, videoToRemoveData.remoteId, function (err, videosList) {
200 if (err) { 151 if (err) {
201 logger.error('Cannot list videos from host and remote id.', { error: err.message }) 152 logger.error('Cannot list videos from host and remote id.', { error: err.message })
202 return callback(err) 153 return callback(err)
203 } 154 }
204 155
205 if (videosList.length === 0) { 156 if (videosList.length === 0) {
206 logger.error('No remote video was found for this pod.', { remoteId: videoToRemoveData.remoteId, podHost: fromHost }) 157 logger.error('No remote video was found for this pod.', { remoteId: videoToRemoveData.remoteId, podHost: fromPod.host })
207 } 158 }
208 159
209 each(videosList, function (video, callbackEach) { 160 each(videosList, function (video, callbackEach) {