aboutsummaryrefslogtreecommitdiff
path: root/gossip.js
blob: 26915b13ff10f105a4d5f22e248e0cae25eff095 (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
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
              }))
            })
          }

          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
              }))
            })
          }
        })
      }
    })
  }

  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)
                }
              })
            }
          })
        } 
        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)
                    }
                  })
                }
              })
            }
          })
        }
      })
    })
  }
}

function getpubkey (connection, keys) {
  connection.onopen = () => {
    connection.send(JSON.stringify({
      requester: keys.publicKey, sendpub: true
    }))
  }

  connection.onmessage = (m) => {
    localforage.setItem(m.origin, m.data)
  }
}

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)
      })
    }
  })
}

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)
    })
  })
}