aboutsummaryrefslogtreecommitdiff
path: root/app.js
diff options
context:
space:
mode:
Diffstat (limited to 'app.js')
-rw-r--r--app.js435
1 files changed, 175 insertions, 260 deletions
diff --git a/app.js b/app.js
index 1de23f4..0ceb9af 100644
--- a/app.js
+++ b/app.js
@@ -1,322 +1,237 @@
var screen = h('div', {id: 'screen'})
document.body.appendChild(screen)
+function compose (keys) {
+ var message = h('div', {classList: 'message'})
-localforage.getItem('id', function (err, value) {
- // the navbar has a dual purpose of generating a key if you don't already have one
+ var scroller = document.getElementById('scroller')
+
+ scroller.insertBefore(message, scroller.firstChild)
- if (value) {
- var keys = value
- 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'])])
- ])
- ])
- document.body.appendChild(navbar)
- } else if (value == null) {
- var genkey = nacl.sign.keyPair()
- if (genkey) {
- var keys = {
- publicKey: '@' + nacl.util.encodeBase64(genkey.publicKey),
- privateKey: nacl.util.encodeBase64(genkey.secretKey)
+ var textarea = h('textarea', {placeholder: 'Write a new bog post'})
+
+ message.appendChild(textarea)
+
+ var composer = h('div', [
+ h('button', {
+ onclick: function () {
+ if (textarea.value) {
+ var content = {
+ author: keys.publicKey,
+ type: 'post',
+ text: textarea.value,
+ timestamp: Date.now()
+ }
+ textarea.value = ''
+ publish(content, keys)
+ }
}
- // when we get our next round of funding, let's figure out how to do this without a page reload
- if (keys.publicKey.includes('/')) {
- console.log('TRYING AGAIN')
- setTimeout(function () {
- window.location.reload()
- }, 10)
- } else {
- localforage.setItem('id', keys)
- window.location.reload()
+ }, ['Publish'])
+ ])
+ message.appendChild(composer)
+}
+
+
+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 () {
+ localStorage['id'] = ''
+ 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']))
-if (!localStorage['subscribees']) {
- var subscribees = ['@218Fd2bCrmXe4gwnMg5Gcb9qVZrjXquym2AlelbkBro=']
- localStorage['subscribees'] = JSON.stringify(subscribees)
+ scroller.appendChild(message)
}
-if (!localStorage['pubs']) {
- var pubs = ['ws://bogbook.com/', 'ws://localhost:8080/']
- localStorage['pubs'] = JSON.stringify(pubs)
+function profilePage (src, keys) {
+ var scroller = document.getElementById('scroller')
+
+ localforage.getItem(src, function (err, log) {
+ if (log) {
+ for (var i=0; i < log.length; i++) {
+ var post = log[i]
+ scroller.appendChild(renderMessage(post))
+ }
+ }
+ })
}
-function compose (keys, opts) {
- localforage.getItem('id', function (err, keys) {
- if (keys) {
- var header = h('div', {classList: 'message'})
- var scroller = document.getElementById('scroller')
-
- scroller.insertBefore(header, scroller.firstChild)
-
- var textarea = h('textarea', {placeholder: 'Write a new bog post'})
-
- header.appendChild(textarea)
-
- var composer = h('div', [
- h('button', {
- onclick: function () {
- if (textarea.value) {
- var content = {
- author: keys.publicKey,
- type: 'post',
- text: textarea.value,
- timestamp: Date.now()
- }
- textarea.value = ''
- publish(content, keys)
- }
- }
- }, ['Publish'])
- ])
- header.appendChild(composer)
+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) {
src = window.location.hash.substring(1)
var scroller = h('div', {id: 'scroller'})
var screen = document.getElementById('screen')
-
-
screen.appendChild(scroller)
if (src === 'key') {
- var keyMessage = h('div', {classList: 'message'})
-
- keyMessage.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 identiy 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
- keyMessage.appendChild(h('pre', {style: 'width: 80%'}, [h('code', [JSON.stringify(keys)])]))
-
- // delete key button
- keyMessage.appendChild(h('button', {
- onclick: function () {
- localStorage['id'] = ''
- location.reload()
- }
- }, ['Delete Key']))
-
- var textarea = h('textarea', {placeholder: 'Import your existing ed25519 keypair'})
- keyMessage.appendChild(textarea)
- keyMessage.appendChild(h('button', {
- onclick: function () {
- if (textarea.value) {
- localforage.setItem('id', JSON.parse(textarea.value))
- location.reload()
- }
- }
- }, ['Import Key']))
-
- scroller.appendChild(keyMessage)
+ keyPage(keys)
+ } else if (src[0] === '@') {
+ profilePage(src, keys)
+ } else if (src[0] === '%') {
+ threadPage(src, keys)
+ } else {
+ publicPage(keys)
+ }
+ })
+}
- if (localStorage['id']) {
+localforage.getItem('id', function (err, keys) {
+ if (keys) {
- var oldKey = h('div', {classlist: 'message'})
+ 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'])])
+ ])
+ ])
- oldKey.appendChild(h('p', ['You had a key in localStorage. Import it to the new database by pasting it into the box above.']))
- oldKey.appendChild(h('pre', {style: 'width: 80%'}, [h('code', [localStorage['id']])]))
+ document.body.appendChild(navbar)
- scroller.appendChild(oldKey)
+ route()
+ } else {
+ var genkey = nacl.sign.keyPair()
+ if (genkey) {
+ var keys = {
+ publicKey: '@' + nacl.util.encodeBase64(genkey.publicKey),
+ privateKey: nacl.util.encodeBase64(genkey.secretKey)
}
-
- var pubMessage = h('div', {classList: 'message'})
-
- var newPub = h('input', {placeholder: 'Add a new pub. Ex: ws://bogbook.com/'})
-
- var pubs = JSON.parse(localStorage['pubs'])
-
- pubMessage.appendChild(h('div', [
- h('p', {innerHTML: marked('These are your bogbook pubs. These servers will sync data when you publish a new post, when you subscribe to new feeds, and when you click on feed ids.')}),
- newPub,
- h('button', {
- onclick: function () {
- if (newPub.value) {
- pubs.push(newPub.value)
- localStorage['pubs'] = JSON.stringify(pubs)
- location.reload()
- }
- }
- }, ['Add Pub'])
- ]))
- function removeButton (pubName) {
- var button = h('button', {
- onclick: function () {
- console.log('removing' + pubName)
- for (var i = pubs.length; i--;) {
- if (pubs[i] === pubName) {
- pubs.splice(i, 1);
- localStorage['pubs'] = JSON.stringify(pubs)
- window.location.reload()
- }
- }
- }
- }, ['Remove Pub'])
- return button
- }
+ if (keys.publicKey.includes('/')) {
+ console.log('TRYING AGAIN')
+ setTimeout(function () {
+ location.reload()
+ }, 10)
+ } else {
+ var scroller = h('div', {id: 'scroller'})
+ screen.appendChild(scroller)
- for (i = 0; i < pubs.length; i++) {
- var pubName = pubs[i]
- pubMessage.appendChild(h('p', [
- pubName,
- removeButton(pubName)
- ]))
- }
+ var message = h('div', {classList: 'message'})
- scroller.appendChild(pubMessage)
- }
+ scroller.appendChild(message)
+ message.appendChild(h('h1', ['Welcome to Bogbook']))
+ message.appendChild(h('p', ['Bogbook is a distributed blogging network of signed append-only feeds. To avoid confusion, we call them "bogs".']))
- else if (src[0] === '@') {
- var profile = h('div', {classList: 'message'})
- scroller.appendChild(profile)
+ message.appendChild(h('p', ['Please note: Bogbook is experimental software, not for use in producton environments. Expect bugs and breaking changes. Pull-requests are needed.']))
- if (src == keys.publicKey) {
- var nameInput = h('input', {placeholder: 'Publish a new name'})
+ message.appendChild(h('p', {innerHTML: marked('View the code: [http://github.com/bogbook/bog](http://github.com/bogbook/bog). Questions? [ev@evbogue.com](mailto:ev@evbogue.com).')}))
- var namePublisher = h('div',[
- nameInput,
- h('button', {
- onclick: function () {
- if (nameInput.value) {
+ message.appendChild(h('hr'))
+ message.appendChild(h('h3', ['Get started']))
- var content = {
- author: keys.publicKey,
- type: 'name',
- text: nameInput.value,
- timestamp: Date.now()
- }
+ message.appendChild(h('p', {innerHTML: marked('This is an ed25519 public/private signing keypair. It was generated using [TweetNaCl.js](https://tweetnacl.js.org/#/)')}))
+ message.appendChild(h('pre', [JSON.stringify(keys)]))
- publish(content, keys)
- }
- }
- }, ['Publish'])
- ])
+ message.appendChild(h('p', ['Right now, this keypair exists only in memory. When you leave this page, the keypair will vanish forever. If you refresh this page you\'ll receive a new keypair.']))
- profile.appendChild(namePublisher)
+ message.appendChild(h('p', {innerHTML: marked('To save this keypair, identify with handle below. Once you identify, your public/private keypair will be stored in your browser using [localForage.js](https://localforage.github.io/localForage). Save your keypair somewhere safe to preserve your identity.')}))
- readFile()
+ message.appendChild(h('hr'))
+ message.appendChild(h('h3', ['Identify']))
- var imageInput = h('span', [
- h('input', {id: 'inp', type:'file'}),
- h('span', {id: 'b64'}),
- h('img', {id: 'img'})
- ])
+ var identify = h('input', {placeholder: 'Your Name'})
- var imagePublisher = h('div', [
- imageInput,
- h('button', {
- onclick: function () {
+ message.appendChild(h('div', [
+ identify,
+ h('button', {onclick: function () {
+ if (identify.value) {
var content = {
author: keys.publicKey,
- type: 'image',
- image: document.getElementById("img").src,
+ type: 'name',
+ naming: keys.publicKey,
+ name: identify.value,
timestamp: Date.now()
}
+ identify.value = ''
publish(content, keys)
- }
- }, ['Publish'])
- ])
-
- profile.appendChild(imagePublisher)
-
- document.getElementById("inp").addEventListener("change", readFile);
-
- } else {
- var subscribees = JSON.parse(localStorage['subscribees'])
- if (subscribees.includes(src)) {
- profile.appendChild(h('button', {
- onclick: function () {
- for (var i = subscribees.length; i--;) {
- if (subscribees[i] === src) {
- subscribees.splice(i, 1);
- localStorage['subscribees'] = JSON.stringify(subscribees)
- window.location.reload()
+ localforage.setItem('id', keys, function (err, published) {
+ if (published) {
+ location.reload()
}
- }
+ })
}
- // remove subscribee
- }, ['UNSUBSCRIBE']))
- } else {
- profile.appendChild(h('button', {
- onclick: function () {
- subscribees.push(src)
- localStorage['subscribees'] = JSON.stringify(subscribees)
- window.location.reload()
- }
- }, ['SUBSCRIBE']))
- }
- }
-
- var pubs = JSON.parse(localStorage['pubs'])
+ }}, ['Identify'])
+ ]))
- for (i = 0; i < pubs.length; i++) {
- requestFeed(src, pubs[i])
- }
-
- 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(h('p', ['When you click [Identify], you will post your first message to your append-only bog, your ed25519 keypair will be saved in your browser, and the page will reload. Don\'t forget to back up your key! and happy bogging.']))
+
+ message.appendChild(h('hr'))
+ message.appendChild(h('h3', ['Already have a key?']))
- else if (src[0] === '%') {
+ message.appendChild(h('p', ['Import it here. Make sure to sync your existing feed from a Bogbook \'pub\' before posting a message.']))
- 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))
+ 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']))
+ }
}
-
- else {
- compose(keys)
-
- var subscribees = JSON.parse(localStorage['subscribees'])
-
- console.log(subscribees)
- for (i = 0; i < subscribees.length; i++) {
- var pubs = JSON.parse(localStorage['pubs'])
- for (n = 0; n < pubs.length; n++) {
- requestFeed(subscribees[i], pubs[n])
- }
- }
-
- localforage.getItem('log', function (err, 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)
- }
- })
- }
- })
-}
+ }
+})
-route()
window.onhashchange = function () {
var oldscreen = document.getElementById('screen')