a demonstration replicated social networking web app built with anproto wiredove.net/
social ed25519 protocols

refactor gossip strategy into similar methods

+102 -140
-4
app.js
··· 10 10 document.body.appendChild(await navbar()) 11 11 12 12 await route() 13 - await bogbot.query() 14 13 await connect() 15 14 16 - //await bogbot.keypair().then(async (pubkey) => { 17 - // if (pubkey) { await connect()} 18 - //})
+1 -5
composer.js
··· 1 1 import { bogbot } from 'bogbot' 2 2 import { render } from './render.js' 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 - import { send } from './connect.js' 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 - console.log(blob) 60 - await blast(pubkey, signed) 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 - import { render } from './render.js' 3 + import { makeWs} from './websocket.js' 4 4 5 5 await bogbot.start('wiredovedbversion1') 6 6 7 - const pubs = new Set() 8 - 9 - export const send = async (msg) => { 10 - pubs.forEach(pub => { 11 - pub.send(msg) 12 - }) 13 - } 14 - 15 - const startWs = async (pub) => { 16 - const ws = new WebSocket(pub) 17 - 18 - ws.onopen = async () => { 19 - console.log('OPEN') 20 - pubs.add(ws) 21 - const p = await bogbot.getPubkeys() 22 - for (const pub of p) { 23 - ws.send(pub) 24 - const latest = await bogbot.getLatest(pub) 25 - if (latest.text) { 26 - ws.send(latest.text) 27 - } else { 28 - const blob = await bogbot.get(latest.opened.substring(13)) 29 - if (blob) {ws.send(blob)} 30 - } 31 - ws.send(latest.sig) 32 - } 33 - //below sends everything in the client to a dovepub pds server 34 - const log = await bogbot.query() 35 - if (log) { 36 - for (const msg of log) { 37 - ws.send(msg.sig) 38 - ws.send(msg.text) 39 - if (!msg.text) { ws.send(await bogbot.get(msg.opened.substring(13)))} 40 - } 41 - } 42 - } 43 - 44 - ws.onmessage = async (m) => { 45 - if (m.data.length === 44) { 46 - //console.log('NEEDS' + m.data) 47 - const blob = await bogbot.get(m.data) 48 - if (blob) { 49 - ws.send(blob) 50 - } 51 - } else { 52 - await render.shouldWe(m.data) 53 - await bogbot.add(m.data) 54 - await render.blob(m.data) 55 - } 56 - } 57 - 58 - ws.onclose = async () => { 59 - console.log('CLOSED') 60 - pubs.delete(ws) 61 - } 62 - } 63 - 64 7 export const connect = async () => { 65 - await startWs('wss://pub.wiredove.net/') 8 + await makeWs('wss://pub.wiredove.net/') 66 9 await makeRoom('wiredovev1') 67 - //await startWs('ws://localhost:9000') 68 - //const pubkeys = await bogbot.getPubkeys() 69 - //if (pubkeys) { 70 - // for (const p of pubkeys) { 71 - // console.log(p) 72 - // await makeRoom(p) 73 - // } 74 - //} 75 10 }
+15 -57
gossip.js
··· 1 1 import { bogbot } from 'bogbot' 2 2 import { joinRoom } from './trystero-torrent.min.js' 3 - import { render } from './render.js' 4 - import { send } from './connect.js' 5 - 6 - export const rooms = new Map() 3 + import { render } from './render.js' 7 4 8 - const queue = new Set() 9 - 10 - export const gossip = async (hash) => { 11 - let speed = 1 5 + export let chan 12 6 13 - const ask = async () => { 14 - const haveBlob = await bogbot.get(hash) 15 - if (haveBlob) { 16 - await bogbot.add(haveBlob) 17 - queue.delete(hash) 18 - } 19 - if (queue.has(hash)) { 20 - speed++ 21 - const values = [...rooms.values()] 22 - const room = values[Math.floor(Math.random() * values.length)] 23 - if (room && room.sendHash) { 24 - room.sendHash(hash) 25 - setTimeout(() => { 26 - if (speed === 100) { 27 - queue.delete(hash) 28 - } 29 - ask() 30 - }, (10000 * speed)) 31 - } 7 + export const sendTry = (m) => { 8 + if (chan) { 9 + if (m.length === 44) { chan.sendHash(m)} else { 10 + chan.sendBlob(m) 32 11 } 33 - } 34 - if (!queue.has(hash)) { 35 - send(hash) 36 - // prevent dupes 37 - queue.add(hash) 38 - await ask() 39 - } 40 - } 41 - 42 - export const blast = async (pubkey, blob) => { 43 - const room = rooms.get(pubkey) 44 - if (room) { 45 - room.sendBlob(blob) 12 + } else { 13 + setTimeout(() => {sendTry(m)}, 1000) 46 14 } 47 15 } 48 16 49 17 export const makeRoom = async (pubkey) => { 50 - const get = rooms.get(pubkey) 51 - 52 - if (!get) { 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 - //console.log(`Received: ${hash}`) 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 - if (latest) { room.sendBlob(latest.sig)} 31 + if (latest) { sendBlob(latest.sig)} 66 32 }) 67 33 68 34 onBlob(async (blob, id) => { 69 - //console.log(`Received: ${blob}`) 35 + console.log(`Received: ${blob}`) 70 36 await render.shouldWe(blob) 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 - room.sendHash(pubkey) 78 - const pubkeys = await bogbot.getPubkeys() 79 - for (const key of pubkeys) { 80 - room.sendHash(key) 81 - const latest = await bogbot.getLatest(key) 82 - if (latest) { 83 - room.sendBlob(latest.sig) 84 - } 85 - } 86 44 }) 87 45 88 46 room.onPeerLeave(id => { 89 - //console.log(id + ' left the room ' + pubkey) 47 + console.log(id + ' left the room ' + pubkey) 90 48 }) 91 49 92 - rooms.set(pubkey, room) 50 + chan = room 93 51 } 94 52 }
+4 -4
render.js
··· 1 1 import { bogbot } from 'bogbot' 2 2 import { h } from 'h' 3 - import { gossip } from './gossip.js' 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 - await gossip(contentHash) 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 - } else { gossip(yaml.image)} 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 - await gossip(yaml.previous) 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 - import { makeRoom, gossip } from './gossip.js' 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 + 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 - 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 - if (log) { 85 + if (log && log[0]) { 86 86 adder(log, src, scroller) 87 + } else { 88 + console.log('we do not have it') 89 + await send(src) 87 90 } 88 91 } catch (err) { console.log(err)} 89 92 }
+8
send.js
··· 1 + import { sendWs } from './websocket.js' 2 + import { sendTry } from './gossip.js' 3 + 4 + export const send = async (m) => { 5 + console.log('SENDING' + m) 6 + await sendWs(m) 7 + await sendTry(m) 8 + }
+66
websocket.js
··· 1 + import {bogbot} from 'bogbot' 2 + import { render} from './render.js' 3 + 4 + const pubs = new Set() 5 + 6 + export const sendWs = async (msg) => { 7 + pubs.forEach(pub => { 8 + pub.send(msg) 9 + }) 10 + } 11 + 12 + export const makeWs = async (pub) => { 13 + const ws = new WebSocket(pub) 14 + 15 + ws.onopen = async () => { 16 + console.log('OPEN') 17 + pubs.add(ws) 18 + const p = await bogbot.getPubkeys() 19 + for (const pub of p) { 20 + ws.send(pub) 21 + const latest = await bogbot.getLatest(pub) 22 + if (latest.text) { 23 + ws.send(latest.text) 24 + } else { 25 + const blob = await bogbot.get(latest.opened.substring(13)) 26 + if (blob) {ws.send(blob)} 27 + } 28 + ws.send(latest.sig) 29 + } 30 + //below sends everything in the client to a dovepub pds server 31 + const log = await bogbot.query() 32 + if (log) { 33 + for (const msg of log) { 34 + ws.send(msg.sig) 35 + if (msg.text) { 36 + ws.send(msg.text) 37 + } 38 + if (!msg.text) { 39 + const get = await bogbot.get(msg.opened.substring(13)) 40 + if (get) {ws.send(get)} 41 + } 42 + } 43 + } 44 + } 45 + 46 + ws.onmessage = async (m) => { 47 + if (m.data.length === 44) { 48 + //console.log('NEEDS' + m.data) 49 + const blob = await bogbot.get(m.data) 50 + if (blob) { 51 + ws.send(blob) 52 + } 53 + } else { 54 + await render.shouldWe(m.data) 55 + await bogbot.make(m.data) 56 + await bogbot.add(m.data) 57 + await render.blob(m.data) 58 + } 59 + } 60 + 61 + ws.onclose = async () => { 62 + console.log('CLOSED') 63 + pubs.delete(ws) 64 + } 65 + } 66 +