git ssb

30+

cel / git-ssb-web



Commit 395710809e2bb0e4cb1e7414e98bf061cb119c11

Make POSTs work on any request path

Charles Lehner committed on 3/26/2016, 7:13:46 PM
Parent: 707a9d074871552af7ea3d78771130b8e21e0b43

Files changed

index.jschanged
index.jsView
@@ -271,8 +271,58 @@
271271 }
272272
273273 function handleRequest(req) {
274274 var u = req._u = url.parse(req.url, true)
275+
276+ if (req.method == 'POST') {
277+ if (isPublic)
278+ return servePlainError(405, 'POST not allowed on public site')
279+ return readNext(function (cb) {
280+ readReqJSON(req, function (err, data) {
281+ if (err) return cb(null, serveError(err, 400))
282+ if (!data) return cb(null, serveError(new Error('No data'), 400))
283+
284+ switch (data.action) {
285+ case 'vote':
286+ var voteValue = +data.vote || 0
287+ if (!data.id)
288+ return cb(null, serveError(new Error('Missing vote id'), 400))
289+ var msg = schemas.vote(data.id, voteValue)
290+ return ssb.publish(msg, function (err) {
291+ if (err) return cb(null, serveError(err))
292+ cb(null, serveRedirect(req.url))
293+ })
294+ return
295+
296+ case 'name':
297+ if (!data.name)
298+ return cb(null, serveError(new Error('Missing name'), 400))
299+ if (!data.id)
300+ return cb(null, serveError(new Error('Missing id'), 400))
301+ var msg = schemas.name(data.id, data.name)
302+ return ssb.publish(msg, function (err) {
303+ if (err) return cb(null, serveError(err))
304+ cb(null, serveRedirect(req.url))
305+ })
306+
307+ default:
308+ if (path == 'issues,new') {
309+ issues.new({
310+ project: repo.id,
311+ title: data.title,
312+ text: data.text
313+ }, function (err, issue) {
314+ if (err) return cb(null, serveError(err))
315+ cb(null, serveRedirect('/' + encodeURIComponent(issue.id)))
316+ })
317+ } else {
318+ cb(null, servePlainError(400, 'What are you trying to do?'))
319+ }
320+ }
321+ })
322+ })
323+ }
324+
275325 var dirs = u.pathname.slice(1).split(/\/+/).map(tryDecodeURIComponent)
276326 var dir = dirs[0]
277327 if (dir == '')
278328 return serveIndex(req)
@@ -510,49 +560,12 @@
510560 function serveRepoPage(req, repo, path) {
511561 var defaultBranch = 'master'
512562 var query = req._u.query
513563
514- if (req.method == 'POST') {
515- if (isPublic)
516- return servePlainError(405, 'POST not allowed on public site')
517- return readNext(function (cb) {
518- readReqJSON(req, function (err, data) {
519- if (err) return cb(null, serveError(err, 400))
520- if (!data) return cb(null, serveError(new Error('No data'), 400))
521- if (data.vote != null) {
522- var voteValue = +data.vote || 0
523- ssb.publish(schemas.vote(repo.id, voteValue), function (err) {
524- if (err) return cb(null, serveError(err))
525- cb(null, serveRedirect(req.url))
526- })
527- } else if ('repo-name' in data) {
528- var name = data['repo-name']
529- if (!name) return cb(null, serveRedirect(req.url))
530- var msg = schemas.name(repo.id, name)
531- ssb.publish(msg, function (err) {
532- if (err) return cb(null, serveError(err))
533- cb(null, serveRedirect(req.url))
534- })
535- } else if (path == 'issues,new') {
536- // cb(null, servePlainError(200, JSON.stringify(data, 0, 2)))
537- issues.new({
538- project: repo.id,
539- title: data.title,
540- text: data.text
541- }, function (err, issue) {
542- if (err) return cb(null, serveError(err))
543- cb(null, serveRedirect('/' + encodeURIComponent(issue.id)))
544- })
545- } else {
546- cb(null, servePlainError(400, 'What are you trying to do?'))
547- }
548- })
549- })
550-
551- } else if (query.rev != null) {
564+ if (query.rev != null) {
552565 // Allow navigating revs using GET query param.
553566 // Replace the branch in the path with the rev query value
554- path[0] = 'tree'
567+ path[0] = path[0] || 'tree'
555568 path[1] = query.rev
556569 req._u.pathname = flattenPath([repo.id].concat(path))
557570 delete req._u.query.rev
558571 delete req._u.search
@@ -623,17 +636,23 @@
623636 (isPublic
624637 ? '<button disabled="disabled"><i>✌</i> Dig</button> '
625638 : '<input type="hidden" name="vote" value="' +
626639 (upvoted ? '0' : '1') + '">' +
640+ '<input type="hidden" name="action" value="vote">' +
641+ '<input type="hidden" name="id" value="' +
642+ escapeHTML(repo.id) + '">' +
627643 '<button type="submit"><i>✌</i> ' +
628644 (upvoted ? 'Undig' : 'Dig') +
629645 '</button>') + ' ' +
630646 '<strong>' + link(digsPath, votes.upvotes) + '</strong>' +
631647 '</form>' +
632648 '<form class="petname" action="" method="post">' +
633649 (isPublic ? '' :
634- '<input name="repo-name" id="repo-name" value="' +
650+ '<input name="name" id="repo-name" value="' +
635651 escapeHTML(repoName) + '" />' +
652+ '<input type="hidden" name="action" value="name">' +
653+ '<input type="hidden" name="id" value="' +
654+ escapeHTML(repo.id) + '">' +
636655 '<label class="repo-name-toggle" for="repo-name" ' +
637656 'title="Rename the repo"><i>✍</i></label>' +
638657 '<input class="repo-name-btn" type="submit" value="Rename">') +
639658 '<h2>' + link([repo.feed], authorName) + ' / ' +

Built with git-ssb-web