a demonstration replicated social networking web app built with anproto wiredove.net/
social ed25519 protocols
at main 84 lines 2.0 kB view raw
1import {apds} from 'apds' 2import { render} from './render.js' 3 4const pubs = new Set() 5 6export const sendWs = async (msg) => { 7 if (pubs.size) { 8 pubs.forEach(pub => { 9 pub.send(msg) 10 }) 11 } else { 12 setTimeout(async () => { 13 pubs.forEach(pub => { 14 pub.send(msg) 15 }) 16 }, 1000) 17 } 18} 19 20export const makeWs = async (pub) => { 21 const ws = new WebSocket(pub) 22 23 ws.onopen = async () => { 24 console.log('OPEN') 25 pubs.add(ws) 26 const p = await apds.getPubkeys() 27 for (const pub of p) { 28 ws.send(pub) 29 const latest = await apds.getLatest(pub) 30 if (latest.text) { 31 ws.send(latest.text) 32 } else { 33 const blob = await apds.get(latest.opened.substring(13)) 34 if (blob) {ws.send(blob)} 35 } 36 ws.send(latest.sig) 37 } 38 //below sends everything in the client to a dovepub pds server 39 //const log = await apds.query() 40 //if (log) { 41 // const ar = [] 42 // for (const msg of log) { 43 // ws.send(msg.sig) 44 // if (msg.text) { 45 // ws.send(msg.text) 46 // const yaml = await apds.parseYaml(msg.text) 47 // //console.log(yaml) 48 // if (yaml.image && !ar.includes(yaml.image)) { 49 // const get = await apds.get(yaml.image) 50 // if (get) { 51 // ws.send(get) 52 // ar.push(yaml.image) 53 // } 54 // } 55 // } 56 // if (!msg.text) { 57 // const get = await apds.get(msg.opened.substring(13)) 58 // if (get) {ws.send(get)} 59 // } 60 // } 61 //} 62 } 63 64 ws.onmessage = async (m) => { 65 if (m.data.length === 44) { 66 //console.log('NEEDS' + m.data) 67 const blob = await apds.get(m.data) 68 if (blob) { 69 ws.send(blob) 70 } 71 } else { 72 await render.shouldWe(m.data) 73 await apds.make(m.data) 74 await apds.add(m.data) 75 await render.blob(m.data) 76 } 77 } 78 79 ws.onclose = async () => { 80 console.log('CLOSED') 81 pubs.delete(ws) 82 } 83} 84