aboutsummaryrefslogtreecommitdiff
path: root/app.js
diff options
context:
space:
mode:
Diffstat (limited to 'app.js')
-rw-r--r--app.js224
1 files changed, 71 insertions, 153 deletions
diff --git a/app.js b/app.js
index 0e5d054..26b06eb 100644
--- a/app.js
+++ b/app.js
@@ -1,176 +1,94 @@
var screen = h('div', {id: 'screen'})
document.body.appendChild(screen)
-function keyPage (keys) {
- var scroller = document.getElementById('scroller')
-
- var message = h('div', {classList: 'message'})
-
- message.appendChild(h('p', {innerHTML: marked('This is your ed25519 public/private keypair. It was generated using [Tweetnacl.js](https://tweetnacl.js.org/#/). Your public key is your identity when using [Bogbook](http://bogbook.com/), save your key in a safe place so that you can continue to use the same identity.')}))
-
- // print stringified keypair
- message.appendChild(h('pre', {style: 'width: 80%'}, [h('code', [JSON.stringify(keys)])]))
-
- // delete key button
- message.appendChild(h('button', {
- onclick: function () {
- localforage.removeItem('id', function () {
- location.hash = ''
- location.reload()
- })
- }
- }, ['Delete Key']))
-
- var textarea = h('textarea', {placeholder: 'Import your existing ed25519 keypair'})
- message.appendChild(textarea)
- message.appendChild(h('button', {
- onclick: function () {
- if (textarea.value) {
- localforage.setItem('id', JSON.parse(textarea.value))
- location.reload()
- }
- }
- }, ['Import Key']))
-
- scroller.appendChild(message)
-}
-
-function profilePage (src, keys) {
- var scroller = document.getElementById('scroller')
-
+function composer (keys, reply) {
+ var messageDiv = h('div')
var message = h('div', {classList: 'message'})
- var identify = h('input', {placeholder: 'Identify ' + src.substring(0, 10) + '...'})
+ var textarea = h('textarea', {placeholder: 'Write a new bog post...'})
- message.appendChild(h('div', [
- identify,
- h('button', {onclick: function () {
- if (identify.value) {
- var content = {
- author: keys.publicKey,
- type: 'name',
- naming: src,
- name: identify.value,
- timestamp: Date.now()
- }
-
- identify.value = ''
- publish(content, keys)
- localforage.setItem('id', keys, function (err, published) {
- if (published) {
- location.reload()
+ var publisher = h('div', [
+ textarea,
+ h('button', {
+ onclick: function () {
+ if (textarea.value) {
+ var content = {
+ type: 'post',
+ text: textarea.value,
+ timestamp: Date.now()
+ }
+ if (reply) {
+ content.reply = reply.key
}
- })
+ publish(content, keys).then(post => {
+ open(post).then(msg => {
+ textarea.value = ''
+ if (reply) {
+ messageDiv.removeChild(messageDiv.firstChild)
+ if (messageDiv.firstChild) {
+ messageDiv.insertBefore(h('div', {classList: 'submessage'}, [render(msg, keys)]), messageDiv.childNodes[1])
+ } else {
+ messageDiv.appendChild(h('div', {classList: 'submessage'}, [render(msg, keys)]))
+ }
+ } else {
+ if (messageDiv.firstChild) {
+ messageDiv.insertBefore(render(msg, keys), messageDiv.childNodes[1])
+ } else {
+ messageDiv.appendChild(render(msg, keys))
+ }
+ }
+ })
+ })
+ }
}
- }}, ['Identify'])
- ]))
-
- scroller.appendChild(message)
+ }, ['Publish'])
+ ])
- requestFeed(src, 'ws://localhost:8080/', keys.publicKey)
-
- localforage.getItem(src, function (err, log) {
- if (log) {
- for (var i=0; i < log.length; i++) {
- var post = log[i]
- scroller.appendChild(renderMessage(post))
- }
- }
- })
+ message.appendChild(publisher)
+ messageDiv.appendChild(message)
+ return messageDiv
}
-function threadPage (src, keys) {
- var scroller = document.getElementById('scroller')
-
- localforage.getItem('log', function (err, log) {
- for (var i = log.length - 1; i >= 0; --i) {
- if (log[i].key === src) {
- var post = log[i]
- scroller.appendChild(renderMessage(post))
- }
- }
- })
-}
-
-function publicPage (keys) {
- compose(keys)
-
- localforage.getItem('log', function (err, log) {
- if (log) {
- for (var i=0; i < log.length; i++) {
- var post = log[i]
- scroller.appendChild(renderMessage(post))
- }
- var newLog = log.sort(function (a, b) {
- return b.content.timestamp - a.content.timestamp
- })
- if (newLog) {
- localforage.setItem('log', log)
- }
- }
- })
-}
-
-function route () {
- localforage.getItem('id', function (err, keys) {
+function route (keys) {
+ src = window.location.hash.substring(1)
- src = window.location.hash.substring(1)
- var scroller = h('div', {id: 'scroller'})
- var screen = document.getElementById('screen')
- screen.appendChild(scroller)
+ var scroller = h('div', {id: 'scroller'})
+ var screen = document.getElementById('screen')
+ screen.appendChild(scroller)
- if (src === 'key') {
- keyPage(keys)
- } else if (src[0] === '@') {
- profilePage(src, keys)
- } else if (src[0] === '%') {
- threadPage(src, keys)
- } else {
- publicPage(keys)
- }
- })
+ if (src === 'key') {
+ keyPage(keys)
+ } else if (src[0] === '@') {
+ profilePage(src, keys)
+ } else if (src[0] === '%') {
+ threadPage(src, keys)
+ } else {
+ publicPage(keys)
+ }
}
-localforage.getItem('id', function (err, keys) {
- if (keys) {
+keys().then(key => {
- var navbar = h('div', {classList: 'navbar'}, [
- h('div', {classList: 'internal'}, [
- h('li', [h('a', {href: '/#'}, ['Home'])]),
- h('li', [h('a', {href: '#' + keys.publicKey}, [getName(keys.publicKey)])]),
- h('li', [h('a', {href: '/#key'}, ['Key'])])
- ])
+ var navbar = h('div', {classList: 'navbar'}, [
+ h('div', {classList: 'internal'}, [
+ h('li', [h('a', {href: '#'}, ['Home'])]),
+ h('li', [h('a', {href: '#' + key.publicKey}, [getName(key.publicKey)])]),
+ h('li', [h('a', {href: '#key'}, ['Key'])])
])
+ ])
+ document.body.appendChild(navbar)
- document.body.appendChild(navbar)
-
- route()
- } else {
- var genkey = nacl.sign.keyPair()
- if (genkey) {
- var keys = {
- publicKey: '@' + nacl.util.encodeBase64(genkey.publicKey),
- privateKey: nacl.util.encodeBase64(genkey.secretKey)
- }
-
- if (keys.publicKey.includes('/')) {
- console.log('TRYING AGAIN')
- setTimeout(function () {
- location.reload()
- }, 10)
- } else {
- welcomeScreen(keys)
- }
- }
- }
+ route(key)
})
-
-
window.onhashchange = function () {
- var oldscreen = document.getElementById('screen')
- var newscreen = h('div', {id: 'screen'})
- oldscreen.parentNode.replaceChild(newscreen, oldscreen)
- route()
+ keys().then(key => {
+ var oldscreen = document.getElementById('screen')
+ var newscreen = h('div', {id: 'screen'})
+ oldscreen.parentNode.replaceChild(newscreen, oldscreen)
+
+ route(key)
+ })
}
+