-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.js
104 lines (95 loc) · 2.74 KB
/
process.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
import { h } from './lib/h.js'
import { bogbot } from './bogbot.js'
import { render } from './render.js'
import { markdown } from './markdown.js'
import { gossip } from './gossip.js'
const doWeHave = async (msg, opened) => {
const query = await bogbot.query(opened.hash)
if (query && query[0]) {
} else {
bogbot.add(opened.raw)
shouldWeRender(msg, opened)
}
}
const shouldWeRender = async (msg, opened) => {
const src = window.location.hash.substring(1)
if (src == '' || src == opened.hash || src == opened.author) {
const rendered = await render(opened)
const scroller = document.getElementById('scroller')
scroller.firstChild.after(rendered)
}
}
const updateAvatar = async (msg, opened) => {
const latest = await bogbot.getInfo(opened.author)
if (msg.image || msg.name) {
if (msg.name) {
if (latest.name != msg.name) {
latest.name = msg.name
setTimeout(() => {
const namesOnScreen = document.getElementsByClassName('name' + opened.author)
for (const names of namesOnScreen) {
names.textContent = latest.name
}
}, 100)
}
}
if (msg.image) {
if (latest.image != msg.image) {
latest.image = msg.image
setTimeout(async () => {
const imagesOnScreen = document.getElementsByClassName('image' + opened.author)
for (const image of imagesOnScreen) {
if (latest.image.length == 44) {
const blob = await find(latest.image)
if (blob) {
image.src = blob
} else { gossip(msg.image)}
}
}
}, 100)
}
}
}
await bogbot.saveInfo(opened.author, msg)
}
export const process = async (data, id) => {
try {
const opened = await bogbot.open(data.payload)
if (data.image || data.name) { await updateAvatar(data, opened)}
if (data && data.text) {
const blobhash = await bogbot.make(data.text)
}
await doWeHave(opened)
} catch (err) {
}
try {
const msg = JSON.parse(data)
const opened = await bogbot.open(msg.payload)
if (msg.image || msg.name) { await updateAvatar(msg, opened)}
if (msg && msg.text) {
const blobhash = await bogbot.make(msg.text)
}
await doWeHave(data, opened)
} catch (err) {
}
try {
const opened = await bogbot.open(data)
await doWeHave(data, opened)
} catch (err) {
const hash = await bogbot.make(data)
const got = document.getElementById(hash)
if (got) {
got.innerHTML = await markdown(data)
}
}
if (data.length === 44) {
const query = await bogbot.query(data)
const blob = await bogbot.find(data)
if (query && query[0]) {
gossip(query[0].raw)
}
if (blob) {
gossip(blob)
}
}
}