-
Notifications
You must be signed in to change notification settings - Fork 16
/
dataService.js
136 lines (122 loc) · 3.19 KB
/
dataService.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
136
const fs = require('fs');
const diskusage = require('diskusage');
var usrFileName = "./users.json";
var users = {};
var fileLocked = false;
function loadUsers() {
fs.readFile(usrFileName, (err, data) => {
if (err) throw err;
users = JSON.parse(data);
});
}
async function getFreeMB() {
try {
const info = await diskusage.check('/');
return info.available / 1024 / 1024;
}
catch (err) {
console.error(err);
return undefined;
}
}
function saveUsers() {
if(!fileLocked){
getFreeMB().then(free => {
if(free > 100) {
fileLocked = true;
var json = JSON.stringify(users);
fs.writeFile(usrFileName, json, 'utf8', function (err) {
if (err) console.trace(err);
fileLocked = false;
});
}
else {
console.log(`Error: can't save user file. Not enough free space left on device (${free} MB)`);
}
});
}
}
function registerUser(msg) {
var uid = msg.chat.id;
var usr = {enabled: true, data: {from: msg.from, chat: msg.chat}};
users[uid] = usr;
saveUsers();
}
function getUser(uid) {
return users[uid];
}
function getUserList() {
return Object.keys(users);
}
function setMetaData(uid, key, val) {
users[uid].data[key] = val;
saveUsers();
}
function getMetaData(uid, key) {
return users[uid].data[key];
}
function assertCounter(uid, id) {
if(users[uid]) {
if(users[uid].counter) {
if(users[uid].counter[id]) {
if("value" in users[uid].counter[id]) {
return true;
}
else {
users[uid].counter[id].value = 0;
}
}
else {
users[uid].counter[id] = {};
users[uid].counter[id].value = 0;
saveUsers();
}
}
else {
users[uid].counter = {};
if(users[uid].count && id == '0') {//old counter detected, migrate count
users[uid].counter[id] = {value: users[uid].count};
delete users[uid].count;
}
else {
users[uid].counter[id] = {};
users[uid].counter[id].value = 0;
}
saveUsers();
}
}
else {
//console.log("[ERROR] User ID", uid, "does not exist in database");
var usr = {enabled: true, data: {from: undefined, chat: undefined, error: "user was not initialized properly"}, counter: {"0": {"value": 1}}};
users[uid] = usr;
saveUsers();
}
}
function setCounter(uid, id, val) {
assertCounter(uid, id);
users[uid].counter[id].value = val;
saveUsers();
}
function getCounter(uid, id) {
assertCounter(uid, id);
try {
return users[uid].counter[id].value;
}
catch (e) {
return 0;
}
}
function getAllCounters(uid) {
assertCounter(uid, '0');
return users[uid].counter;
}
module.exports = {
loadUsers,
registerUser,
getUserList,
setMetaData,
getMetaData,
setCounter,
getCounter,
getAllCounters
};