-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
82 lines (72 loc) · 2.15 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* global module, require */
var pg = require('pg');
function checkMailboxArgs(args) {
// XXX-LEFT OFF HERE
}
function connectPG(dbUrl, cb) {
cb(new Error('XXX-TODO(keunwoo): implement me'));
};
function memDB(cb) {
var caps = [];
cb({
saveMailbox: function(args, cb) {
var err = checkMailboxArgs(args);
if (err) {
cb(err);
return;
}
caps.push({
mboxid: args.mboxid,
userid: args.userid,
prjid: args.prjid,
keyid: args.keyid,
credentials: args.credentials,
expiryMillis: args.expiryMillis
});
cb(null);
},
getMailbox: function(mboxid, cb) {
for (var i = 0; i < caps.length; i++) {
var c = caps[i];
if ((c.mboxid === mboxid) && (c.expiryMillis > Date.now())) {
cb(null, c);
return;
}
}
cb(new Error('no such mailbox in getMailbox'), null);
},
deleteMailbox: function(mboxid, userid, cb) {
var newCaps = [];
for (var i = 0; i < caps.length; i++) {
var c = caps[i];
if (c.mboxid === mboxid) {
if (c.userid !== userid) {
cb(new Error('user does not own mailbox'), null);
}
continue;
}
newCaps.push(c); // keep this capability
}
caps = newCaps;
cb(null);
},
listMailboxes: function(userid, cb) {
try {
var result = [];
for (var i = 0; i < caps.length; i++) {
var c = caps[i];
if ((c.userid === userid) && (c.expiryMillis > Date.now())) {
result.push(c);
}
}
cb(null, result);
} catch (exc) {
cb(exc, null);
}
}
});
};
module.exports = {
connectPG: connectPG,
memDB: memDB
};