aboutsummaryrefslogtreecommitdiff
path: root/lib/serve.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/serve.js')
-rw-r--r--lib/serve.js129
1 files changed, 102 insertions, 27 deletions
diff --git a/lib/serve.js b/lib/serve.js
index 9296c46..a5c2e48 100644
--- a/lib/serve.js
+++ b/lib/serve.js
@@ -122,10 +122,17 @@ Serve.prototype.go = function () {
else next()
}
- function next(err) {
+ function next(err, publishedMsg) {
if (err) {
self.res.writeHead(400, {'Content-Type': 'text/plain'})
self.res.end(err.stack)
+ } else if (publishedMsg) {
+ if (self.data.redirect_to_published_msg) {
+ self.redirect(self.app.render.toUrl(publishedMsg.key))
+ } else {
+ self.publishedMsg = publishedMsg
+ self.handle()
+ }
} else {
self.handle()
}
@@ -178,8 +185,7 @@ Serve.prototype.publish = function (content, cb) {
if (err) return cb(err)
delete self.data.text
delete self.data.recps
- self.publishedMsg = msg
- return cb()
+ return cb(null, msg)
})
})
}
@@ -209,6 +215,13 @@ Serve.prototype.respondSink = function (status, headers, cb) {
})
}
+Serve.prototype.redirect = function (dest) {
+ this.res.writeHead(302, {
+ Location: dest
+ })
+ this.res.end()
+}
+
Serve.prototype.path = function (url) {
var m
url = url.replace(/^\/+/, '/')
@@ -217,7 +230,8 @@ Serve.prototype.path = function (url) {
case '/robots.txt': return this.res.end('User-agent: *')
}
if (m = /^\/%23(.*)/.exec(url)) {
- return this.channel(decodeURIComponent(m[1]))
+ return this.redirect(this.app.render.toUrl('/channel/'
+ + decodeURIComponent(m[1])))
}
m = /^([^.]*)(?:\.(.*))?$/.exec(url)
switch (m[1]) {
@@ -235,6 +249,7 @@ Serve.prototype.path = function (url) {
}
m = /^(\/?[^\/]*)(\/.*)?$/.exec(url)
switch (m[1]) {
+ case '/channel': return this.channel(m[2])
case '/type': return this.type(m[2])
case '/links': return this.links(m[2])
case '/static': return this.static(m[2])
@@ -364,10 +379,7 @@ Serve.prototype.search = function (ext) {
}
if (u.isRef(searchQ) || searchQ[0] === '#') {
- self.res.writeHead(302, {
- Location: self.app.render.toUrl(searchQ)
- })
- return self.res.end()
+ return self.redirect(self.app.render.toUrl(searchQ))
}
pull(
@@ -462,6 +474,7 @@ Serve.prototype.compose = function (ext) {
var self = this
self.composer({
channel: '',
+ redirectToPublishedMsg: true,
}, function (err, composer) {
if (err) return cb(err)
pull(
@@ -521,20 +534,48 @@ Serve.prototype.peers = function (ext) {
Serve.prototype.channels = function (ext) {
var self = this
+ var id = self.app.sbot.id
+
+ function renderMyChannels() {
+ return pull(
+ self.app.streamMyChannels(id),
+ paramap(function (channel, cb) {
+ // var subscribed = false
+ cb(null, [
+ h('a', {href: self.app.render.toUrl('/channel/' + channel)}, '#' + channel),
+ ' '
+ ])
+ }, 8),
+ pull.map(u.toHTML),
+ self.wrapMyChannels()
+ )
+ }
+
+ function renderNetworkChannels() {
+ return pull(
+ self.app.streamChannels(),
+ paramap(function (channel, cb) {
+ // var subscribed = false
+ cb(null, [
+ h('a', {href: self.app.render.toUrl('/channel/' + channel)}, '#' + channel),
+ ' '
+ ])
+ }, 8),
+ pull.map(u.toHTML),
+ self.wrapChannels()
+ )
+ }
pull(
- self.app.streamChannels(),
- paramap(function (channel, cb) {
- var subscribed = false
- cb(null, [
- h('a', {href: self.app.render.toUrl('#' + channel)}, '#' + channel),
- ' '
+ cat([
+ ph('section', {}, [
+ ph('h3', {}, 'Channels:'),
+ renderMyChannels(),
+ renderNetworkChannels()
])
- }, 8),
- pull.map(u.toHTML),
- self.wrapChannels(),
- self.wrapPage('channels'),
- self.respondSink(200, {
+ ]),
+ this.wrapPage('channels'),
+ this.respondSink(200, {
'Content-Type': ctype(ext)
})
)
@@ -721,7 +762,8 @@ Serve.prototype.rawId = function (id) {
})
}
-Serve.prototype.channel = function (channel) {
+Serve.prototype.channel = function (path) {
+ var channel = decodeURIComponent(String(path).substr(1))
var q = this.query
var gt = Number(q.gt) || -Infinity
var lt = Number(q.lt) || Date.now()
@@ -771,7 +813,8 @@ Serve.prototype.id = function (id, ext) {
if (self.query.raw != null) return self.rawId(id)
this.app.getMsgDecrypted(id, function (err, rootMsg) {
- if (err && err.name === 'NotFoundError') err = null, rootMsg = {key: id}
+ if (err && err.name === 'NotFoundError') err = null, rootMsg = {
+ key: id, value: {content: false}}
if (err) return self.respond(500, err.stack || err)
var rootContent = rootMsg && rootMsg.value && rootMsg.value.content
var recps = rootContent && rootContent.recps
@@ -795,7 +838,9 @@ Serve.prototype.id = function (id, ext) {
self.wrapThread({
recps: recps,
root: threadRootId,
- branches: id === threadRootId ? threadHeads(links, id) : id,
+ post: id,
+ branches: threadHeads(links, threadRootId),
+ postBranches: threadRootId !== id && threadHeads(links, id),
channel: channel,
}),
self.wrapPage(id),
@@ -1041,6 +1086,7 @@ Serve.prototype.wrapPage = function (title, searchQ) {
h('meta', {charset: 'utf-8'}),
h('title', title),
h('meta', {name: 'viewport', content: 'width=device-width,initial-scale=1'}),
+ h('link', {rel: 'icon', href: render.toUrl('/static/hermie.ico'), type: 'image/x-icon'}),
h('style', styles())
),
h('body',
@@ -1233,8 +1279,10 @@ Serve.prototype.wrapThread = function (opts) {
placeholder: recps ? 'private reply' : 'reply',
id: 'reply',
root: opts.root,
+ post: opts.post,
channel: opts.channel || '',
branches: opts.branches,
+ postBranches: opts.postBranches,
recps: recps,
}, function (err, composer) {
if (err) return cb(err)
@@ -1282,7 +1330,7 @@ Serve.prototype.wrapChannel = function (channel) {
cb(null, [
h('section',
h('h3.feed-name',
- h('a', {href: self.app.render.toUrl('#' + channel)}, '#' + channel)
+ h('a', {href: self.app.render.toUrl('/channel/' + channel)}, '#' + channel)
)
),
composer,
@@ -1337,7 +1385,21 @@ Serve.prototype.wrapChannels = function (opts) {
return u.hyperwrap(function (channels, cb) {
cb(null, [
h('section',
- h('h3', 'Channels')
+ h('h4', 'Network')
+ ),
+ h('section',
+ channels
+ )
+ ])
+ })
+}
+
+Serve.prototype.wrapMyChannels = function (opts) {
+ var self = this
+ return u.hyperwrap(function (channels, cb) {
+ cb(null, [
+ h('section',
+ h('h4', 'Subscribed')
),
h('section',
channels
@@ -1414,6 +1476,12 @@ Serve.prototype.composer = function (opts, cb) {
channel != null ?
h('div', '#', h('input', {name: 'channel', placeholder: 'channel',
value: channel})) : '',
+ opts.root !== opts.post ? h('div',
+ h('label', {for: 'fork_thread'},
+ h('input', {id: 'fork_thread', type: 'checkbox', name: 'fork_thread', value: 'post', checked: data.fork_thread || undefined}),
+ ' fork thread'
+ )
+ ) : '',
h('textarea', {
id: opts.id,
name: 'text',
@@ -1428,7 +1496,7 @@ Serve.prototype.composer = function (opts, cb) {
h('code', '@' + mention.name), ': ',
h('input', {name: 'mention_name', type: 'hidden',
value: mention.name}),
- h('input.id-input', {name: 'mention_id',
+ h('input.id-input', {name: 'mention_id', size: 60,
value: mention.id, placeholder: 'id'}))
}))
] : '',
@@ -1487,8 +1555,13 @@ Serve.prototype.composer = function (opts, cb) {
} else {
if (opts.recps) content.recps = opts.recps
}
- if (opts.root) content.root = opts.root
- if (opts.branches) content.branch = u.fromArray(opts.branches)
+ 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 = {
@@ -1511,6 +1584,8 @@ Serve.prototype.composer = function (opts, cb) {
return [
h('input', {type: 'hidden', name: 'content',
value: JSON.stringify(content)}),
+ opts.redirectToPublishedMsg ? h('input', {type: 'hidden',
+ name: 'redirect_to_published_msg', value: '1'}) : '',
h('div', h('em', 'draft:')),
msgContainer,
h('div.composer-actions',