From 552d2797bbc61a5a2397d43b03e358f7a9004590 Mon Sep 17 00:00:00 2001 From: cel Date: Fri, 10 Mar 2017 10:14:41 -0500 Subject: Publish message before rendering page and show it at the top of the page --- lib/render.js | 23 ++++++++-------------- lib/serve.js | 63 +++++++++++++++++++++++++++++++++++++---------------------- 2 files changed, 48 insertions(+), 38 deletions(-) (limited to 'lib') 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) - }) - } - } -- cgit v1.2.3