summaryrefslogtreecommitdiff
path: root/bucky/app/federate.js
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2017-12-15 07:31:57 +0100
committerJules Laplace <julescarbon@gmail.com>2017-12-15 07:31:57 +0100
commit0aef20e45046858c198a7854499aeb80f39aaaec (patch)
treeb2525c6e588c9244646a8283eba73400162a1f1e /bucky/app/federate.js
parenta1b33089877660ba33331d76281e68790f35ae44 (diff)
federate high up in the foodchain
Diffstat (limited to 'bucky/app/federate.js')
-rw-r--r--bucky/app/federate.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/bucky/app/federate.js b/bucky/app/federate.js
new file mode 100644
index 0000000..56550fc
--- /dev/null
+++ b/bucky/app/federate.js
@@ -0,0 +1,60 @@
+var fetch = require('fetch')
+var db = require('../db')
+
+module.exports = {
+
+ route: (app) => {
+ app.put('/raw/import/thread/', importRaw(db.Thread), (req, res) => res.send({ status: 'ok' }))
+ app.put('/raw/import/keyword/', importRaw(db.Keyword), (req, res) => res.send({ status: 'ok' }))
+ app.put('/raw/import/file/', importRaw(db.File), (req, res) => res.send({ status: 'ok' }))
+ app.put('/raw/import/comment/', importRaw(db.Comment), (req, res) => res.send({ status: 'ok' }))
+
+ app.get('/raw/export/thread/:id', exportThread, (req, res) => res.send({ status: 'ok' }))
+ app.get('/raw/export/keyword/:id', exportKeyword, (req, res) => res.send({ status: 'ok' }))
+
+ function importRaw (model) {
+ return (req, res, next) => {
+ new model(req.body).save().then((el) => {
+ res.el = el;
+ next()
+ })
+ }
+ }
+ function exportKeyword (req, res, next) {
+ db.getKeyword(req.params.keyword).then(keyword => {
+ send("keyword", keyword)
+ return db.getThreadsForKeyword(keyword)
+ }).then(threads => {
+ var promises = threads.map(thread => {
+ exportThread({ params: { id: thread.get('id') } }, res, function(){})
+ })
+ return Promise.all(promises)
+ }).then( () => {
+ next()
+ })
+ }
+ function exportThread (req, res, next) {
+ return db.getThread(req.params.id).then(thread => {
+ send("thread", thread)
+ return db.getCommentsForThread(req.params.id)
+ }).then(comments => {
+ var promises = comments.map(comment => send("comment", comment))
+ return Promise.all(promises)
+ }).then( () => {
+ return db.getFilesForThread(req.params.id)
+ }).then(files => {
+ var promises = files.map(comment => send("comment", comment))
+ return promises
+ }).then( () => {
+ next()
+ })
+ }
+ function send(type, data){
+ var json = data.toJSON()
+ fetch("https://bucky.asdf.us/raw/import/" + type, {
+ method: 'PUT',
+ }).then(() => {})
+ }
+
+ }
+} \ No newline at end of file