aboutsummaryrefslogtreecommitdiff
path: root/lib/serve.js
diff options
context:
space:
mode:
authorcel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2017-03-10 10:14:41 -0500
committercel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2017-03-10 10:14:56 -0500
commit552d2797bbc61a5a2397d43b03e358f7a9004590 (patch)
treed34449d8c78937f5f8bfc9c0288a3acfaeaf663e /lib/serve.js
parentfc6c226031f569bd611380beff11fb0dd3f71093 (diff)
downloadpatchfoo-552d2797bbc61a5a2397d43b03e358f7a9004590.tar.gz
patchfoo-552d2797bbc61a5a2397d43b03e358f7a9004590.zip
Publish message before rendering page
and show it at the top of the page
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)
- })
- }
-
}