-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
117 lines (96 loc) · 2.42 KB
/
main.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
'use strict';
var level = require('level');
var ttl = require('level-ttl');
var uuid = require('uuid');
var Sublevel = require('level-sublevel');
var concat = require('concat-stream');
var Publico = function (user, options) {
var self = this;
var DEFAULT_TTL = 10000; // 10 seconds
var CHAT_TTL_LONG = 259200000; // 3 days
var setTime = function () {
return Date.now();
};
if (!options) {
options = {};
}
this.user = user;
this.dbPath = options.db || './db';
this.limit = options.limit || 30;
this.db = Sublevel(level(this.dbPath, {
createIfMissing: true,
valueEncoding: 'json'
}));
this.db = ttl(this.db, { checkFrequency: options.frequency || 10000 });
var sendChat = function (key, chat, created, options, callback) {
var ttl = DEFAULT_TTL;
if (options.ttl) {
ttl = parseInt(options.ttl, 10);
if (isNaN(ttl)) {
ttl = DEFAULT_TTL;
}
}
callback(null, {
message: chat,
fingerprint: options.fingerprint || '',
media: options.media || false,
owner: options.owner || false,
ttl: ttl,
key: key,
created: created
});
};
this.getChat = function (key, callback) {
self.db.get(key, function (err, chat) {
if (err || !chat) {
callback(new Error('Chat not found'));
} else {
callback(null, chat);
}
});
};
this.getChats = function (reverse, key, callback) {
var rs = self.db.createReadStream({
limit: self.limit,
reverse: reverse,
start: key || '\x00'
});
rs.pipe(concat(function (chats) {
callback(null, {
chats: chats
});
}));
rs.on('error', function (err) {
callback(err);
});
};
this.addChat = function (chat, options, callback) {
var ttl = DEFAULT_TTL;
if (!options) {
options = {};
}
if (options.ttl) {
ttl = parseInt(options.ttl, 10);
if (isNaN(ttl)) {
ttl = DEFAULT_TTL;
}
}
var created = setTime();
var key = setTime() + '!' + uuid.v4();
self.db.put(key, {
fingerprint: options.fingerprint || '',
message: chat,
media: options.media || false,
owner: options.owner || false,
ttl: ttl,
created: created
}, { ttl: ttl }, function (err) {
if (err) {
callback(err);
} else {
sendChat(key, chat, created, options, callback);
}
});
};
};
module.exports = Publico;