From c8ba67e6dd520dbd3671256249feb67be220938d Mon Sep 17 00:00:00 2001 From: cel Date: Tue, 30 May 2017 09:39:00 -1000 Subject: refactor to allow asynchronously generating message content --- lib/serve.js | 144 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 77 insertions(+), 67 deletions(-) (limited to 'lib/serve.js') diff --git a/lib/serve.js b/lib/serve.js index e0c089c..1d8d8af 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -2042,78 +2042,88 @@ Serve.prototype.composer = function (opts, cb) { )) done(cb) + function prepareContent(cb) { + content = { + type: 'post', + text: String(data.text).replace(/\r\n/g, '\n'), + } + var mentions = ssbMentions(data.text, {bareFeedNames: true, emoji: true}) + .filter(function (mention) { + if (mention.emoji) { + mention.link = formEmojiNames[mention.name] + || self.app.getReverseEmojiNameSync(mention.name) + if (!mention.link) return false + } + var blob = blobs[mention.link] + if (blob) { + if (!isNaN(blob.size)) + mention.size = blob.size + if (blob.type && blob.type !== 'application/octet-stream') + mention.type = blob.type + } else if (mention.link === '@') { + // bare feed name + var name = mention.name + var id = formNames[name] || self.app.getReverseNameSync('@' + name) + if (id) mention.link = id + else return false + } + return true + }) + if (mentions.length) content.mentions = mentions + if (data.recps != null) { + if (opts.recps) return cb(new Error('got recps in opts and data')) + content.recps = [myId] + u.extractFeedIds(data.recps).forEach(function (recp) { + if (content.recps.indexOf(recp) === -1) content.recps.push(recp) + }) + } else { + if (opts.recps) content.recps = opts.recps + } + if (data.fork_thread) { + content.root = opts.post || undefined + content.branch = u.fromArray(opts.postBranches) || undefined + } else { + content.root = opts.root || undefined + content.branch = u.fromArray(opts.branches) || undefined + } + if (channel) content.channel = data.channel + cb(null, content) + } + function preview(raw, cb) { - var myId = self.app.sbot.id + var msgContainer = h('table.ssb-msgs') + var contentInput = h('input', {type: 'hidden', name: 'content'}) + var content - try { - content = JSON.parse(data.text) - } catch (err) { - data.text = String(data.text).replace(/\r\n/g, '\n') - content = { - type: 'post', - text: data.text, - } - var mentions = ssbMentions(data.text, {bareFeedNames: true, emoji: true}) - .filter(function (mention) { - if (mention.emoji) { - mention.link = formEmojiNames[mention.name] - || self.app.getReverseEmojiNameSync(mention.name) - if (!mention.link) return false - } - var blob = blobs[mention.link] - if (blob) { - if (!isNaN(blob.size)) - mention.size = blob.size - if (blob.type && blob.type !== 'application/octet-stream') - mention.type = blob.type - } else if (mention.link === '@') { - // bare feed name - var name = mention.name - var id = formNames[name] || self.app.getReverseNameSync('@' + name) - if (id) mention.link = id - else return false - } - return true - }) - if (mentions.length) content.mentions = mentions - if (data.recps != null) { - if (opts.recps) return cb(new Error('got recps in opts and data')) - content.recps = [myId] - u.extractFeedIds(data.recps).forEach(function (recp) { - if (content.recps.indexOf(recp) === -1) content.recps.push(recp) - }) - } else { - if (opts.recps) content.recps = opts.recps - } - if (data.fork_thread) { - content.root = opts.post || undefined - content.branch = u.fromArray(opts.postBranches) || undefined - } else { - content.root = opts.root || undefined - content.branch = u.fromArray(opts.branches) || undefined - } - if (channel) content.channel = data.channel - } - var msg = { - value: { - author: myId, - timestamp: Date.now(), - content: content + try { content = JSON.parse(data.text) } + catch (err) {} + if (content) gotContent(null, content) + else prepareContent(gotContent) + + function gotContent(err, content) { + if (err) return cb(err) + contentInput.value = JSON.stringify(content) + var myId = self.app.sbot.id + var msg = { + value: { + author: myId, + timestamp: Date.now(), + content: content + } } + if (content.recps) msg.value.private = true + pull( + pull.once(msg), + self.app.unboxMessages(), + self.app.render.renderFeeds(raw), + pull.drain(function (el) { + msgContainer.appendChild(h('tbody', el)) + }, cb) + ) } - if (content.recps) msg.value.private = true - var msgContainer = h('table.ssb-msgs') - pull( - pull.once(msg), - self.app.unboxMessages(), - self.app.render.renderFeeds(raw), - pull.drain(function (el) { - msgContainer.appendChild(h('tbody', el)) - }, cb) - ) + return [ - h('input', {type: 'hidden', name: 'content', - value: JSON.stringify(content)}), + contentInput, opts.redirectToPublishedMsg ? h('input', {type: 'hidden', name: 'redirect_to_published_msg', value: '1'}) : '', h('div', h('em', 'draft:')), -- cgit v1.2.3