-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.js
135 lines (124 loc) · 3.56 KB
/
log.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { open } from './sbog.js'
import { cachekv } from './cachekv.js'
let arraystore = []
var log = []
export function save () {
const stringedarray = JSON.stringify(arraystore)
const stringedlog = JSON.stringify(log)
cachekv.put('log', stringedlog)
cachekv.put('arraystore', stringedarray)
}
cachekv.get('arraystore').then(file => {
if (file) {
arraystore = JSON.parse(file)
arraystore.sort((a,b) => a.timestamp - b.timestamp)
}
})
cachekv.get('log').then(file => {
if (file) {
log = JSON.parse(file)
const newset = new Set(log)
const newarray = []
newset.forEach(msg => {
open(msg).then(opened => {
if (opened) {
newarray.push(opened)
}
})
})
setTimeout(function () {
newarray.sort((a,b) => a.timestamp - b.timestamp)
arraystore = newarray
save()
}, 5000)
}
})
let newData = true
setInterval(function () {
if (newData) {
arraystore.sort((a,b) => a.timestamp - b.timestamp)
save()
newData = false
}
}, 10000)
export const logs = function logs (query) {
return {
getLatest: async function (query) {
if (arraystore[0]) {
const querylog = arraystore.filter(msg => msg.author == query)
if (querylog[0]) {
//querylog.sort((a,b) => a.timestamp - b.timestamp)
return querylog[querylog.length - 1]
}
}
},
getLatestHash: async function (query) {
if (arraystore[0]) {
const querylog = arraystore.filter(msg => msg.author == query)
if (querylog[0]) {
//querylog.sort((a,b) => a.timestamp - b.timestamp)
return querylog[querylog.length - 1].hash
}
}
},
getFeeds: async function () {
const feeds = []
arraystore.map(msg => {
if (!feeds.includes(msg.author)) {
feeds.push(msg.author)
}
})
return feeds
},
getLog: async function () {
//arraystore.sort((a,b) => a.timestamp - b.timestamp)
return arraystore
},
query: async function (query) {
if (arraystore[0]) {
if (query.startsWith('?')) {
const querylog = arraystore.filter(msg => msg.text && msg.text.includes(query.substring(1)))
//querylog.sort((a,b) => a.timestamp - b.timestamp)
return querylog
} else {
const querylog = arraystore.filter(msg => msg.author == query || msg.hash == query)
//querylog.sort((a,b) => a.timestamp - b.timestamp)
return querylog
}
}
},
getNext: async function (hash) {
if (arraystore[0]) {
const findNext = arraystore.filter(msg => msg.previous == hash)
if (findNext[0] && findNext[0].hash != hash) {
//console.log(hash + '\'s next message is ' + findNext[0].hash)
return findNext[0].hash
}
}
},
get: async function (hash) {
const msgarray = arraystore.filter(msg => msg.hash == hash)
if (msgarray[0]) {
//console.log('we have ' + hash)
return msgarray[0]
} else {
//console.log('we do not have ' + hash)
}
},
add: function (msg) {
open(msg).then(opened => {
const dupe = arraystore.filter(message => message.hash === opened.hash)
if (dupe[0]) {
//console.log('we already have ' + opened.hash + ' not adding')
}
if (opened && !dupe[0]) {
//console.log('we do not have ' + opened.hash + ' adding')
log.push(msg)
arraystore.push(opened)
newData = true
save()
}
})
}
}
}()