OverviewFilesCommitsIssuesDocsReleasesPeers

apds commits / 50ed06e

implement a query algorithm
50ed06e948c3ec24eeb15d384a2b644c6859ef2f
by Ev Bogue <ev@evbogue.com> · 2025-01-06T01:38:48.000Z

Changed files (3) +42 -21

bogbot.js +34 -14
……@@ -5,7 +5,8 @@ import { human } from './lib/human.js'
55import { vb } from './lib/vb.js'
66
77let db
8let log = []
8let hashLog = []
9let openedLog = []
910let newMessages = false
1011let sort = true
1112
……@@ -16,40 +17,46 @@ bogbot.start = async (appId) => {
1617
1718 setInterval(async () => {
1819 if (newMessages) {
19 await db.put('log', JSON.stringify(log))
20 await db.put('hashlog', JSON.stringify(hashLog))
21 await db.put('openedlog', JSON.stringify(openedLog))
2022 newMessages = false
2123 }
2224 }, 1000)
2325
24 const getLog = await db.get('log')
25 if (getLog) {
26 log = JSON.parse(getLog)
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)
2733 }
28
2934
3035 setInterval(async () => {
3136 if (sort) {
3237 const newArray = []
3338
34 await Promise.all(log.map(async (hash) => {
39 await Promise.all(hashLog.map(async (hash) => {
3540 const obj = {
3641 hash,
3742 sig: await bogbot.get(hash)
3843 }
44 obj.author = obj.sig.substring(0, 44)
3945 obj.opened = await bogbot.open(obj.sig)
46 obj.text = await bogbot.get(obj.opened.substring(13))
4047 obj.ts = obj.opened.substring(0, 13)
4148 newArray.push(obj)
4249 }))
43
44 await newArray.sort((a,b) => a.ts - b.ts)
45
4650 const newLog = []
51
52 await newArray.sort((a,b) => a.ts - b.ts)
4753
4854 await newArray.forEach(msg => {
4955 newLog.push(msg.hash)
5056 })
5157
52 log = newLog
58 hashLog = newLog
59 openedLog = newArray
5360 newMessages = true
5461 sort = false
5562 }
……@@ -173,8 +180,9 @@ bogbot.add = async (msg) => {
173180 const opened = await bogbot.open(msg)
174181 if (opened) {
175182 const hash = await bogbot.make(msg)
176 if (!log.includes(hash)) {
177 log.push(hash)
183 if (!hashLog.includes(hash)) {
184 hashLog.push(hash)
185 openedLog.push(opened)
178186 newMessages = true
179187 sort = true
180188 }
……@@ -182,7 +190,19 @@ bogbot.add = async (msg) => {
182190}
183191
184192bogbot.getLog = async () => {
185 return log
193 return hashLog
194}
195
196bogbot.query = async (query) => {
197 if (openedLog[0] && !query) { return openedLog }
198 if (openedLog[0] && query.startsWith('?')) {
199 const search = query.substring(1).replace(/%20/g, ' ').toUpperCase()
200 const result = openedLog.filter(msg => msg.text && msg.text.toUpperCase().includes(search))
201 return result
202 } else if (arraystore[0]) {
203 const result = openedLog.filter(msg => msg.author == query || msg.hash == query)
204 return result
205 }
186206}
187207
188208bogbot.human = async (ts) => {
example.js +7 -3
……@@ -18,8 +18,12 @@ scroller.id = 'scroller'
1818
1919document.body.appendChild(scroller)
2020
21const log = await bogbot.getLog()
21const log = await bogbot.query()
2222
23log.forEach(async (hash) => {
24 await render.hash(hash, scroller)
23log.forEach(async (obj) => {
24 await render.hash(obj.hash, scroller)
2525})
26
27const search = await bogbot.query('?hello world')
28
29console.log(search)
render.js +1 -4
……@@ -57,8 +57,5 @@ render.hash = async (hash, scroller) => {
5757
5858 if (sig) {
5959 await render.blob(sig)
60 } else {
61 await gossip(hash)
62 }
63
60 }
6461}