aboutsummaryrefslogtreecommitdiff
path: root/lib/serve.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/serve.js')
-rw-r--r--lib/serve.js98
1 files changed, 94 insertions, 4 deletions
diff --git a/lib/serve.js b/lib/serve.js
index f975f4b..d87880c 100644
--- a/lib/serve.js
+++ b/lib/serve.js
@@ -386,6 +386,7 @@ Serve.prototype.path = function (url) {
case '/npm-registry': return this.npmRegistry(m[2])
case '/markdown': return this.markdown(m[2])
case '/edit-diff': return this.editDiff(m[2])
+ case '/about-diff': return this.aboutDiff(m[2])
case '/shard': return this.shard(m[2])
case '/zip': return this.zip(m[2])
case '/web': return this.web(m[2])
@@ -3837,14 +3838,14 @@ Serve.prototype.editDiff = function (url) {
return pull(
ph('section', {}, [
'diff: ',
- ph('a', {href: self.app.render.toUrl(id)}, id.substr(0, 8) + '…'),
+ ph('a', {href: self.app.render.toUrl(id)}, ph('code', id.substr(0, 8) + '…')),
u.readNext(function (cb) {
self.getMsgDecryptedMaybeOoo(id, function (err, msg) {
if (err) return cb(null, pull.once(u.renderError(err).outerHTML))
var c = msg.value.content || {}
self.getMsgDecryptedMaybeOoo(c.updated, function (err, oldMsg) {
if (err) return cb(null, pull.once(u.renderError(err).outerHTML))
- cb(null, self.postEditDiffTable(oldMsg, msg))
+ cb(null, self.textEditDiffTable(oldMsg, msg))
})
})
})
@@ -3854,10 +3855,99 @@ Serve.prototype.editDiff = function (url) {
)
}
-Serve.prototype.postEditDiffTable = function (oldMsg, newMsg) {
+function findMsg(msgs, id) {
+ for (var i = 0; i < msgs.length; i++) {
+ if (msgs[i].key === id) return i
+ }
+ return -1
+}
+
+Serve.prototype.aboutDiff = function (url) {
+ var self = this
+ var id
+ try {
+ id = decodeURIComponent(url.substr(1))
+ } catch(err) {
+ return pull(
+ pull.once(u.renderError(err).outerHTML),
+ self.wrapPage('diff: ' + id),
+ self.respondSink(400)
+ )
+ }
+ return pull(
+ ph('section', {}, [
+ 'diff: ',
+ ph('a', {href: self.app.render.toUrl(id)}, ph('code', id.substr(0, 8) + '…')),
+ u.readNext(function (cb) {
+ // About messages don't always include branch links. So get the whole thread
+ // and use ssb-sort to find what to consider the previous message(s).
+ self.getMsgDecryptedMaybeOoo(id, function (err, msg) {
+ if (err) return cb(null, pull.once(u.renderError(err).outerHTML))
+ var c = msg.value.content || {}
+ var rootId = c.about
+ if (!rootId) return gotLinks(new Error('Missing about root'))
+ var msgDate = new Date(msg.value.timestamp)
+ cb(null, ph('div', [
+ self.phIdLink(msg.value.author), ' ',
+ ph('span', {title: msgDate.toLocaleString()}, htime(msgDate)),
+ ph('div', u.readNext(next.bind(this, rootId, msg)))
+ ]))
+ })
+ })
+ ]),
+ self.wrapPage('diff: ' + id),
+ self.respondSink(200)
+ )
+
+ function next(rootId, msg, cb) {
+ pull(
+ self.app.getLinks(rootId),
+ pull.unique('key'),
+ self.app.unboxMessages(),
+ pull.collect(function (err, links) {
+ if (err) return gotLinks(err)
+ if (!self.useOoo) return gotLinks(null, links)
+ self.app.expandOoo({msgs: links, dest: id}, gotLinks)
+ })
+ )
+ function gotLinks(err, links) {
+ if (err) return cb(null, pull.once(u.renderError(err).outerHTML))
+
+ sort(links)
+ links = links.filter(function (msg) {
+ var c = msg && msg.value && msg.value.content
+ return c && c.type === 'about' && c.about === rootId
+ && typeof c.description === 'string'
+ })
+ var i = findMsg(links, id)
+ if (i < 0) return cb(null, ph('div', 'Unable to find previous message'))
+ var prevMsg = links[i-1]
+ var nextMsg = links[i+1]
+ var prevHref = prevMsg ?
+ self.app.render.toUrl('/about-diff/' + encodeURIComponent(prevMsg.key)) : null
+ var nextHref = nextMsg ?
+ self.app.render.toUrl('/about-diff/' + encodeURIComponent(nextMsg.key)) : null
+ cb(null, cat([
+ prevMsg
+ ? pull.values(['prev: ', ph('a', {href: prevHref}, ph('code',
+ prevMsg.key.substr(0, 8) + '…')), ', '])
+ : pull.empty(),
+ nextMsg
+ ? pull.values(['next: ', ph('a', {href: nextHref}, ph('code',
+ nextMsg.key.substr(0, 8) + '…'))])
+ : pull.empty(),
+ prevMsg ? self.textEditDiffTable(prevMsg, msg) : pull.empty()
+ ]))
+ }
+ }
+}
+
+Serve.prototype.textEditDiffTable = function (oldMsg, newMsg) {
var oldC = oldMsg.value.content || {}
var newC = newMsg.value.content || {}
- var diff = Diff.structuredPatch('', '', String(oldC.text), String(newC.text))
+ var oldText = String(oldC.text || oldC.description)
+ var newText = String(newC.text || newC.description)
+ var diff = Diff.structuredPatch('', '', oldText, newText)
var self = this
return pull(
ph('table', [