aboutsummaryrefslogtreecommitdiff
path: root/views.js
blob: 87a4c10eb9b2a318197c1f3c27dc89bed04a09d6 (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
function threadPage (src, keys) {
  get(src).then(msg => {
    scroller.appendChild(render(msg, keys))
  })
}

function profilePage (src, keys) {

  var profile = h('div', {classList: 'profile'})

  scroller.appendChild(profile)


  if (src != keys.publicKey) {
    reply = { author: src }
    scroller.appendChild(composer(keys, reply))
  } else {
    scroller.appendChild(composer(keys))
  }

  var subs = [src]

  sync(subs, keys)

  var input = h('input', {placeholder: 'New name'})

  profile.appendChild(h('a', {href: '#' + src}, [getName(src, keys)]))

  profile.appendChild(h('br'))

  var identify = h('div', [
    input,
    h('button', {
      onclick: function () {
        if (input.value) {
          content = {
            type: 'name',
            named: src,
            name: input.value
          }
          localforage.removeItem('name:' + src)
          publish(content, keys).then(post => {
            open(post).then(msg => {
              input.value = ''
              scroller.insertBefore(render(msg, keys), scroller.childNodes[1])
            })
          })
        }
      }
    }, ['Identify'])
  ])

  var identifyButton = h('button', {
    onclick: function () {
      profile.appendChild(identify)
      identifyButton.parentNode.removeChild(identifyButton) 
    }
  }, ['Identify ' + src.substring(0, 10) + '...'])

  var mentionsButton = h('button', {
    onclick: function () {
      location.href = '#?' + src
    }
  }, ['Mentions'])

  profile.appendChild(identifyButton)

  profile.appendChild(mentionsButton)

  localforage.getItem('subscriptions').then(function (subs) {
    if (subs.includes(src)) {
      profile.appendChild(h('button', {
        onclick: function () {
          subs = subs.filter(a => a !== src)
          localforage.setItem('subscriptions', subs).then(function () { location.reload() })
        }
      }, ['Unsubscribe']))
    } else {
      profile.appendChild(h('button', {
        onclick: function () {
          subs.push(src)
          localforage.setItem('subscriptions', subs).then(function () { location.reload() })
        }
      }, ['Subscribe']))
    }
  })
  
  profile.appendChild(h('button', {
    onclick: function () {
      localforage.removeItem(src).then(function () {
        var home = true
        regenerate(home)
      })
    }
  }, ['Delete feed']))
  
  bog().then(log => {
    if (log) {
      log.forEach(function (msg) {
        if (msg.author === src) {
          scroller.appendChild(render(msg, keys))
        }
      })
    }
  })

  /*bog(src).then(log => {
    if (log) {
      log.forEach(function (msg) {
        open(msg).then(post => {
          scroller.appendChild(render(post, keys))
        })
      })
    }
  })*/
}

function searchPage (src, keys) {
  var search = src.substring(1).replace("%20"," ").toUpperCase()

  //scroller.appendChild(composer(keys))
  bog().then(log => {
    if (log) {
      log.forEach(function (msg) {
        if (msg.text) {
          if (msg.text.toUpperCase().includes(search)) {
            scroller.appendChild(render(msg, keys))
          }
        }
      })
    }
  })
}

function publicPage (keys) {

  localforage.getItem('subscriptions').then(function (subs) {
    if (subs) {
      sync(subs, keys)
    } else {
      var subs = [keys.publicKey]
      localforage.setItem('subscriptions', subs)
      sync(subs, keys)
    }
  })

  var div = h('div')

  div.appendChild(h('button', {
    onclick: function () {
      localforage.clear().then(function () {location.reload()})
    }
  }, ['Delete Everything']))

  div.appendChild(h('button', {
    onclick: function () {
      regenerate()
    }
  }, ['Regenerate']))

  scroller.appendChild(div)

  scroller.appendChild(composer(keys))
  bog().then(log => {
    if (log) {
      log.forEach(function (msg) {
        scroller.appendChild(render(msg, keys))
      })
    }
  })
}

function keyPage (keys) {
  var message = h('div', {classList: 'message'})

  message.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 identity when using [Bogbook](http://bogbook.com/), save your key in a safe place so that you can continue to use the same identity.')}))

  message.appendChild(h('pre', {style: 'width: 80%'}, [h('code', [JSON.stringify(keys)])]))

  message.appendChild(h('button', {
    onclick: function () {
     localforage.removeItem('id', function () {
       location.hash = ''
       location.reload()
     })
    }
  }, ['Delete Key']))

  var textarea = h('textarea', {placeholder: 'Import your existing ed25519 keypair'})
  message.appendChild(textarea)
  message.appendChild(h('button', {
    onclick: function () {
      if (textarea.value) {
        localforage.setItem('id', JSON.parse(textarea.value)).then(function () { location.reload() })
      }
    }
  }, ['Import Key']))

  scroller.appendChild(message)
}

function pubs () {
  var message = h('div', {classList: 'message'})
 
  message.appendChild(h('p', {innerHTML: marked('These are the Bogbook pubs that your browser will connect to as it looks for new messages from your subscriptions, when you post new bog posts, and when you click on feeds.\n\nAdd or remove these pubs to control where your Bogbook gossips. Localhost is a default, but will only work if you install Bogbook on your local computer by [cloning down the repo](https://git.sr.ht/~ev/bogbook).')}))

  var add = h('input', {placeholder: 'Add a pub'})

  localforage.getItem('securepubs').then(function (servers) {

    message.appendChild(h('div', [
      add,
      h('button', {
        onclick: function () {
          if (add.value) {
            servers.push(add.value)
            localforage.setItem('securepubs', servers).then(function () { location.reload() })
          }
        }
      }, ['Add a pub'])
    ]))

    servers.forEach(function (pub) {
      message.appendChild(h('p', [
        pub,
        h('button', {
          onclick: function () {
            var newServers = servers.filter(item => item !== pub)
            localforage.setItem('securepubs', newServers).then(function () { location.reload() })
          }
        }, ['Remove'])
      ]))
    })
  })

  message.appendChild(h('button', {
    onclick: function () {
      localforage.removeItem('securepubs').then(function () {
        location.hash = ''
        location.reload()
      })
    }
  }, ['Reset pubs']))

  scroller.appendChild(message)
}