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

function getLoc (src) {
  var loc = h('span')
  bog().then(log => {
    if (log) {
      for (var i = 0; i < log.length; i++) {
        if (((log[i].located === src) && (log[i].author === src)) || ((log[i].located === src.key) && (log[i].author === src.author))) {
          // if you've identified someone as something else show that something else
          return loc.textContent = log[i].loc
        }
      }
    }
  })
  return loc
}

function profilePage (src, keys) {

  var timer = setInterval(function () {
    if (window.location.hash.substring(1) != src) {
      clearInterval(timer)
      console.log('stop syncing')
    }
    sync([src], keys)
    console.log('syncing ' + src)
  }, 2500)

  /*timer = function() {
    if (src === window.location.hash.substring(1)) {
      //if (interval < 10000) { interval = interval + 50 }
      sync([src], keys)
      setTimeout(timer, interval)
    }
  }

  timer()*/

  var msg = {}
  msg.author = src

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

  function getDesc (src) {
    var desc = h('span')
    bog().then(log => {
      if (log) {
        for (var i = 0; i < log.length; i++) {
          if ((log[i].descripted === src) && (log[i].author === src)) {
            // if you've identified someone as something else show that something else
            return desc.innerHTML = marked(log[i].description)
          }
        }
      }
    })
    return desc
  }

  function getBg (src, profile) {
    bog().then(log => {
      if (log) {
        for (var i = 0; i < log.length; i++) {
          if ((log[i].backgrounded === src) && (log[i].author === src)) {
            // if you've identified someone as something else show that something else  
            banner.style.height = '300px'
            return banner.style.background = 'fixed top/680px no-repeat url(' + log[i].background + ')'
          }
        }
      }
    })
  }

  getBg(src, profile)

  profileDiv.appendChild(banner)
  profileDiv.appendChild(profile)
  scroller.appendChild(profileDiv)

  profile.appendChild(h('span', {classList: 'right'}, [getLoc(src)]))
  profile.appendChild(h('div', [
    h('a', {href: '#' + src}, [
      getImage(src, keys, 'profileAvatar'),
      getName(src, keys)
    ]),
    profile.appendChild(getDesc(src))
  ]))

  quickName(src).then(name => {
    var mentionsButton = h('button', {
      onclick: function () {
        location.href = '#?' + src
      }
    }, [name + '\'s Mentions'])
    profile.appendChild(mentionsButton)
    var respond = h('button', {
      onclick: function () {
        scroller.insertBefore(composer(keys, msg, name), scroller.childNodes[1])
      }
    }, ['Reply to ' + name])
    profile.appendChild(respond)

    profile.appendChild(h('button', {
      onclick: function () {
        localforage.removeItem(src).then(function () {
          var home = true
          regenerate(home)
        })
      }
    }, ['Delete ' + name + '\'s feed']))
 
    if (src != keys.publicKey) {
      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.hash = '' })
            }
          }, ['Unsubscribe from ' + name]))
        } else {
          profile.appendChild(h('button', {
            onclick: function () {
              subs.push(src)
              localforage.setItem('subscriptions', subs).then(function () { location.hash = '' })
            }
          }, ['Subscribe to ' + name]))
        }
        profile.appendChild(identify(src, profile, keys))
      })
    } else {
      profile.appendChild(identify(src, profile, keys))
    }
   //profile.appendChild(identify(src, profile, keys, name))
  })


  async function addPosts (posts, keys) {
    posts.forEach(function (msg) {
      if (msg.author === src) {
        scroller.appendChild(render(msg, keys))
      }
    })
  }
  bog().then(log => {
    var index = 0
    if (log) {
      var posts = log.slice(index, index + 25)
      addPosts(posts, keys).then(done => {
        index = index + 25
        window.onscroll = function(ev) {
          if (((window.innerHeight + window.scrollY) >= document.body.scrollHeight) && (window.location.hash.substring(1) === src)) {
            posts = log.slice(index, index + 25)
            index = index + 25
            addPosts(posts, keys)
            console.log("Bottom of page")
          }
        }
      })
    }
  })
}

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

  async function addPosts (posts, keys) {
    posts.forEach(function (msg) {
      if (msg.text) {
        if (msg.text.toUpperCase().includes(search)) {
          scroller.appendChild(render(msg, keys))
        }
      }
    })
  }
  bog().then(log => {
    var index = 0
    if (log) {
      var posts = log.slice(index, index + 25)
      addPosts(posts, keys).then(done => {
        index = index + 25
        window.onscroll = function(ev) {
          if (((window.innerHeight + window.scrollY) >= document.body.scrollHeight) && (window.location.hash.substring(1) === src)) {
            posts = log.slice(index, index + 25)
            index = index + 25
            addPosts(posts, keys)
            console.log("Bottom of page")
          }
        }
      })
    }
  })
}

function publicPage (keys) {

  localforage.getItem('log').then(log => {
    log.sort((a, b) => a.timestamp - b.timestamp)
    var reversed = log.reverse()
    localforage.setItem('log', reversed)
  })

  localforage.getItem('subscriptions').then(subs => {
    if (subs) {
      // the next two lines just fix a bug where the first sub was being set to null. Delete this code after March 2020.
      
      subs.forEach(function (sub, index) {
        var timer = setInterval(function () {
          setTimeout(function () {
            if (window.location.hash.substring(1) != '') {
              clearInterval(timer)
              console.log('stop syncing')
            }
            sync([sub], keys)
            console.log('syncing ' + sub)
          }, 1000 * index)
        }, 2500)

        if ((sub == null) || (sub == undefined)) {
          var subs = [keys.publicKey]
          localforage.setItem('subscriptions', subs)
        }
      })
    } else {
      var subs = [keys.publicKey]
      console.log(subs)
      localforage.setItem('subscriptions', subs)
    }
  })

  scroller.appendChild(composer(keys))

  async function addPosts (posts, keys) {
    posts.forEach(function (msg) {
      scroller.appendChild(render(msg, keys))
    })
  }

  bog().then(log => {
    var index = 0
    if (log) {
      var posts = log.slice(index, index + 25)
      addPosts(posts, keys).then(done => {
        index = index + 25
        window.onscroll = function(ev) {
          if (((window.innerHeight + window.scrollY) >= document.body.scrollHeight) && window.location.hash.substring(1) === '') {
            posts = log.slice(index, index + 25)
            index = index + 25
            addPosts(posts, keys)
            console.log("Bottom of page")
          }
        }
      })
    }
  })
}