aboutsummaryrefslogtreecommitdiff
path: root/beacons.js
blob: 16568792860273d9f3f79e49d514c1ebb11659ce (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
function beaconsPage (keys) {

  var pubslist = h('select')

  localforage.getItem('securepubs').then(function (servers) {
    servers.forEach(function (pub) {
      pubslist.appendChild(
        h('option', {value: pub}, [pub])
      )
    })
  })
  
  var ads = h('div', {classList: 'message'})

  ads.appendChild(h('span', {innerHTML: marked('Sometimes when you\'re lost on the Internet you might want to send out a beacon so that people can see you. \n\n Beacons from guests will run for 100 views before they are deleted by the pub. \n\nSelect a pub:')}))

  ads.appendChild(pubslist)

  var recp = h('input', {placeholder: 'Ex: @Q++V5BbvWIg8B+TqtC9ZKFhetruuw+nOgxEqfjlOZI0='})

  var adstext = h('textarea', {placeholder: 'Hello World!'})

  ads.appendChild(h('span', [
    h('br'),
    h('p', [" Send a beacon (leave the 'To:' field blank for a public beacon): "]),
    h('p', ['To: ',
      recp
    ]),
    adstext,
    h('br'),
    h('button', {
      onclick: function () {
        var split = pubslist.value.split('~')
        console.log(split)
        var serverurl = split[0]
        var serverpub = split[1]
        var ws = new WebSocket(serverurl)

        if ((recp.value) && (adstext.value)) {
          var tobox = {
            author: keys.publicKey,
            timestamp: Date.now(),
            content: adstext.value
          }
          box(JSON.stringify(tobox), recp.value, keys).then(boxedmsg => {
            var msg = {
              type: 'beacon',
              author: keys.publicKey,
              box: boxedmsg
            }
            ws.onopen = function () {
              box(JSON.stringify(msg), serverpub, keys).then(boxed => {
                var obj = {
                  requester: keys.publicKey,
                  box: boxed
                }
                ws.send(JSON.stringify(obj))
              })
              adstext.value = ''
              recp.value = ''
            }
          })
        }

        if ((!recp.value) && (adstext.value)) {
          var msg = {
            type: 'beacon',
            author: keys.publicKey
          }
          msg.signature = nacl.util.encodeBase64(nacl.sign(nacl.util.decodeUTF8(JSON.stringify(adstext.value)), nacl.util.decodeBase64(keys.privateKey)))
          ws.onopen = function () {
            box(JSON.stringify(msg), serverpub, keys).then(boxed => {
              var obj = {
                requester: keys.publicKey,
                box: boxed
              }
              ws.send(JSON.stringify(obj))
            })
            adstext.value = ''
          }
        }
      }
    }, ['Publish'])
  ]))

  scroller.appendChild(ads)
}