diff options
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/api/remote.js | 79 | ||||
-rw-r--r-- | server/controllers/api/videos.js | 46 |
2 files changed, 22 insertions, 103 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 | ||
29 | function remoteVideos (req, res, next) { | 29 | function 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 | ||
53 | function addRemoteVideo (videoToCreateData, fromHost, finalCallback) { | 53 | function 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 | ||
195 | function removeRemoteVideo (videoToRemoveData, fromHost, callback) { | 146 | function 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) { |
diff --git a/server/controllers/api/videos.js b/server/controllers/api/videos.js index ddf85d77d..f29edac74 100644 --- a/server/controllers/api/videos.js +++ b/server/controllers/api/videos.js | |||
@@ -1,6 +1,5 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const each = require('async/each') | ||
4 | const express = require('express') | 3 | const express = require('express') |
5 | const fs = require('fs') | 4 | const fs = require('fs') |
6 | const multer = require('multer') | 5 | const multer = require('multer') |
@@ -97,51 +96,20 @@ function addVideo (req, res, next) { | |||
97 | function findOrCreateAuthor (t, callback) { | 96 | function findOrCreateAuthor (t, callback) { |
98 | const user = res.locals.oauth.token.User | 97 | const user = res.locals.oauth.token.User |
99 | 98 | ||
100 | const query = { | 99 | const name = user.username |
101 | where: { | 100 | // null because it is OUR pod |
102 | name: user.username, | 101 | const podId = null |
103 | podId: null, | 102 | const userId = user.id |
104 | userId: user.id | ||
105 | }, | ||
106 | defaults: { | ||
107 | name: user.username, | ||
108 | podId: null, // null because it is OUR pod | ||
109 | userId: user.id | ||
110 | }, | ||
111 | transaction: t | ||
112 | } | ||
113 | |||
114 | db.Author.findOrCreate(query).asCallback(function (err, result) { | ||
115 | const authorInstance = result[0] | ||
116 | 103 | ||
104 | db.Author.findOrCreateAuthor(name, podId, userId, t, function (err, authorInstance) { | ||
117 | return callback(err, t, authorInstance) | 105 | return callback(err, t, authorInstance) |
118 | }) | 106 | }) |
119 | }, | 107 | }, |
120 | 108 | ||
121 | function findOrCreateTags (t, author, callback) { | 109 | function findOrCreateTags (t, author, callback) { |
122 | const tags = videoInfos.tags | 110 | const tags = videoInfos.tags |
123 | const tagInstances = [] | 111 | |
124 | 112 | db.Tag.findOrCreateTags(tags, t, function (err, tagInstances) { | |
125 | each(tags, function (tag, callbackEach) { | ||
126 | const query = { | ||
127 | where: { | ||
128 | name: tag | ||
129 | }, | ||
130 | defaults: { | ||
131 | name: tag | ||
132 | }, | ||
133 | transaction: t | ||
134 | } | ||
135 | |||
136 | db.Tag.findOrCreate(query).asCallback(function (err, res) { | ||
137 | if (err) return callbackEach(err) | ||
138 | |||
139 | // res = [ tag, isCreated ] | ||
140 | const tag = res[0] | ||
141 | tagInstances.push(tag) | ||
142 | return callbackEach() | ||
143 | }) | ||
144 | }, function (err) { | ||
145 | return callback(err, t, author, tagInstances) | 113 | return callback(err, t, author, tagInstances) |
146 | }) | 114 | }) |
147 | }, | 115 | }, |