-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.js
222 lines (196 loc) · 4.63 KB
/
data.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
var config = require('./config');
var client = require('./client');
var qs = require('querystring');
var data = {};
var parties = {};
var items = {};
var comments = {};
var countdown = 0;
var pendingActions = [];
function countdownFn(n) {
return function(x) {
n(x);
countdown--;
if (countdown <= 0) {
for (var index in pendingActions) {
pendingActions[index]();
}
}
};
}
function makeUpdater(storage, id, seq)
{
return function(doc) {
storage[id] = {
seq: seq,
doc: doc
};
};
}
function shortcutComment(uuid, comment)
{
comment._id = uuid;
comments[uuid] = { seq: -1, doc: comment };
}
function load(path, seq, storage) {
var params = {};
if (seq) {
params.since = seq,
params.feed = 'longpoll'
}
couchGet(path+'/_changes', params, function(initial) {
for (var index in initial.results) {
var change = initial.results[index];
var fn = function(x) { return x; };
if (seq == undefined) {
countdown++;
fn = countdownFn;
}
couchGet(path + '/' + change.id, fn(makeUpdater(storage, change.id, change.seq)));
}
load(path, initial.last_seq, storage);
});
}
function pend(action) {
if (countdown == 0) {
action();
}
else {
pendingActions.push(action);
}
}
load('/party', undefined, parties);
load('/items', undefined, items);
load('/chat', undefined, comments);
// call as couchGet(path, callback)
// or as couchGet(path, params, callback)
function couchGet(path, params, callback) {
if (!callback) {
callback = params;
params = undefined;
}
var querystring = qs.stringify(params)
if (querystring.length > 0) {
querystring = '?' + querystring;
}
var options = {
host: config.couch.server,
port: 443,
path: path + querystring,
headers: {
Host: config.couch.server
}
};
if (config.couch.basicauth) {
options.headers.Authorization = "Basic " + config.couch.basicauth;
}
client.get(options, callback);
}
function couchPost(path, body, callback) {
var options = {
host: config.couch.server,
port: 443,
path: path,
method: 'PUT',
headers: {
Host: config.couch.server
}
};
if (config.couch.basicauth) {
options.headers.Authorization = "Basic " + config.couch.basicauth;
}
client.post(options, JSON.stringify(body), callback);
}
function partyHasUser(party, user) {
return true;
}
function getItems(session, partyid, since, callback) {
if (!since) {
since = 0;
}
var results = [];
var last_seq = since;
for (var id in items) {
var item = items[id];
if (item.seq > last_seq && item.doc.partyid === partyid) {
results.push(id);
last_seq = Math.max(last_seq, item.seq);
}
}
callback({last_seq:last_seq,items:results});
}
function getParties(session, since, callback) {
if (!since) {
since = 0;
}
pend(function(){
var userid = session.user ? session.user.id : '';
var results = [];
var last_seq = since;
for (var id in parties) {
var party = parties[id];
if (party.seq > last_seq) {
results.push(id);
last_seq = Math.max(last_seq, party.seq);
}
}
callback({last_seq:last_seq,parties:results});
});
}
function getComments(session, partyid, since, callback) {
if (!since) {
since = 0;
}
var results = [];
var last_seq = since;
for (var id in comments) {
var comment = comments[id];
if ((comment.seq < 0 || comment.seq > last_seq) && comment.doc.partyid === partyid) {
results.push(id);
last_seq = Math.max(last_seq, comment.seq);
}
}
callback({last_seq:last_seq,comments:results});
}
function updateParty(session, party, callback) {
var path = '/party/'+party._id;
couchPost(path, party, callback);
}
function getParty(session, partyid, callback) {
pend(function(){
var party = parties[partyid];
callback(party.doc);
});
}
function getComment(session, chatid, callback) {
pend(function(){
var comment = comments[chatid];
if (!comment) {
callback({error:"permission denied"});
}
else {
callback(comment.doc);
}
});
}
function getItem(session, itemid, callback) {
pend(function(){
var item = items[itemid];
if (!item) {
callback({error:"permission denied"});
}
else {
callback(item.doc);
}
});
}
exports.getParty = getParty;
exports.getParties = getParties;
exports.getItem = getItem;
exports.getComment = getComment;
exports.getComments = getComments;
exports.getItems = getItems;
exports.updateParty = updateParty;
exports.couchGet = couchGet;
exports.couchPost = couchPost;
exports.shortcutComment = shortcutComment;