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

function profilePage (src, keys) {
  var msg = {}
  msg.author = src

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

  scroller.appendChild(profile)
  //scroller.appendChild(h('div'))

  var subs = [src]

  var interval = 500
  timer = function() {
    if (src === window.location.hash.substring(1)) {
      if (interval < 10000) { interval = interval + 100 }
      sync(subs, keys)
      setTimeout(timer, interval)
    }
  }
  timer()

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

  profile.appendChild(h('br'))

  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)

    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.reload() })
            }
          }, ['Unsubscribe from ' + name]))
        } else {
          profile.appendChild(h('button', {
            onclick: function () {
              subs.push(src)
              localforage.setItem('subscriptions', subs).then(function () { location.reload() })
            }
          }, ['Subscribe to ' + name]))
        }
      })
    }

    profile.appendChild(h('button', {
      onclick: function () {
        localforage.removeItem(src).then(function () {
          var home = true
          regenerate(home)
        })
      }
    }, ['Delete ' + name + '\'s feed']))
  })

  profile.appendChild(identify(src, profile, keys))

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

  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) {
    var interval = 1000
    timer = function() {
      if ('' === window.location.hash.substring(1)) {
        if (interval < 10000) { interval = interval + 1000 }
        sync(subs, keys)
        setTimeout(timer, interval)
      }
    }
    if (subs) {
      timer()
    } else {
      var subs = []
      localforage.setItem('subscriptions', subs)
      subs.push(keys.publicKey)
      timer()
    }
  })

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