| … | … | @@ -5,7 +5,8 @@ import { human } from './lib/human.js' |
| 5 | 5 | import { vb } from './lib/vb.js' |
| 6 | 6 | |
| 7 | 7 | let db |
| 8 | | let log = [] |
| 8 | let hashLog = [] |
| 9 | let openedLog = [] |
| 9 | 10 | let newMessages = false |
| 10 | 11 | let sort = true |
| 11 | 12 | |
| … | … | @@ -16,40 +17,46 @@ bogbot.start = async (appId) => { |
| 16 | 17 | |
| 17 | 18 | setInterval(async () => { |
| 18 | 19 | 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)) |
| 20 | 22 | newMessages = false |
| 21 | 23 | } |
| 22 | 24 | }, 1000) |
| 23 | 25 | |
| 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) |
| 27 | 33 | } |
| 28 | | |
| 29 | 34 | |
| 30 | 35 | setInterval(async () => { |
| 31 | 36 | if (sort) { |
| 32 | 37 | const newArray = [] |
| 33 | 38 | |
| 34 | | await Promise.all(log.map(async (hash) => { |
| 39 | await Promise.all(hashLog.map(async (hash) => { |
| 35 | 40 | const obj = { |
| 36 | 41 | hash, |
| 37 | 42 | sig: await bogbot.get(hash) |
| 38 | 43 | } |
| 44 | obj.author = obj.sig.substring(0, 44) |
| 39 | 45 | obj.opened = await bogbot.open(obj.sig) |
| 46 | obj.text = await bogbot.get(obj.opened.substring(13)) |
| 40 | 47 | obj.ts = obj.opened.substring(0, 13) |
| 41 | 48 | newArray.push(obj) |
| 42 | 49 | })) |
| 43 | | |
| 44 | | await newArray.sort((a,b) => a.ts - b.ts) |
| 45 | | |
| 46 | 50 | const newLog = [] |
| 51 | |
| 52 | await newArray.sort((a,b) => a.ts - b.ts) |
| 47 | 53 | |
| 48 | 54 | await newArray.forEach(msg => { |
| 49 | 55 | newLog.push(msg.hash) |
| 50 | 56 | }) |
| 51 | 57 | |
| 52 | | log = newLog |
| 58 | hashLog = newLog |
| 59 | openedLog = newArray |
| 53 | 60 | newMessages = true |
| 54 | 61 | sort = false |
| 55 | 62 | } |
| … | … | @@ -173,8 +180,9 @@ bogbot.add = async (msg) => { |
| 173 | 180 | const opened = await bogbot.open(msg) |
| 174 | 181 | if (opened) { |
| 175 | 182 | 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) |
| 178 | 186 | newMessages = true |
| 179 | 187 | sort = true |
| 180 | 188 | } |
| … | … | @@ -182,7 +190,19 @@ bogbot.add = async (msg) => { |
| 182 | 190 | } |
| 183 | 191 | |
| 184 | 192 | bogbot.getLog = async () => { |
| 185 | | return log |
| 193 | return hashLog |
| 194 | } |
| 195 | |
| 196 | bogbot.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 | } |
| 186 | 206 | } |
| 187 | 207 | |
| 188 | 208 | bogbot.human = async (ts) => { |