git ssb

9+

cel / ssb-viewer



Commit 4d2dd658deab23b90f797d31084d9217cbf66345

Add id query form on home page

cel committed on 8/22/2018, 7:59:03 PM
Parent: 36c63bc1828456bf3417ff9eb5187cfcb776b3b2

Files changed

index.jschanged
index.jsView
@@ -10,8 +10,10 @@
1010 var memo = require('asyncmemo')
1111 var lru = require('lrucache')
1212 var webresolve = require('ssb-web-resolver')
1313 var serveEmoji = require('emoji-server')()
14 +var refs = require('ssb-ref')
15 +var h = require('hyperscript')
1416 var {
1517 MdRenderer,
1618 renderEmoji,
1719 formatMsgs,
@@ -97,8 +99,10 @@
9799 case '%': return serveId(req, res, m[1], m[3], m[5])
98100 case '@': return serveFeed(req, res, m[1], m[3], m[5])
99101 case '&': return serveBlob(req, res, sbot, m[1])
100102 }
103 +
104 + if (m[4] === '/') return serveHome(req, res, m[5])
101105 return respond(res, 404, 'Not found')
102106 }
103107
104108 function serveFeed(req, res, feedId, ext) {
@@ -524,8 +528,44 @@
524528 fs.createReadStream(file).pipe(res)
525529 })
526530 }
527531
532 +function asLink(id) {
533 + if (!id || typeof id !== 'string') return null
534 + id = id.trim()
535 + if (refs.isLink(id)) return id
536 + try {
537 + id = decodeURIComponent(id)
538 + } catch(e) {
539 + return null
540 + }
541 + if (refs.isLink(id)) return id
542 +}
543 +
544 +function serveHome(req, res, query, conf) {
545 + var q = query ? qs.parse(query) : {}
546 + var id = asLink(q.id)
547 + if (id) {
548 + res.writeHead(303, {
549 + Location: '/' + (refs.isMsgId(id) ? encodeURIComponent(id) : id)
550 + })
551 + return res.end()
552 + }
553 + res.writeHead(200, {
554 + 'Content-Type': 'text/html'
555 + })
556 + pull(
557 + pull.once(h('form', {method: 'get', action: ''},
558 + h('input', {name: 'id', placeholder: 'id', size: 60, value: q.id || ''}), ' ',
559 + h('input', {type: 'submit', value: 'Go'})
560 + ).outerHTML),
561 + wrapPage('ssb-viewer'),
562 + toPull(res, function (err) {
563 + if (err) console.error('[viewer]', err)
564 + })
565 + )
566 +}
567 +
528568 function serveRobots(req, res, conf) {
529569 var disallow = conf.disallowRobots == null ? true : conf.disallowRobots
530570 res.end('User-agent: *\n'
531571 + (disallow ? 'Disallow: /\n' : ''))

Built with git-ssb-web