From bd14700d559f08e1585cf6ae40b6f78c9cd295eb Mon Sep 17 00:00:00 2001 From: Ev Bogue Date: Thu, 25 Apr 2019 21:16:17 -0500 Subject: this isnt quite working yet, but commiting anyway --- app.js | 14 ++++------- bog.js | 12 ++++++++-- css/style.css | 4 ++-- gossip.js | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ index.html | 1 + render.js | 12 +++++++--- server.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++- views.js | 14 +++++++---- 8 files changed, 175 insertions(+), 22 deletions(-) create mode 100644 gossip.js diff --git a/app.js b/app.js index 26b06eb..14a8d51 100644 --- a/app.js +++ b/app.js @@ -25,17 +25,11 @@ function composer (keys, reply) { 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)])) - } + } + if (messageDiv.firstChild) { + messageDiv.insertBefore(render(msg, keys), messageDiv.childNodes[1]) } else { - if (messageDiv.firstChild) { - messageDiv.insertBefore(render(msg, keys), messageDiv.childNodes[1]) - } else { - messageDiv.appendChild(render(msg, keys)) - } + messageDiv.appendChild(render(msg, keys)) } }) }) diff --git a/bog.js b/bog.js index aa347ce..26cc409 100644 --- a/bog.js +++ b/bog.js @@ -25,7 +25,15 @@ async function keys (key) { publicKey: '@' + nacl.util.encodeBase64(genkey.publicKey), privateKey: nacl.util.encodeBase64(genkey.secretKey) } - localforage.setItem('id', keypair) + if (keypair.publicKey.includes('/')) { + console.log('TRYING AGAIN') + setTimeout(function () { + location.reload() + }, 10) + } else { + localforage.setItem('id', keypair) + } + return keypair } } @@ -103,7 +111,7 @@ async function publish (post, keys) { } else { - post.seq = 0 + post.seq = 1 message.key = '%' + nacl.util.encodeBase64(nacl.hash(nacl.util.decodeUTF8(JSON.stringify(post)))), message.signature = nacl.util.encodeBase64(nacl.sign(nacl.util.decodeUTF8(JSON.stringify(post)), nacl.util.decodeBase64(keys.privateKey))) diff --git a/css/style.css b/css/style.css index 7a517d1..ae1256b 100644 --- a/css/style.css +++ b/css/style.css @@ -56,14 +56,14 @@ hr { border-radius: 5px; } -.message, .message > *, .navbar, .navbar > * { +/*.message, .message > *, .navbar, .navbar > * { animation: fadein .5s; } @keyframes fadein { from { opacity: 0; } to { opacity: 1; } -} +}*/ .submessage { diff --git a/gossip.js b/gossip.js new file mode 100644 index 0000000..b598dde --- /dev/null +++ b/gossip.js @@ -0,0 +1,74 @@ +function sync (src, server, keys) { + var ws = new WebSocket(server + src) + + console.log(server) + console.log(src) + console.log(keys.publicKey) + + bog(src).then(srclog => { + if (srclog) { + open(srclog[0]).then(msg => { + var req = { + src, + seq: msg.seq, + requester: keys.publicKey + } + console.log(req) + ws.onopen = function () { + ws.send(JSON.stringify(req)) + } + ws.onmessage = function (message) { + var serverMsg = JSON.parse(message.data) + console.log(serverMsg) + if (msg.seq === serverMsg.seq) { + console.log('DO NOTHING') + } else if (msg.seq > serverMsg.seq) { + console.log('SENDING') + var diff = msg.seq - serverMsg.seq + console.log(diff) + var sendlog = srclog.slice(0, diff) + console.log(sendlog) + var send = { + src, + log: sendlog, + requester: keys.publicKey + } + console.log(send) + ws.send(JSON.stringify(send)) + } else { + console.log('RECEIVING') + + } + } + }) + } else { + console.log('NO LOG!') + var req = { + src, + seq: null, + requester: keys.publicKey + } + + ws.onopen = function () { + ws.send(JSON.stringify(req)) + } + ws.onmessage = function (message) { + var serverMsg = JSON.parse(message.data) + console.log(serverMsg) + localforage.getItem('log').then(log => { + if (log) { + var newlog = serverMsg.log.concat(log) + + localforage.setItem('log', newlog) + } + }) + + localforage.setItem(src, serverMsg.log) + + setTimeout(function () { + location.reload() + }, 1000) + } + } + }) +} diff --git a/index.html b/index.html index 712f2d3..2e8a7e7 100644 --- a/index.html +++ b/index.html @@ -13,6 +13,7 @@ + diff --git a/render.js b/render.js index 3460579..3920b49 100644 --- a/render.js +++ b/render.js @@ -14,9 +14,10 @@ function getHeader (post, mini) { } function render (msg, keys) { - var messageDiv = h('messageDiv', {id: msg.key}) + var messageDiv = h('div', {id: msg.key}) var message = h('div', {classList: 'message'}) + /* TODO: this is really slow for some reason bog().then(logger => { logger.forEach(function (nextPost) { open(nextPost).then(nextMessage => { @@ -28,7 +29,7 @@ function render (msg, keys) { } }) }) - }) + })*/ if (msg.type == 'post') { message.appendChild(getHeader(msg)) @@ -43,7 +44,12 @@ function render (msg, keys) { message.appendChild(h('div', [msg.text])) message.appendChild(h('button', { onclick: function () { - messageDiv.appendChild(composer(keys, msg)) + if (messageDiv.firstChild) { + messageDiv.insertBefore(h('div', {classList: 'submessage'}, [composer(keys, msg)]), messageDiv.childNodes[1]) + } else { + messageDiv.appendChild(h('div', {classList: 'submessage'}, [composer(keys, msg)])) + } + } }, ['Reply'])) } diff --git a/server.js b/server.js index 858687e..ddd91a9 100644 --- a/server.js +++ b/server.js @@ -21,7 +21,71 @@ var wserver = new WebSocket.Server({ port: 8080 }) wserver.on('connection', function (ws) { ws.on('message', function (message) { + var req = JSON.parse(message) + console.log(req) + if (req.seq) { + if (fs.existsSync(__dirname + '/bogs/' + req.src)) { + fs.readFile(__dirname + '/bogs/' + req.src, 'UTF-8', function (err, data) { + if (data) { + var log = JSON.parse(data) + console.log(log[0]) + var pubkey = nacl.util.decodeBase64(req.src.substring(1)) + var sig = nacl.util.decodeBase64(log[0].signature) + var opened = JSON.parse(nacl.util.encodeUTF8(nacl.sign.open(sig, pubkey))) - }) + console.log(opened) + var res = { + feed: req.src, + seq: opened.seq + } + ws.send(JSON.stringify(res)) + // COMPARE SEQ + } + }) + } else { + var res = { + feed: req.src, + seq: null + } + console.log(res) + ws.send(JSON.stringify(res)) + } + } else { + if (fs.existsSync(__dirname + '/bogs/' + req.src)) { + fs.readFile(__dirname + '/bogs/' + req.src, 'UTF-8', function (err, data) { + var log = JSON.parse(data) + var res = { + src: req.src, + log + } + console.log('SENDING FULL LOG') + ws.send(JSON.stringify(res)) + }) + } + } + if (req.log) { + console.log('LOG') + console.log(req.log) + if (fs.existsSync(__dirname + '/bogs/' + req.src)) { + fs.readFile(__dirname + '/bogs/' + req.src, 'UTF-8', function (err, data) { + var serverlog = JSON.parse(data) + + var newlog = req.log.concat(serverlog) + fs.writeFile(__dirname + '/bogs/' + req.src, JSON.stringify(newlog), 'UTF-8', function (err, success) { + console.log('APPENDED DIFF AND WROTE LOG') + }) + + console.log('TRY APPENDING') + // read bogfile, append received data, and then save again + }) + } else { + fs.writeFile(__dirname + '/bogs/' + req.src, JSON.stringify(req.log), 'UTF-8', function (err, success) { + if (err) throw err + else + console.log('WROTE LOG') + }) + } + } + }) }) diff --git a/views.js b/views.js index 94fcb5d..1c4dca3 100644 --- a/views.js +++ b/views.js @@ -7,12 +7,18 @@ function threadPage (src, keys) { } function profilePage (src, keys) { + var server = 'ws://localhost:8080/' + + sync(src, server, keys) + bog(src).then(log => { - log.forEach(function (msg) { - open(msg).then(post => { - scroller.appendChild(render(post, keys)) + if (log) { + log.forEach(function (msg) { + open(msg).then(post => { + scroller.appendChild(render(post, keys)) + }) }) - }) + } }) } -- cgit v1.2.3-70-g09d2