aboutsummaryrefslogtreecommitdiff
path: root/gossip.js
blob: cd2e107495de8efdb5341e0e37fbe6ebaf50b668 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
function sync (subs, keys) {

  var wsServers

  localforage.getItem('securepubs').then(function (servers) {
    if (servers) {
      wsServers = servers
    } else {
      servers = ['ws://bogbook.com', 'ws://localhost:8080']
      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)
        }
      })
      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)
      })
    })
  })
}