tangled
alpha
login
or
join now
evbogue.com
/
wiredove
1
fork
atom
a demonstration replicated social networking web app built with anproto
wiredove.net/
social
ed25519
protocols
1
fork
atom
overview
issues
pulls
pipelines
refactor gossip strategy into similar methods
Everett Bogue
9 months ago
b6ed1eb9
302b10cd
+102
-140
8 changed files
expand all
collapse all
unified
split
app.js
composer.js
connect.js
gossip.js
render.js
route.js
send.js
websocket.js
-4
app.js
···
10
10
document.body.appendChild(await navbar())
11
11
12
12
await route()
13
13
-
await bogbot.query()
14
13
await connect()
15
14
16
16
-
//await bogbot.keypair().then(async (pubkey) => {
17
17
-
// if (pubkey) { await connect()}
18
18
-
//})
+1
-5
composer.js
···
1
1
import { bogbot } from 'bogbot'
2
2
import { render } from './render.js'
3
3
-
import { blast } from './gossip.js'
4
3
import { h } from 'h'
5
4
import { avatarSpan, nameSpan } from './profile.js'
6
5
import { ntfy } from './ntfy.js'
7
7
-
import { send } from './connect.js'
6
6
+
import { send } from './send.js'
8
7
import { markdown } from './markdown.js'
9
8
10
9
export const composer = async (sig) => {
···
56
55
const opened = await bogbot.open(signed)
57
56
58
57
const blob = await bogbot.get(opened.substring(13))
59
59
-
console.log(blob)
60
60
-
await blast(pubkey, signed)
61
61
-
await blast(pubkey, blob)
62
58
await ntfy(signed)
63
59
await ntfy(blob)
64
60
await send(signed)
+2
-67
connect.js
···
1
1
import { bogbot } from 'bogbot'
2
2
import { makeRoom } from './gossip.js'
3
3
-
import { render } from './render.js'
3
3
+
import { makeWs} from './websocket.js'
4
4
5
5
await bogbot.start('wiredovedbversion1')
6
6
7
7
-
const pubs = new Set()
8
8
-
9
9
-
export const send = async (msg) => {
10
10
-
pubs.forEach(pub => {
11
11
-
pub.send(msg)
12
12
-
})
13
13
-
}
14
14
-
15
15
-
const startWs = async (pub) => {
16
16
-
const ws = new WebSocket(pub)
17
17
-
18
18
-
ws.onopen = async () => {
19
19
-
console.log('OPEN')
20
20
-
pubs.add(ws)
21
21
-
const p = await bogbot.getPubkeys()
22
22
-
for (const pub of p) {
23
23
-
ws.send(pub)
24
24
-
const latest = await bogbot.getLatest(pub)
25
25
-
if (latest.text) {
26
26
-
ws.send(latest.text)
27
27
-
} else {
28
28
-
const blob = await bogbot.get(latest.opened.substring(13))
29
29
-
if (blob) {ws.send(blob)}
30
30
-
}
31
31
-
ws.send(latest.sig)
32
32
-
}
33
33
-
//below sends everything in the client to a dovepub pds server
34
34
-
const log = await bogbot.query()
35
35
-
if (log) {
36
36
-
for (const msg of log) {
37
37
-
ws.send(msg.sig)
38
38
-
ws.send(msg.text)
39
39
-
if (!msg.text) { ws.send(await bogbot.get(msg.opened.substring(13)))}
40
40
-
}
41
41
-
}
42
42
-
}
43
43
-
44
44
-
ws.onmessage = async (m) => {
45
45
-
if (m.data.length === 44) {
46
46
-
//console.log('NEEDS' + m.data)
47
47
-
const blob = await bogbot.get(m.data)
48
48
-
if (blob) {
49
49
-
ws.send(blob)
50
50
-
}
51
51
-
} else {
52
52
-
await render.shouldWe(m.data)
53
53
-
await bogbot.add(m.data)
54
54
-
await render.blob(m.data)
55
55
-
}
56
56
-
}
57
57
-
58
58
-
ws.onclose = async () => {
59
59
-
console.log('CLOSED')
60
60
-
pubs.delete(ws)
61
61
-
}
62
62
-
}
63
63
-
64
7
export const connect = async () => {
65
65
-
await startWs('wss://pub.wiredove.net/')
8
8
+
await makeWs('wss://pub.wiredove.net/')
66
9
await makeRoom('wiredovev1')
67
67
-
//await startWs('ws://localhost:9000')
68
68
-
//const pubkeys = await bogbot.getPubkeys()
69
69
-
//if (pubkeys) {
70
70
-
// for (const p of pubkeys) {
71
71
-
// console.log(p)
72
72
-
// await makeRoom(p)
73
73
-
// }
74
74
-
//}
75
10
}
+15
-57
gossip.js
···
1
1
import { bogbot } from 'bogbot'
2
2
import { joinRoom } from './trystero-torrent.min.js'
3
3
-
import { render } from './render.js'
4
4
-
import { send } from './connect.js'
5
5
-
6
6
-
export const rooms = new Map()
3
3
+
import { render } from './render.js'
7
4
8
8
-
const queue = new Set()
9
9
-
10
10
-
export const gossip = async (hash) => {
11
11
-
let speed = 1
5
5
+
export let chan
12
6
13
13
-
const ask = async () => {
14
14
-
const haveBlob = await bogbot.get(hash)
15
15
-
if (haveBlob) {
16
16
-
await bogbot.add(haveBlob)
17
17
-
queue.delete(hash)
18
18
-
}
19
19
-
if (queue.has(hash)) {
20
20
-
speed++
21
21
-
const values = [...rooms.values()]
22
22
-
const room = values[Math.floor(Math.random() * values.length)]
23
23
-
if (room && room.sendHash) {
24
24
-
room.sendHash(hash)
25
25
-
setTimeout(() => {
26
26
-
if (speed === 100) {
27
27
-
queue.delete(hash)
28
28
-
}
29
29
-
ask()
30
30
-
}, (10000 * speed))
31
31
-
}
7
7
+
export const sendTry = (m) => {
8
8
+
if (chan) {
9
9
+
if (m.length === 44) { chan.sendHash(m)} else {
10
10
+
chan.sendBlob(m)
32
11
}
33
33
-
}
34
34
-
if (!queue.has(hash)) {
35
35
-
send(hash)
36
36
-
// prevent dupes
37
37
-
queue.add(hash)
38
38
-
await ask()
39
39
-
}
40
40
-
}
41
41
-
42
42
-
export const blast = async (pubkey, blob) => {
43
43
-
const room = rooms.get(pubkey)
44
44
-
if (room) {
45
45
-
room.sendBlob(blob)
12
12
+
} else {
13
13
+
setTimeout(() => {sendTry(m)}, 1000)
46
14
}
47
15
}
48
16
49
17
export const makeRoom = async (pubkey) => {
50
50
-
const get = rooms.get(pubkey)
51
51
-
52
52
-
if (!get) {
18
18
+
if (!chan) {
53
19
const room = joinRoom({appId: 'wiredovetestnet', password: 'iajwoiejfaowiejfoiwajfe'}, pubkey)
54
20
const [ sendHash, onHash ] = room.makeAction('hash')
55
21
const [ sendBlob, onBlob ] = room.makeAction('blob')
···
58
24
room.sendBlob = sendBlob
59
25
60
26
onHash(async (hash, id) => {
61
61
-
//console.log(`Received: ${hash}`)
27
27
+
console.log(`Received: ${hash}`)
62
28
const get = await bogbot.get(hash)
63
29
if (get) { sendBlob(get, id)}
64
30
const latest = await bogbot.getLatest(hash)
65
65
-
if (latest) { room.sendBlob(latest.sig)}
31
31
+
if (latest) { sendBlob(latest.sig)}
66
32
})
67
33
68
34
onBlob(async (blob, id) => {
69
69
-
//console.log(`Received: ${blob}`)
35
35
+
console.log(`Received: ${blob}`)
70
36
await render.shouldWe(blob)
37
37
+
await bogbot.make(blob)
71
38
await bogbot.add(blob)
72
39
await render.blob(blob)
73
40
})
74
41
75
42
room.onPeerJoin(async (id) => {
76
43
console.log(id + ' joined the room ' + pubkey)
77
77
-
room.sendHash(pubkey)
78
78
-
const pubkeys = await bogbot.getPubkeys()
79
79
-
for (const key of pubkeys) {
80
80
-
room.sendHash(key)
81
81
-
const latest = await bogbot.getLatest(key)
82
82
-
if (latest) {
83
83
-
room.sendBlob(latest.sig)
84
84
-
}
85
85
-
}
86
44
})
87
45
88
46
room.onPeerLeave(id => {
89
89
-
//console.log(id + ' left the room ' + pubkey)
47
47
+
console.log(id + ' left the room ' + pubkey)
90
48
})
91
49
92
92
-
rooms.set(pubkey, room)
50
50
+
chan = room
93
51
}
94
52
}
+4
-4
render.js
···
1
1
import { bogbot } from 'bogbot'
2
2
import { h } from 'h'
3
3
-
import { gossip } from './gossip.js'
3
3
+
import { send } from './send.js'
4
4
import { composer } from './composer.js'
5
5
import { markdown } from './markdown.js'
6
6
···
118
118
await render.content(contentHash, getContent, content)
119
119
await render.comments(hash, blob, meta)
120
120
} else {
121
121
-
await gossip(contentHash)
121
121
+
await send(contentHash)
122
122
}
123
123
}
124
124
···
175
175
const image = await bogbot.get(yaml.image)
176
176
if (image) {
177
177
get.src = image
178
178
-
} else { gossip(yaml.image)}
178
178
+
} else { send(yaml.image)}
179
179
}
180
180
}
181
181
···
187
187
if (yaml.previous) {
188
188
const check = await bogbot.query(yaml.previous)
189
189
if (!check[0]) {
190
190
-
await gossip(yaml.previous)
190
190
+
await send(yaml.previous)
191
191
}
192
192
}
193
193
+6
-3
route.js
···
3
3
import { h } from 'h'
4
4
import { composer } from './composer.js'
5
5
import { profile } from './profile.js'
6
6
-
import { makeRoom, gossip } from './gossip.js'
6
6
+
import { makeRoom } from './gossip.js'
7
7
import { settings, importKey } from './settings.js'
8
8
import { adder } from './adder.js'
9
9
import { importBlob } from './import.js'
10
10
+
import { send } from './send.js'
10
11
11
12
export const route = async () => {
12
13
const src = window.location.hash.substring(1)
···
68
69
console.log(ar)
69
70
let query = []
70
71
for (const pubkey of ar) {
71
71
-
await makeRoom(pubkey)
72
72
const q = await bogbot.query(pubkey)
73
73
if (q) {
74
74
query.push(...q)
···
82
82
else if (src.length === 44) {
83
83
try {
84
84
const log = await bogbot.query(src)
85
85
-
if (log) {
85
85
+
if (log && log[0]) {
86
86
adder(log, src, scroller)
87
87
+
} else {
88
88
+
console.log('we do not have it')
89
89
+
await send(src)
87
90
}
88
91
} catch (err) { console.log(err)}
89
92
}
+8
send.js
···
1
1
+
import { sendWs } from './websocket.js'
2
2
+
import { sendTry } from './gossip.js'
3
3
+
4
4
+
export const send = async (m) => {
5
5
+
console.log('SENDING' + m)
6
6
+
await sendWs(m)
7
7
+
await sendTry(m)
8
8
+
}
+66
websocket.js
···
1
1
+
import {bogbot} from 'bogbot'
2
2
+
import { render} from './render.js'
3
3
+
4
4
+
const pubs = new Set()
5
5
+
6
6
+
export const sendWs = async (msg) => {
7
7
+
pubs.forEach(pub => {
8
8
+
pub.send(msg)
9
9
+
})
10
10
+
}
11
11
+
12
12
+
export const makeWs = async (pub) => {
13
13
+
const ws = new WebSocket(pub)
14
14
+
15
15
+
ws.onopen = async () => {
16
16
+
console.log('OPEN')
17
17
+
pubs.add(ws)
18
18
+
const p = await bogbot.getPubkeys()
19
19
+
for (const pub of p) {
20
20
+
ws.send(pub)
21
21
+
const latest = await bogbot.getLatest(pub)
22
22
+
if (latest.text) {
23
23
+
ws.send(latest.text)
24
24
+
} else {
25
25
+
const blob = await bogbot.get(latest.opened.substring(13))
26
26
+
if (blob) {ws.send(blob)}
27
27
+
}
28
28
+
ws.send(latest.sig)
29
29
+
}
30
30
+
//below sends everything in the client to a dovepub pds server
31
31
+
const log = await bogbot.query()
32
32
+
if (log) {
33
33
+
for (const msg of log) {
34
34
+
ws.send(msg.sig)
35
35
+
if (msg.text) {
36
36
+
ws.send(msg.text)
37
37
+
}
38
38
+
if (!msg.text) {
39
39
+
const get = await bogbot.get(msg.opened.substring(13))
40
40
+
if (get) {ws.send(get)}
41
41
+
}
42
42
+
}
43
43
+
}
44
44
+
}
45
45
+
46
46
+
ws.onmessage = async (m) => {
47
47
+
if (m.data.length === 44) {
48
48
+
//console.log('NEEDS' + m.data)
49
49
+
const blob = await bogbot.get(m.data)
50
50
+
if (blob) {
51
51
+
ws.send(blob)
52
52
+
}
53
53
+
} else {
54
54
+
await render.shouldWe(m.data)
55
55
+
await bogbot.make(m.data)
56
56
+
await bogbot.add(m.data)
57
57
+
await render.blob(m.data)
58
58
+
}
59
59
+
}
60
60
+
61
61
+
ws.onclose = async () => {
62
62
+
console.log('CLOSED')
63
63
+
pubs.delete(ws)
64
64
+
}
65
65
+
}
66
66
+