-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterface.js
91 lines (83 loc) · 2.41 KB
/
interface.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
var util = require('./util.js')
var through = require('through2')
var concat = require('concat-stream')
function Interface (db) {
if (!(this instanceof Interface)) return new Interface(db)
this.db = db
}
Interface.prototype.writeMessage = function (channel, nick, msg) {
var self = this
self.db.get(`${channel}/latest`, (err, node) => {
node = node || {value: 0}
var latest = parseInt(node.value)
var newLatest = latest + 1
self.db.put(`${channel}/${newLatest}`, {text: msg, author: nick, time: Date.now()}, (err, node) => {
self.db.put(`${channel}/latest`, newLatest)
})
})
}
var channelPattern = /channels\/(.*)\/.*/
Interface.prototype.getChannels = function () {
var self = this
return new Promise((resolve, reject) => {
var stream = self.db.createReadStream('channels')
var concatStream = concat((data) => {
var channels = {}
data.forEach((d) => {
var match = channelPattern.exec(d)
if (match && match[1]) {
channels[match[1]] = true
}
})
resolve(Object.keys(channels))
})
stream
.pipe(through.obj(function (chunk, enc, next) {
if (chunk.key.slice(-6) !== 'latest') this.push([chunk.key])
next()
}))
.pipe(concatStream)
})
}
Interface.prototype.test = function () {
var self = this
var stream = self.db.createReadStream('data')
stream
.pipe(through.obj(function (chunk, enc, next) {
this.push(chunk.value)
next()
}))
.pipe(process.stdout)
self.db.list('data', (arr) => {
console.log('DB.LIST')
console.log(arr)
})
}
Interface.prototype.getMessages = function (channel, max) {
var self = this
return new Promise((resolve, reject) => {
self.db.get(`${channel}/latest`, (err, node) => {
if (!node) return
var latest = node.value
var messagePromises = []
for (var i = 0; i < max; i++) {
if (latest - i < 1) break
var promise = getMessage(latest - i, channel)
messagePromises.push(promise)
}
function getMessage (time, channel) {
return new Promise((resolve, reject) => {
self.db.get(`${channel}/${time}`, (err, node) => {
if (err) reject(err)
resolve(node)
})
})
}
messagePromises.reverse()
Promise.all(messagePromises).then((messages) => {
resolve(messages)
})
})
})
}
module.exports = Interface