| … | … | @@ -0,0 +1,235 @@ |
| 1 | import { decode } from './lib/base64.js' | |
| 2 | import { cachekv } from './lib/cachekv.js' | |
| 3 | import { human } from './lib/human.js' | |
| 4 | import { vb } from './lib/vb.js' | |
| 5 | import { an } from 'https://anproto.com/an.js' | |
| 6 | ||
| 7 | let db | |
| 8 | let hashLog = [] | |
| 9 | let openedLog = [] | |
| 10 | let newMessages = false | |
| 11 | let sort = true | |
| 12 | ||
| 13 | export const apds = {} | |
| 14 | ||
| 15 | apds.start = async (appId) => { | |
| 16 | db = await cachekv(appId) | |
| 17 | ||
| 18 | setInterval(async () => { | |
| 19 | if (newMessages) { | |
| 20 | await db.put('hashlog', JSON.stringify(hashLog)) | |
| 21 | await db.put('openedlog', JSON.stringify(openedLog)) | |
| 22 | newMessages = false | |
| 23 | } | |
| 24 | }, 1000) | |
| 25 | ||
| 26 | const getHashLog = await db.get('hashlog') | |
| 27 | const getOpenedLog = await db.get('openedlog') | |
| 28 | if (getHashLog) { | |
| 29 | hashLog = JSON.parse(getHashLog) | |
| 30 | } | |
| 31 | if (getOpenedLog) { | |
| 32 | openedLog = JSON.parse(getOpenedLog) | |
| 33 | } | |
| 34 | ||
| 35 | setInterval(async () => { | |
| 36 | if (sort) { | |
| 37 | const newArray = [] | |
| 38 | ||
| 39 | await Promise.all(hashLog.map(async (hash) => { | |
| 40 | try { | |
| 41 | const obj = { | |
| 42 | hash, | |
| 43 | sig: await apds.get(hash) | |
| 44 | } | |
| 45 | obj.author = obj.sig.substring(0, 44) | |
| 46 | obj.opened = await apds.open(obj.sig) | |
| 47 | obj.text = await apds.get(obj.opened.substring(13)) | |
| 48 | obj.ts = obj.opened.substring(0, 13) | |
| 49 | newArray.push(obj) | |
| 50 | } catch (err) { console.log(err) } | |
| 51 | })) | |
| 52 | const newLog = [] | |
| 53 | ||
| 54 | await newArray.sort((a,b) => a.ts - b.ts) | |
| 55 | ||
| 56 | await newArray.forEach(msg => { | |
| 57 | newLog.push(msg.hash) | |
| 58 | }) | |
| 59 | ||
| 60 | hashLog = newLog | |
| 61 | openedLog = newArray | |
| 62 | newMessages = true | |
| 63 | sort = false | |
| 64 | } | |
| 65 | }, 20000) | |
| 66 | } | |
| 67 | ||
| 68 | apds.generate = async () => { | |
| 69 | const genkey = await an.gen() | |
| 70 | return genkey | |
| 71 | } | |
| 72 | ||
| 73 | apds.keypair = async () => { | |
| 74 | const keypair = await db.get('keypair') | |
| 75 | if (keypair) { | |
| 76 | return keypair | |
| 77 | } | |
| 78 | } | |
| 79 | ||
| 80 | apds.pubkey = async () => { | |
| 81 | const keypair = await apds.keypair() | |
| 82 | console.log(keypair) | |
| 83 | if (keypair) { | |
| 84 | return keypair.substring(0, 44) | |
| 85 | } | |
| 86 | } | |
| 87 | ||
| 88 | apds.privkey = async () => { | |
| 89 | const keypair = await apds.keypair() | |
| 90 | if (keypair) { | |
| 91 | return keypair.substring(44) | |
| 92 | } | |
| 93 | } | |
| 94 | ||
| 95 | apds.deletekey = async () => { | |
| 96 | db.rm('keypair') | |
| 97 | } | |
| 98 | ||
| 99 | apds.clear = async () => { | |
| 100 | db.clear() | |
| 101 | } | |
| 102 | ||
| 103 | apds.hash = async (data) => { return await an.hash(data) } | |
| 104 | ||
| 105 | apds.sign = async (data) => { | |
| 106 | const hash = await apds.make(data) | |
| 107 | const sig = await an.sign(hash, await apds.keypair()) | |
| 108 | console.log(sig) | |
| 109 | await apds.add(sig) | |
| 110 | const protocolMsg = await apds.make(sig) | |
| 111 | ||
| 112 | db.put('previous', protocolMsg) | |
| 113 | return protocolMsg | |
| 114 | } | |
| 115 | ||
| 116 | apds.open = async (msg) => { | |
| 117 | try { | |
| 118 | return await an.open(msg) | |
| 119 | } catch (err) { | |
| 120 | //console.log('Not a valid Bog5 protocol message') | |
| 121 | } | |
| 122 | } | |
| 123 | ||
| 124 | import { yaml } from './lib/yaml.js' | |
| 125 | ||
| 126 | apds.parseYaml = async (doc) => { | |
| 127 | return await yaml.parse(doc) | |
| 128 | } | |
| 129 | ||
| 130 | apds.createYaml = async (obj, content) => { | |
| 131 | return await yaml.create(obj, content) | |
| 132 | } | |
| 133 | ||
| 134 | apds.compose = async (content, prev) => { | |
| 135 | let obj = {} | |
| 136 | if (prev) { obj = prev } | |
| 137 | ||
| 138 | const name = await db.get('name') | |
| 139 | const image = await db.get('image') | |
| 140 | const previous = await db.get('previous') | |
| 141 | ||
| 142 | if (name) { obj.name = name} | |
| 143 | if (image) { obj.image = image} | |
| 144 | if (previous) { obj.previous = previous} | |
| 145 | ||
| 146 | if (Object.keys(obj).length > 0) { | |
| 147 | const yaml = await apds.createYaml(obj, content) | |
| 148 | return await apds.sign(yaml) | |
| 149 | } else { | |
| 150 | return await apds.sign(content) | |
| 151 | } | |
| 152 | } | |
| 153 | ||
| 154 | apds.make = async (data) => { | |
| 155 | const hash = await apds.hash(data) | |
| 156 | ||
| 157 | await db.put(hash, data) | |
| 158 | ||
| 159 | return hash | |
| 160 | } | |
| 161 | ||
| 162 | apds.get = async (hash) => { | |
| 163 | const blob = await db.get(hash) | |
| 164 | ||
| 165 | return blob | |
| 166 | } | |
| 167 | ||
| 168 | apds.put = async (key, value) => { | |
| 169 | await db.put(key, value) | |
| 170 | } | |
| 171 | ||
| 172 | apds.rm = async (key) => { | |
| 173 | await db.rm(key) | |
| 174 | } | |
| 175 | ||
| 176 | apds.add = async (msg) => { | |
| 177 | const opened = await apds.open(msg) | |
| 178 | if (opened) { | |
| 179 | const hash = await apds.make(msg) | |
| 180 | if (!hashLog.includes(hash)) { | |
| 181 | hashLog.push(hash) | |
| 182 | const obj = { | |
| 183 | hash, | |
| 184 | sig: msg | |
| 185 | } | |
| 186 | obj.author = obj.sig.substring(0, 44) | |
| 187 | obj.opened = opened | |
| 188 | obj.text = await apds.get(obj.opened.substring(13)) | |
| 189 | obj.ts = obj.opened.substring(0, 13) | |
| 190 | openedLog.push(obj) | |
| 191 | newMessages = true | |
| 192 | sort = true | |
| 193 | } | |
| 194 | } | |
| 195 | } | |
| 196 | ||
| 197 | apds.getHashLog = async () => { return hashLog } | |
| 198 | ||
| 199 | apds.getOpenedLog = async () => { return openedLog } | |
| 200 | ||
| 201 | apds.query = async (query) => { | |
| 202 | if (openedLog[0] && !query) { return openedLog } | |
| 203 | if (openedLog[0] && query.startsWith('?')) { | |
| 204 | const search = query.substring(1).replace(/%20/g, ' ').toUpperCase() | |
| 205 | const result = openedLog.filter(msg => msg.text && msg.text.toUpperCase().includes(search)) | |
| 206 | return result | |
| 207 | } else if (openedLog[0]) { | |
| 208 | const result = openedLog.filter(msg => msg.author == query || msg.hash == query) | |
| 209 | return result | |
| 210 | } | |
| 211 | } | |
| 212 | ||
| 213 | apds.getPubkeys = async () => { | |
| 214 | const arr = await apds.query() | |
| 215 | const newSet = new Set() | |
| 216 | for (const msg of arr) { | |
| 217 | newSet.add(msg.author) | |
| 218 | } | |
| 219 | const newArr = [...newSet] | |
| 220 | return newArr | |
| 221 | } | |
| 222 | ||
| 223 | apds.getLatest = async (pubkey) => { | |
| 224 | const q = openedLog.filter(msg => msg.author === pubkey) | |
| 225 | return q[q.length -1] | |
| 226 | } | |
| 227 | ||
| 228 | apds.human = async (ts) => { | |
| 229 | return await human(new Date(parseInt(ts))) | |
| 230 | } | |
| 231 | ||
| 232 | apds.visual = async (pubkey) => { | |
| 233 | return vb(decode(pubkey), 256) | |
| 234 | } | |
| 235 |
| … | … | @@ -1,235 +0,0 @@ |
| 1 | import { decode } from './lib/base64.js' | |
| 2 | import { cachekv } from './lib/cachekv.js' | |
| 3 | import { human } from './lib/human.js' | |
| 4 | import { vb } from './lib/vb.js' | |
| 5 | import { an } from 'https://esm.sh/gh/evbogue/anproto@f7cd761/an.js' | |
| 6 | ||
| 7 | let db | |
| 8 | let hashLog = [] | |
| 9 | let openedLog = [] | |
| 10 | let newMessages = false | |
| 11 | let sort = true | |
| 12 | ||
| 13 | export const apds = {} | |
| 14 | ||
| 15 | apds.start = async (appId) => { | |
| 16 | db = await cachekv(appId) | |
| 17 | ||
| 18 | setInterval(async () => { | |
| 19 | if (newMessages) { | |
| 20 | await db.put('hashlog', JSON.stringify(hashLog)) | |
| 21 | await db.put('openedlog', JSON.stringify(openedLog)) | |
| 22 | newMessages = false | |
| 23 | } | |
| 24 | }, 1000) | |
| 25 | ||
| 26 | const getHashLog = await db.get('hashlog') | |
| 27 | const getOpenedLog = await db.get('openedlog') | |
| 28 | if (getHashLog) { | |
| 29 | hashLog = JSON.parse(getHashLog) | |
| 30 | } | |
| 31 | if (getOpenedLog) { | |
| 32 | openedLog = JSON.parse(getOpenedLog) | |
| 33 | } | |
| 34 | ||
| 35 | setInterval(async () => { | |
| 36 | if (sort) { | |
| 37 | const newArray = [] | |
| 38 | ||
| 39 | await Promise.all(hashLog.map(async (hash) => { | |
| 40 | try { | |
| 41 | const obj = { | |
| 42 | hash, | |
| 43 | sig: await apds.get(hash) | |
| 44 | } | |
| 45 | obj.author = obj.sig.substring(0, 44) | |
| 46 | obj.opened = await apds.open(obj.sig) | |
| 47 | obj.text = await apds.get(obj.opened.substring(13)) | |
| 48 | obj.ts = obj.opened.substring(0, 13) | |
| 49 | newArray.push(obj) | |
| 50 | } catch (err) { console.log(err) } | |
| 51 | })) | |
| 52 | const newLog = [] | |
| 53 | ||
| 54 | await newArray.sort((a,b) => a.ts - b.ts) | |
| 55 | ||
| 56 | await newArray.forEach(msg => { | |
| 57 | newLog.push(msg.hash) | |
| 58 | }) | |
| 59 | ||
| 60 | hashLog = newLog | |
| 61 | openedLog = newArray | |
| 62 | newMessages = true | |
| 63 | sort = false | |
| 64 | } | |
| 65 | }, 20000) | |
| 66 | } | |
| 67 | ||
| 68 | apds.generate = async () => { | |
| 69 | const genkey = await an.gen() | |
| 70 | return genkey | |
| 71 | } | |
| 72 | ||
| 73 | apds.keypair = async () => { | |
| 74 | const keypair = await db.get('keypair') | |
| 75 | if (keypair) { | |
| 76 | return keypair | |
| 77 | } | |
| 78 | } | |
| 79 | ||
| 80 | apds.pubkey = async () => { | |
| 81 | const keypair = await apds.keypair() | |
| 82 | console.log(keypair) | |
| 83 | if (keypair) { | |
| 84 | return keypair.substring(0, 44) | |
| 85 | } | |
| 86 | } | |
| 87 | ||
| 88 | apds.privkey = async () => { | |
| 89 | const keypair = await apds.keypair() | |
| 90 | if (keypair) { | |
| 91 | return keypair.substring(44) | |
| 92 | } | |
| 93 | } | |
| 94 | ||
| 95 | apds.deletekey = async () => { | |
| 96 | db.rm('keypair') | |
| 97 | } | |
| 98 | ||
| 99 | apds.clear = async () => { | |
| 100 | db.clear() | |
| 101 | } | |
| 102 | ||
| 103 | apds.hash = async (data) => { return await an.hash(data) } | |
| 104 | ||
| 105 | apds.sign = async (data) => { | |
| 106 | const hash = await apds.make(data) | |
| 107 | const sig = await an.sign(hash, await apds.keypair()) | |
| 108 | console.log(sig) | |
| 109 | await apds.add(sig) | |
| 110 | const protocolMsg = await apds.make(sig) | |
| 111 | ||
| 112 | db.put('previous', protocolMsg) | |
| 113 | return protocolMsg | |
| 114 | } | |
| 115 | ||
| 116 | apds.open = async (msg) => { | |
| 117 | try { | |
| 118 | return await an.open(msg) | |
| 119 | } catch (err) { | |
| 120 | //console.log('Not a valid Bog5 protocol message') | |
| 121 | } | |
| 122 | } | |
| 123 | ||
| 124 | import { yaml } from './lib/yaml.js' | |
| 125 | ||
| 126 | apds.parseYaml = async (doc) => { | |
| 127 | return await yaml.parse(doc) | |
| 128 | } | |
| 129 | ||
| 130 | apds.createYaml = async (obj, content) => { | |
| 131 | return await yaml.create(obj, content) | |
| 132 | } | |
| 133 | ||
| 134 | apds.compose = async (content, prev) => { | |
| 135 | let obj = {} | |
| 136 | if (prev) { obj = prev } | |
| 137 | ||
| 138 | const name = await db.get('name') | |
| 139 | const image = await db.get('image') | |
| 140 | const previous = await db.get('previous') | |
| 141 | ||
| 142 | if (name) { obj.name = name} | |
| 143 | if (image) { obj.image = image} | |
| 144 | if (previous) { obj.previous = previous} | |
| 145 | ||
| 146 | if (Object.keys(obj).length > 0) { | |
| 147 | const yaml = await apds.createYaml(obj, content) | |
| 148 | return await apds.sign(yaml) | |
| 149 | } else { | |
| 150 | return await apds.sign(content) | |
| 151 | } | |
| 152 | } | |
| 153 | ||
| 154 | apds.make = async (data) => { | |
| 155 | const hash = await apds.hash(data) | |
| 156 | ||
| 157 | await db.put(hash, data) | |
| 158 | ||
| 159 | return hash | |
| 160 | } | |
| 161 | ||
| 162 | apds.get = async (hash) => { | |
| 163 | const blob = await db.get(hash) | |
| 164 | ||
| 165 | return blob | |
| 166 | } | |
| 167 | ||
| 168 | apds.put = async (key, value) => { | |
| 169 | await db.put(key, value) | |
| 170 | } | |
| 171 | ||
| 172 | apds.rm = async (key) => { | |
| 173 | await db.rm(key) | |
| 174 | } | |
| 175 | ||
| 176 | apds.add = async (msg) => { | |
| 177 | const opened = await apds.open(msg) | |
| 178 | if (opened) { | |
| 179 | const hash = await apds.make(msg) | |
| 180 | if (!hashLog.includes(hash)) { | |
| 181 | hashLog.push(hash) | |
| 182 | const obj = { | |
| 183 | hash, | |
| 184 | sig: msg | |
| 185 | } | |
| 186 | obj.author = obj.sig.substring(0, 44) | |
| 187 | obj.opened = opened | |
| 188 | obj.text = await apds.get(obj.opened.substring(13)) | |
| 189 | obj.ts = obj.opened.substring(0, 13) | |
| 190 | openedLog.push(obj) | |
| 191 | newMessages = true | |
| 192 | sort = true | |
| 193 | } | |
| 194 | } | |
| 195 | } | |
| 196 | ||
| 197 | apds.getHashLog = async () => { return hashLog } | |
| 198 | ||
| 199 | apds.getOpenedLog = async () => { return openedLog } | |
| 200 | ||
| 201 | apds.query = async (query) => { | |
| 202 | if (openedLog[0] && !query) { return openedLog } | |
| 203 | if (openedLog[0] && query.startsWith('?')) { | |
| 204 | const search = query.substring(1).replace(/%20/g, ' ').toUpperCase() | |
| 205 | const result = openedLog.filter(msg => msg.text && msg.text.toUpperCase().includes(search)) | |
| 206 | return result | |
| 207 | } else if (openedLog[0]) { | |
| 208 | const result = openedLog.filter(msg => msg.author == query || msg.hash == query) | |
| 209 | return result | |
| 210 | } | |
| 211 | } | |
| 212 | ||
| 213 | apds.getPubkeys = async () => { | |
| 214 | const arr = await apds.query() | |
| 215 | const newSet = new Set() | |
| 216 | for (const msg of arr) { | |
| 217 | newSet.add(msg.author) | |
| 218 | } | |
| 219 | const newArr = [...newSet] | |
| 220 | return newArr | |
| 221 | } | |
| 222 | ||
| 223 | apds.getLatest = async (pubkey) => { | |
| 224 | const q = openedLog.filter(msg => msg.author === pubkey) | |
| 225 | return q[q.length -1] | |
| 226 | } | |
| 227 | ||
| 228 | apds.human = async (ts) => { | |
| 229 | return await human(new Date(parseInt(ts))) | |
| 230 | } | |
| 231 | ||
| 232 | apds.visual = async (pubkey) => { | |
| 233 | return vb(decode(pubkey), 256) | |
| 234 | } | |
| 235 |