summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bucky/util/federate.js76
1 files changed, 39 insertions, 37 deletions
diff --git a/bucky/util/federate.js b/bucky/util/federate.js
index b782801..5181d85 100644
--- a/bucky/util/federate.js
+++ b/bucky/util/federate.js
@@ -30,45 +30,44 @@ module.exports = {
}
function exportKeyword (req, res, next) {
console.log('export keyword', req.params.keyword)
- db.getKeyword(req.params.keyword).then(keyword => {
- send("keyword", keyword)
- return db.getThreadsForKeyword(req.params.keyword)
- }).then(threads => {
- var promises = threads.map(thread => {
- exportThread({ params: { id: thread.get('id') } }, res, function(){})
- })
- return Promise.all(promises)
- }).then( () => {
- next()
- })
+ return db.getKeyword(
+ req.params.keyword
+ ).then(keyword => send("keyword", keyword)
+ ).then(res => db.getThreadsForKeyword(req.params.keyword)
+ ).then(threads => threads.reduce((promise, thread) => (
+ promise.then(result => exportThread({ params: { id: thread.get('id') } }, res, function(){}))
+ ), new Promise (resolve => resolve()))
+ ).then(() => next())
}
function exportThread (req, res, next) {
var thread_id
- return db.getThread(req.params.id).then(thread => {
- return send("thread", thread)
- }).then(json => {
- thread_id = json.el.id
- console.log('got thread id', thread_id)
+ return db.getThread(
+ req.params.id
+
+ ).then(thread => send("thread", thread)
+ ).then(json => {
+ console.log('got thread id', json.el.id)
return db.getCommentsForThread(req.params.id)
- }).then(comments => {
- var promises = comments.map(comment => {
- comment.set('thread', thread_id)
- send("comment", comment)
- })
- return Promise.all(promises)
- }).then( () => {
- return db.getFilesForThread(req.params.id)
- }).then(files => {
- var promises = files.map(file => {
- copyFileToS3(file, thread_id)
- file.set('thread', thread_id)
- file.set('storage', process.env.S3_BUCKET)
- return send("file", file)
- })
- return promises
- }).then( () => {
- next()
- }).catch(e => {
+
+ }).then(comments => comments.reduce((promise, comment) => (
+ promise.then(result => {
+ comment.set('thread', thread_id)
+ return send("comment", comment)
+ })
+ ), new Promise(resolve => resolve()))
+
+ ).then(() => db.getFilesForThread(req.params.id)
+ ).then(files => files.reduce((promise, file) => (
+ promise.then(result => {
+ copyFileToS3(file, thread_id)
+ file.set('thread', thread_id)
+ file.set('storage', process.env.S3_BUCKET)
+ return send("file", file)
+ })
+ ), new Promise(resolve => resolve()))
+
+ ).then(() => next()
+ ).catch(e => {
console.error(e)
next()
})
@@ -83,8 +82,11 @@ module.exports = {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
- }).then((res) => {return res.json()})
- then((json) => console.log(json))
+ }).then(res => res.json())
+ .then(json => {
+ console.log(json)
+ return json
+ })
}
function copyFileToS3(file, thread_id){
// since for now we are essentially backing up local files,