forked from maratonusp/contestwatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
59 lines (49 loc) · 1.1 KB
/
db.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
const logger = require('./logger');
const lowdb = require('lowdb');
const low = lowdb('db.json', {
storage: require('lowdb/lib/storages/file-async')
});
module.exports = {low: low};
module.exports.user = (function () {
var User = {};
low.defaults({users: []}).write();
User.create = function (chat) {
logger.info('Creating user ' + chat.id);
low.get('users')
.push({
id: chat.id,
notify: false,
ignore: {calendar: true},
last_activity: Date.now(),
cf_handles: [],
is_group: chat.type == "group"
})
.write();
return low
.get('users')
.find({id : chat.id});
};
User.get = function (chat) {
var user = low
.get('users')
.find({id : chat.id});
if (user.isUndefined().value())
return module.exports.user.create(chat);
return user;
};
User.get_by_id = function (id) {
var user = low
.get('users')
.find({id : id});
return user;
};
User.migrate = function (old_id, new_id) {
logger.info('Migrating user ' + old_id + ' to ' + new_id);
var user = User
.get(old_id)
.assign({id : new_id})
.write();
};
return User;
})();
// vim:noet