aboutsummaryrefslogtreecommitdiff
path: root/lib/serve.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/serve.js')
-rw-r--r--lib/serve.js63
1 files changed, 40 insertions, 23 deletions
diff --git a/lib/serve.js b/lib/serve.js
index eef11d0..dc20141 100644
--- a/lib/serve.js
+++ b/lib/serve.js
@@ -103,17 +103,49 @@ Serve.prototype.go = function () {
} else {
gotData(null, {})
}
+
function gotData(err, data) {
+ self.data = data
+ if (err) next(err)
+ else if (data.action === 'publish') self.publish(next)
+ else next()
+ }
+
+ function next(err) {
if (err) {
- self.req.writeHead(400, {'Content-Type': 'text/plain'})
- self.req.end(err.stack)
+ self.res.writeHead(400, {'Content-Type': 'text/plain'})
+ self.res.end(err.stack)
} else {
- self.data = data
self.handle()
}
}
}
+Serve.prototype.publish = function (cb) {
+ var self = this
+ var content
+ try {
+ content = JSON.parse(self.data.content)
+ } catch(e) {
+ return cb(e)
+ }
+ var done = multicb({pluck: 1, spread: true})
+ u.toArray(content && content.mentions).forEach(function (mention) {
+ if (mention.link && mention.link[0] === '&' && !isNaN(mention.size))
+ self.app.pushBlob(mention.link, done())
+ })
+ done(function (err) {
+ if (err) return cb(err)
+ self.app.publish(content, function (err, msg) {
+ if (err) return cb(err)
+ delete self.data.text
+ delete self.data.recps
+ self.publishedMsg = msg
+ return cb()
+ })
+ })
+}
+
Serve.prototype.handle = function () {
var m = urlIdRegex.exec(this.req.url)
this.query = m[5] ? qs.parse(m[5]) : {}
@@ -676,6 +708,10 @@ Serve.prototype.wrapPage = function (title, searchQ) {
// h('a', {href: '/friends'}, 'friends'), ' ',
// h('a', {href: '/git'}, 'git')
)),
+ self.publishedMsg ? h('div',
+ 'published:',
+ self.app.render.msgLink(self.publishedMsg, done())
+ ) : '',
content
)))
done(cb)
@@ -877,8 +913,7 @@ Serve.prototype.composer = function (opts, cb) {
h('input', {name: 'blob_type', value: data.upload.type})
] : '',
data.action === 'preview' ? preview(false, done()) :
- data.action === 'raw' ? preview(true, done()) :
- data.action === 'publish' ? publish(done()) : ''
+ data.action === 'raw' ? preview(true, done()) : ''
)
))
done(cb)
@@ -969,22 +1004,4 @@ Serve.prototype.composer = function (opts, cb) {
]
}
- function publish(cb) {
- var content
- try {
- content = JSON.parse(self.data.content)
- } catch(e) {
- return cb(), u.renderError(e)
- }
- var done = multicb({pluck: 1, spread: true})
- u.toArray(content && content.mentions).forEach(function (mention) {
- if (mention.link && mention.link[0] === '&' && !isNaN(mention.size))
- self.app.pushBlob(mention.link, done())
- })
- done(function (err) {
- if (err) return cb(err)
- self.app.render.publish(content, cb)
- })
- }
-
}