aboutsummaryrefslogtreecommitdiff
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
parentfc6c226031f569bd611380beff11fb0dd3f71093 (diff)
downloadpatchfoo-552d2797bbc61a5a2397d43b03e358f7a9004590.tar.gz
patchfoo-552d2797bbc61a5a2397d43b03e358f7a9004590.zip
Publish message before rendering page
and show it at the top of the page
-rw-r--r--lib/render.js23
-rw-r--r--lib/serve.js63
2 files changed, 48 insertions, 38 deletions
diff --git a/lib/render.js b/lib/render.js
index ed8cb1a..eaca499 100644
--- a/lib/render.js
+++ b/lib/render.js
@@ -197,24 +197,17 @@ Render.prototype.privateLine = function (recps, cb) {
return el
}
-Render.prototype.publish = function (content, cb) {
+Render.prototype.msgLink = function (msg, cb) {
var self = this
-
var el = h('div')
- self.app.publish(content, function (err, msg) {
+ self.app.unboxMsg(msg, function (err, msg) {
if (err) return el.appendChild(u.renderError(err)), cb()
- self.app.unboxMsg(msg, function (err, msg) {
- if (err) return el.appendChild(u.renderError(err)), cb()
- self.renderMsg(msg, false, function (err, msgEl) {
- if (err) msgEl = [
- h('a', {href: self.toUrl(msg.key)}, msg.key),
- u.renderError(err)]
- el.appendChild(h('div',
- 'published:',
- h('table.ssb-msgs', msgEl)
- ))
- cb()
- })
+ self.renderMsg(msg, false, function (err, msgEl) {
+ if (err) msgEl = [
+ h('a', {href: self.toUrl(msg.key)}, msg.key),
+ u.renderError(err)]
+ el.appendChild(h('table.ssb-msgs', msgEl))
+ cb()
})
})
return el
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)
- })
- }
-
}