aboutsummaryrefslogtreecommitdiff
path: root/gossip.js
diff options
context:
space:
mode:
Diffstat (limited to 'gossip.js')
-rw-r--r--gossip.js417
1 files changed, 175 insertions, 242 deletions
diff --git a/gossip.js b/gossip.js
index d6785c0..8fdf55f 100644
--- a/gossip.js
+++ b/gossip.js
@@ -1,267 +1,200 @@
-function blobSync (blob, author, keys, needs) {
- console.log(needs)
+function processreq (req, pubkey, connection, keys) {
+ if (req.seq === 0 || req.seq) {
+ bog(req.author).then(feed => {
+ if (feed) {
+ open(feed[0]).then(msg => {
+ if (req.seq > msg.seq) {
+ var reqdiff = JSON.stringify({author: req.author, seq: msg.seq})
+ box(reqdiff, pubkey, keys).then(boxed => {
+ connection.send(JSON.stringify({
+ requester: keys.publicKey,
+ box: boxed
+ }))
+ })
+ }
- var wsServers
+ if (req.seq < msg.seq) {
+ var endrange = feed.length - req.seq - 25
+ if (endrange < 0) {
+ endrange = feed.length - req.seq - 1
+ }
+ var baserange = feed.length - req.seq
+ var diff = JSON.stringify(
+ feed.slice(
+ endrange,
+ baserange)
+ )
+ box(diff, pubkey, keys).then(boxed => {
+ connection.send(JSON.stringify({
+ requester: keys.publicKey,
+ box: boxed
+ }))
+ })
+ }
+ })
+ }
+ })
+ }
- // duplicate code, we should abstract this
- localforage.getItem('securepubs').then(function (servers) {
- if (servers) {
- wsServers = servers
- } else {
- servers = ['wss://interskri.be/ws', 'ws://localhost:8080', 'wss://interskri.be/bogbook', 'ws://bogbook.com']
- var pubs = []
- servers.forEach(server => {
- var ws = new WebSocket(server)
- ws.onopen = function () {
- ws.send(JSON.stringify({requester: keys.publicKey, sendpub: true}))
- }
- ws.onmessage = function (message) {
- pubs.push(server + '/~' + message.data)
- localforage.setItem('securepubs', pubs)
- }
+ if (req.latest) {
+ var latest
+ latest = document.getElementById('latest')
+ var src = window.location.hash.substring(1)
+ if ((!latest) && (src == req.latest)) {
+ latest = h('div', {id: 'latest'})
+ latest.appendChild(h('div', {classList: 'message', innerHTML: marked('**Ankoraŭ sinkronigas la fluon**. Intertempe jen la lastaj kvin mesaĝoj...')
+ }))
+ req.feed.forEach(post => {
+ open(post).then(msg => {
+ latest.appendChild(render(msg, keys))
+ })
})
- wsServers = pubs
- }
- }).then(function () {
- wsServers.forEach(function (server, index) {
- setTimeout(function () {
- var split = server.split('~')
- var serverurl = split[0]
- var serverpub = split[1]
- var ws = new WebSocket(serverurl)
+ scroller.firstChild.appendChild(latest)
- console.log('requesting ' + blob + ' from ' + server)
- ws.onopen = function () {
- var req = {
- blob: blob,
- needs: needs
- }
+ var timer = setInterval(function () {
+ localforage.getItem(req.latest).then(feed => {
+ open(feed[0]).then(msg => {
+ open(req.feed[0]).then(latestmsg => {
+ src = window.location.hash.substring(1)
+ if (msg.seq >= latestmsg.seq) {
+ latest.parentNode.removeChild(latest)
+ clearInterval(timer)
+ //console.log('we are caught up, deleting latest div')
+ }
+ if (src != req.latest) {
+ clearInterval(timer)
+ //console.log('we navigated away')
+ }
+ })
+ })
+ })
+ //console.log('checking to see if we have caught up')
+ }, 5000)
+ }
+ }
- box(JSON.stringify(req), serverpub, keys).then(boxed => {
- var obj = {
- requester: keys.publicKey,
- box: boxed
+ if (Array.isArray(req)) {
+ open(req[0]).then(msg => {
+ localforage.getItem(msg.author).then(feed => {
+ if (!feed) {
+ localforage.setItem(msg.author, req)
+ localforage.getItem('log').then(log => {
+ if (!log) { var log = [] }
+ for (var i = req.length -1; i >= 0; --i) {
+ open(req[i]).then(opened => {
+ log.unshift(opened)
+ var src = window.location.hash.substring(1)
+ if ((src === msg.author) || (src === '')) {
+ var scroller = document.getElementById('scroller')
+ scroller.insertBefore(render(opened, keys), scroller.childNodes[1])
+ }
+ if (opened.seq === req.length) {
+ localforage.setItem('log', log)
+ }
+ })
}
- ws.send(JSON.stringify(obj))
})
- }
- ws.onmessage = function (message) {
- var serverreq = JSON.parse(message.data)
- unbox(serverreq.box, serverreq.requester, keys).then(unboxed => {
-
- var unboxedreq = JSON.parse(nacl.util.encodeUTF8(unboxed))
- //console.log(unboxedreq)
- if (unboxedreq.blobFile) {
- var openedimg = nacl.sign.open(nacl.util.decodeBase64(unboxedreq.blobFile), nacl.util.decodeBase64(author.substring(1)))
- if (openedimg) {
- localforage.setItem(unboxedreq.blob, unboxedreq.blobFile)
- }
- } else {
- localforage.getItem(unboxedreq.blob).then(blob => {
- if (blob) {
- var nextreq = {
- author: author,
- blob: unboxedreq.blob,
- blobFile: blob
- }
- box(JSON.stringify(nextreq), serverreq.requester, keys).then(boxed => {
- var obj = {
- requester: keys.publicKey,
- box: boxed
+ }
+ if (feed) {
+ open(feed[0]).then(lastmsg => {
+ if (req.length + lastmsg.seq === msg.seq) {
+ var newlog = req.concat(feed)
+ localforage.setItem(msg.author, newlog)
+ localforage.getItem('log').then(log => {
+ if (!log) { var log = [] }
+ for (var i = req.length -1; i >= 0; --i) {
+ open(req[i]).then(opened => {
+ log.unshift(opened)
+ var src = window.location.hash.substring(1)
+ if ((src === msg.author) || (src === '')) {
+ var scroller = document.getElementById('scroller')
+ scroller.insertBefore(render(opened, keys), scroller.childNodes[1])
+ }
+ if (req.length + lastmsg.seq === opened.seq) {
+ localforage.setItem('log', log)
}
- ws.send(JSON.stringify(obj))
})
}
})
}
})
}
- }, index * 10000)
+ })
})
- })
+ }
}
-function sync (subs, keys) {
+function getpubkey (connection, keys) {
+ connection.onopen = () => {
+ connection.send(JSON.stringify({
+ requester: keys.publicKey, sendpub: true
+ }))
+ }
- var wsServers
+ connection.onmessage = (m) => {
+ localforage.setItem(m.origin, m.data)
+ }
+}
- localforage.getItem('securepubs').then(function (servers) {
- if (servers) {
- wsServers = servers
- } else {
- servers = ['wss://interskri.be/ws', 'ws://localhost:8080', 'wss://interskri.be/bogbook', 'ws://bogbook.com']
- var pubs = []
- servers.forEach(server => {
- var ws = new WebSocket(server)
- ws.onopen = function () {
- ws.send(JSON.stringify({requester: keys.publicKey, sendpub: true}))
- }
- ws.onmessage = function (message) {
- pubs.push(server + '/~' + message.data)
- localforage.setItem('securepubs', pubs)
- }
+function getfeed (feed, pubkey, connection, keys) {
+ bog(feed).then(log => {
+ var logseq = 0
+ connection.onopen = () => {
+ if (log) {
+ open(log[0]).then(msg => {
+ var string = JSON.stringify(msg)
+ box(string, pubkey, keys).then(boxed => {
+ connection.send(JSON.stringify({
+ requester: keys.publicKey,
+ box: boxed
+ }))
+ })
+ logseq = msg.seq
+ })
+ } else {
+ var msg = {
+ author: feed,
+ seq: logseq
+ }
+ box(JSON.stringify(msg), pubkey, keys).then(boxed => {
+ connection.send(JSON.stringify({
+ requester: keys.publicKey,
+ box: boxed
+ }))
+ })
+ }
+ }
+ connection.onmessage = (m) => {
+ var req = JSON.parse(m.data)
+ unbox(req.box, req.requester, keys).then(unboxed => {
+ var unboxedreq = JSON.parse(unboxed)
+ processreq(unboxedreq, pubkey, connection, keys)
})
- wsServers = pubs
}
- }).then(function () {
- subs.forEach(function (sub) {
- wsServers.forEach(function (server, index) {
- setTimeout(function () {
- console.log('SYNCING WITH: ' + server + ' to fetch ' + sub)
- var split = server.split('~')
- var serverurl = split[0]
- var serverpub = split[1]
- var ws = new WebSocket(serverurl)
-
- bog(sub).then(srclog => {
- if (srclog) {
- open(srclog[0]).then(msg => {
- ws.onopen = function () {
- var message = JSON.stringify(msg)
- // if we have a log then send the latest log and see if we get anything back
- box(message, serverpub, keys).then(boxed => {
- var obj = {
- requester: keys.publicKey,
- box: boxed
- }
- ws.send(JSON.stringify(obj))
- })
- }
- ws.onmessage = function (message) {
- var req = JSON.parse(message.data)
- console.log(req)
- unbox(req.box, req.requester, keys).then(unboxed => {
- var unboxedreq = JSON.parse(nacl.util.encodeUTF8(unboxed))
- console.log(unboxedreq)
- if (unboxedreq.seq === 0) {
- var stringedfeed = JSON.stringify(srclog)
- box(stringedfeed, serverpub, keys).then(boxed => {
- var obj = {
- requester: keys.publicKey,
- box: boxed
- }
- console.log('Sending entire log of ' + msg.author + ' to ' + serverpub)
- console.log(obj)
- ws.send(JSON.stringify(obj))
- })
- }
-
- if (unboxedreq.seq > msg.seq) {
- console.log('server feed is longer, requesting diff from server')
- var reqdiff = JSON.stringify({author: unboxedreq.author, seq: msg.seq})
- box(reqdiff, serverpub, keys).then(boxed => {
- var obj = {
- requester: keys.publicKey,
- box: boxed
- }
- ws.send(JSON.stringify(obj))
- })
- }
-
- if (unboxedreq.seq < msg.seq) {
- console.log('server feed is shorter, sending diff to server')
- var diff = JSON.stringify(srclog.slice(0, msg.seq - unboxedreq.seq))
- box(diff, serverpub, keys).then(boxed => {
- var obj = {
- requester: keys.publicKey,
- box: boxed
- }
- ws.send(JSON.stringify(obj))
- })
- }
+ })
+}
- if (Array.isArray(unboxedreq)) {
- console.log('received diff from server')
- open(unboxedreq[0]).then(msg => {
- localforage.getItem(msg.author).then(feed => {
- open(feed[0]).then(lastmsg => {
- if (unboxedreq.length + lastmsg.seq === msg.seq) {
- var newlog = unboxedreq.concat(feed)
- localforage.setItem(msg.author, newlog).then(success => {
- console.log('combined existing feed of ' + msg.author + ' with diff and saved to client')
- })
- localforage.getItem('log').then(log => {
- if (!log) {
- var log = []
- }
- for (var i = unboxedreq.length -1; i >= 0; --i) {
- open(unboxedreq[i]).then(opened => {
- log.unshift(opened)
- var scroller = document.getElementById('scroller')
- scroller.insertBefore(render(opened, keys), scroller.childNodes[2])
- if (unboxedreq.length + lastmsg.seq === opened.seq) {
- log.sort((a, b) => a.timestamp - b.timestamp)
- var reversed = log.reverse()
- localforage.setItem('log', reversed).then(success => {
- console.log('saved log with ' + opened.author + ' prepended to localForage')
- })
- }
- })
- }
- })
- }
- })
- })
- })
-
- }
-
- })
- }
- })
- } else {
- console.log('NO LOG IN CLIENT')
- ws.onopen = function () {
- var reqwhole = JSON.stringify({author: sub, seq: 0})
- box(reqwhole, serverpub, keys).then(boxed => {
- var obj = {
- requester: keys.publicKey,
- box: boxed
- }
- ws.send(JSON.stringify(obj))
- })
- }
- ws.onmessage = function (message) {
- var req = JSON.parse(message.data)
- console.log('received message from ' + req.requester)
- unbox(req.box, req.requester, keys).then(unboxed => {
- var unboxedreq = JSON.parse(nacl.util.encodeUTF8(unboxed))
- if (Array.isArray(unboxedreq)) {
- open(unboxedreq[0]).then(msg => {
- localforage.getItem(msg.author).then(feed => {
- if (!feed) {
- localforage.setItem(msg.author, unboxedreq).then(success => {
- console.log('saved log of ' + msg.author + ' to localforage')
- })
- localforage.getItem('log').then(log => {
- if (!log) {
- var log = []
- }
- for (var i = unboxedreq.length -1; i >= 0; --i) {
- open(unboxedreq[i]).then(opened => {
- log.unshift(opened)
- var scroller = document.getElementById('scroller')
- scroller.insertBefore(render(opened, keys), scroller.childNodes[2])
- if (opened.seq === unboxedreq.length) {
- log.sort((a, b) => a.timestamp - b.timestamp)
- var reversed = log.reverse()
- localforage.setItem('log', reversed).then(success => {
- console.log('saved log with ' + opened.author + ' prepended to localForage')
- })
- }
- })
- }
- })
- }
- })
- })
- }
- })
- }
- }
- })
- }, index * 100000)
- })
+function sync (feeds, keys) {
+ var pubs
+ localforage.getItem('pubs').then(pubs => {
+ if (!pubs) {
+ pubs = ['ws://' + location.hostname + ':8080', 'ws://bogbook.com']
+ localforage.setItem('pubs', pubs)
+ }
+ pubs.forEach(function (pub, index) {
+ setTimeout(function () {
+ var connection = new WebSocket(pub)
+ localforage.getItem(pub).then(pubkey => {
+ if (!pubkey) {
+ getpubkey(connection, keys)
+ }
+ if (pubkey) {
+ feeds.forEach(feed => {
+ getfeed(feed, pubkey, connection, keys)
+ })
+ }
+ })
+ }, index * 5000)
})
})
}