From 606ff997bc4249d4ef0baae2e6336a739c3d64c7 Mon Sep 17 00:00:00 2001 From: cel Date: Tue, 7 Mar 2017 13:19:25 -0500 Subject: Implement choosing ids for mentions --- lib/app.js | 11 ++++++++++- lib/serve.js | 39 ++++++++++++++++++++++++++++++++++----- lib/util.js | 8 ++++++++ static/styles.css | 4 ++++ 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/lib/app.js b/lib/app.js index d994d04..530f10f 100644 --- a/lib/app.js +++ b/lib/app.js @@ -31,8 +31,9 @@ function App(sbot, config) { sbot.get = memo({cache: lru(100)}, sbot.get) this.getMsg = memo({cache: lru(100)}, getMsgWithValue, sbot) - this.getAbout = memo({cache: lru(100)}, getAbout, sbot, sbot.id) + this.getAbout = memo({cache: lru(100)}, getAbout.bind(this), sbot, sbot.id) this.unboxContent = memo({cache: lru(100)}, sbot.private.unbox) + this.reverseNameCache = lru(100) this.unboxMsg = this.unboxMsg.bind(this) @@ -110,9 +111,15 @@ App.prototype.addBlob = function (cb) { } App.prototype.pushBlob = function (id, cb) { + console.error('pushing blob', id) this.sbot.blobs.push(id, cb) } +App.prototype.getReverseNameSync = function (name) { + var id = this.reverseNameCache.get(name) + return id +} + function getMsgWithValue(sbot, id, cb) { sbot.get(id, function (err, value) { if (err) return cb(err) @@ -121,12 +128,14 @@ function getMsgWithValue(sbot, id, cb) { } function getAbout(sbot, src, id, cb) { + var self = this ssbAvatar(sbot, src, id, function (err, about) { if (err) return cb(err) var sigil = id && id[0] || '@' if (about.name && about.name[0] !== sigil) { about.name = sigil + about.name } + self.reverseNameCache.set(about.name, id) cb(null, about) }) } diff --git a/lib/serve.js b/lib/serve.js index 1f86188..fcc737e 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -81,7 +81,9 @@ Serve.prototype.go = function () { }) }) busboy.on('field', function (fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) { - data[fieldname] = val + if (!(fieldname in data)) data[fieldname] = val + else if (Array.isArray(data[fieldname])) data[fieldname].push(val) + else data[fieldname] = [data[fieldname], val] }) this.req.pipe(busboy) } else { @@ -830,6 +832,13 @@ Serve.prototype.composer = function (opts, cb) { } var channel = data.channel != null ? data.channel : opts.channel + var formNames = {} + var mentionIds = u.toArray(data.mention_id) + var mentionNames = u.toArray(data.mention_name) + for (var i = 0; i < mentionIds.length && i < mentionNames.length; i++) { + formNames[mentionNames[i]] = u.extractFeedIds(mentionIds[i])[0] + } + var done = multicb({pluck: 1, spread: true}) done()(null, h('section.composer', h('form', {method: 'post', action: opts.id ? '#' + opts.id : '', @@ -877,6 +886,7 @@ Serve.prototype.composer = function (opts, cb) { function preview(raw, cb) { var myId = self.app.sbot.id var content + var unknownMentions = [] try { content = JSON.parse(data.text) } catch (err) { @@ -887,21 +897,29 @@ Serve.prototype.composer = function (opts, cb) { } var mentions = ssbMentions(data.text) if (mentions.length) { - content.mentions = mentions.map(function (mention) { + content.mentions = mentions.filter(function (mention) { 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 (/^[@%&]$/.test(mention.link)) { + // bare mention + var name = mention.name + var fullName = mention.link + name + var id = formNames[name] || self.app.getReverseNameSync(fullName) + unknownMentions.push({name: name, fullName: fullName, id: id}) + if (id) mention.link = id + else return false } - return mention + return true }) } if (data.recps != null) { if (opts.recps) return cb(new Error('got recps in opts and data')) content.recps = [myId] - String(data.recps).replace(u.ssbRefRegex, function (recp) { + u.extractFeedIds(data.recps).forEach(function (recp) { if (content.recps.indexOf(recp) === -1) content.recps.push(recp) }) } else { @@ -934,6 +952,17 @@ Serve.prototype.composer = function (opts, cb) { value: JSON.stringify(content)}), h('div', h('em', 'draft:')), msgContainer, + unknownMentions.length > 0 ? [ + h('div', h('em', 'names:')), + h('ul', unknownMentions.map(function (mention) { + return h('li', + h('code', mention.fullName), ': ', + h('input', {name: 'mention_name', type: 'hidden', + value: mention.name}), + h('input.mention-id-input', {name: 'mention_id', + value: mention.id, placeholder: 'id'})) + })) + ] : '', h('div.composer-actions', h('input', {type: 'submit', name: 'action', value: 'publish'}) ) @@ -948,7 +977,7 @@ Serve.prototype.composer = function (opts, cb) { return cb(), u.renderError(e) } var done = multicb({pluck: 1, spread: true}) - toArray(content && content.mentions).forEach(function (mention) { + u.toArray(content && content.mentions).forEach(function (mention) { if (mention.link && mention.link[0] === '&' && !isNaN(mention.size)) self.app.pushBlob(mention.link, done()) }) diff --git a/lib/util.js b/lib/util.js index b94ebc2..d0ab97a 100644 --- a/lib/util.js +++ b/lib/util.js @@ -95,3 +95,11 @@ u.tryDecodeJSON = function (json) { return null } } + +u.extractFeedIds = function (str) { + var ids = [] + String(str).replace(u.ssbRefRegex, function (id) { + ids.push(id) + }) + return ids +} diff --git a/static/styles.css b/static/styles.css index a6119d5..8a7129b 100644 --- a/static/styles.css +++ b/static/styles.css @@ -134,3 +134,7 @@ td { margin: 0; padding: 0; } + +.mention-id-input { + width: 60ex; +} -- cgit v1.2.3