aboutsummaryrefslogtreecommitdiff
path: root/app.js
blob: 2b2f05bd772004306e0d472fd23cd5018eff565a (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
var screen = h('div', {id: 'screen'})
document.body.appendChild(screen)

localforage.getItem('id', function (err, value) {
  // the navbar has a dual purpose of generating a key if you don't already have one

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

if (!localStorage['subscribees']) {
  var subscribees = ['@218Fd2bCrmXe4gwnMg5Gcb9qVZrjXquym2AlelbkBro=']
  localStorage['subscribees'] = JSON.stringify(subscribees)
}

if (!localStorage['pubs']) {
  var pubs = ['ws://bogbook.com/', 'ws://localhost:8080/']
  localStorage['pubs'] = JSON.stringify(pubs)
}

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 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) {
            localStorage['id'] = textarea.value
            location.reload()
          }
        }
      }, ['Import Key']))

      scroller.appendChild(keyMessage)

      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 
      }

      for (i = 0; i < pubs.length; i++) {
        var pubName = pubs[i]
        pubMessage.appendChild(h('p', [
          pubName,
          removeButton(pubName)
        ]))
      }    

      scroller.appendChild(pubMessage)
    }



    else if (src[0] === '@') {
      var profile = h('div', {classList: 'message'})
      scroller.appendChild(profile)

      if (src == keys.publicKey) {
        var nameInput = h('input', {placeholder: 'Publish a new name'})

        var namePublisher = h('div',[
          nameInput,
          h('button', {
            onclick: function () {
              if (nameInput.value) {

                var content = {
                  author: keys.publicKey,
                  type: 'name',
                  text: nameInput.value,
                  timestamp: Date.now()
                }

                publish(content, keys)
              }
            }
          }, ['Publish'])
        ])

        profile.appendChild(namePublisher)

        readFile()

        var imageInput = h('span', [
          h('input', {id: 'inp', type:'file'}),
          h('span', {id: 'b64'}),
          h('img', {id: 'img'})
        ])

        var imagePublisher = h('div', [
          imageInput,
          h('button', {
            onclick: function () {
              var content = {
                author: keys.publicKey,
                type: 'image',
                image: document.getElementById("img").src,
                timestamp: Date.now()
              }

              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()
                }
              }
            }
            // 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'])

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

    else if (src[0] === '%') {

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

    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')
  var newscreen = h('div', {id: 'screen'})
  oldscreen.parentNode.replaceChild(newscreen, oldscreen)
  route()
}