-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavatar.js
127 lines (114 loc) · 3.17 KB
/
avatar.js
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
import { h, vb } from './lib/misc.js'
import { decode } from './lib/base64.js'
import { keys } from './keys.js'
import { publish, open } from './sbog.js'
import { render } from './render.js'
import { logs, save } from './log.js'
import { make, find } from './blob.js'
import { blast } from './replicate.js'
import { cachekv } from './cachekv.js'
const imagecache = new Map()
const namecache = new Map()
setInterval(function () {
imagecache.clear()
namecache.clear()
}, 10000)
export function plainTextName (id) {
const got = namecache.get(id)
if (got) {
return got
} else {
return id.substring(0, 10) + '...'
}
}
export function getImage (id) {
const img = vb(decode(id), 256)
img.classList = 'avatar'
if (imagecache.has(id)) {
const got = imagecache.get(id)
img.src = got
} else {
logs.query(id).then(querylog => {
if (querylog && querylog[0]) {
querylog.forEach(msg => {
if (!imagecache.has(id)) {
if (msg.text && msg.text.startsWith('image:') && msg.text.substring(50) === id) {
const query = msg.text.substring(6,50)
find(query).then(image => {
if (image) {
imagecache.set(id, image)
img.src = image
}
})
}
}
})
}
})
}
return img
}
export function getName (id) {
const nameDiv = h('span')
nameDiv.textContent = id.substring(0, 10) + '...'
if (namecache.has(id)) {
const got = namecache.get(id)
nameDiv.textContent = got
} else {
logs.query(id).then(querylog => {
if (querylog && querylog[0]) {
querylog.forEach(msg => {
if (!namecache.has(id)) {
if (msg.text && msg.text.startsWith('name:') && msg.text.substring(49) === id) {
const query = msg.text.substring(5, 49)
find(query).then(name => {
cachekv.put('name:' + msg.author, name)
namecache.set(id, name)
nameDiv.textContent = name
})
}
}
})
}
})
}
return nameDiv
}
export function getBoth (id) {
const both = h('span', [
h('a', {href: '#' + id}, [getImage(id)]),
' ',
h('a', {href: '#' + id}, [getName(id)])
])
return both
}
export function newName (id, div) {
const input = h('input', {placeholder: 'Give a name to ' + id.substring(0, 10)})
if (id === keys.pubkey()) {
input.placeholder = 'Give yourself a name.'
}
const nameDiv = h('div', [
input,
h('button', {classList: 'btn', onclick: function () {
if (input.value.length === keys.keypair().length) {
alert('Error: Names cannot be the same length as your keypair!')
}
if (input.value) {
make(input.value).then(made => {
publish('name:' + made + id).then(msg => {
blast(made)
open(msg).then(opened => {
blast(opened.raw)
blast(opened.data)
render(opened).then(rendered => {
div.appendChild(rendered)
})
save()
})
})
})
}
}},['Publish'])
])
return nameDiv
}