This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
forked from SaftigeKumquat/Bombay-Crushed
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
executable file
·514 lines (457 loc) · 12.6 KB
/
server.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#!/usr/bin/env node
var http = require('http');
var fs = require('fs');
var Cookies = require('cookies');
var ejs = require('./ejs.js');
var lf = require('./lfcli.js');
var querystring = require('querystring');
var delegations = function(state, render) {
var delegations;
var finish = function() {
if(delegations !== undefined) {
state.context.delegations = delegations;
render();
}
}
lf.query('/delegation', {
'member_id': state.user_id(),
'direction': 'out'
}, function(res) {
var links = res.result;
var i, resolved = 0;
var resolved_delegations = [];
if(links.length) {
for(i = 0; i < links.length; i++) {
lf.query('/member', {'member_id': links[i].trustee_id}, function(res) {
var delegate = res.result[0];
resolved_delegations.push({
user: {
'name': delegate.name,
'picsmal': '/avatar/' + delegate.id
}
});
if(resolved_delegations.length === links.length) {
finish();
}
});
}
} else {
delegations = [];
finish();
}
});
};
var areas = function(state, render) {
var units, areas, memberships;
// TODO areas and memberships
// data output
var finish = function() {
var i, j, k;
// TODO this can be done more efficiently using hashes (objects)
if(units !== undefined && areas !== undefined && memberships !== undefined) {
// TODO filter all units of which you cannot become a member
for(i = 0; i < units.length; i++) {
units[i].areas = [];
}
for(i = 0; i < areas.length; i++) {
area = areas[i];
for(j = 0; j < units.length; j++) {
if(units[j].id === area.unit_id) {
// check if user is a member of the area
for(k = 0; k < memberships.length; k++) {
if(memberships[k].area_id === area.id) {
area.checked = true;
}
}
units[j].areas.push(area);
}
}
}
state.context.units = units;
render();
}
}
lf.query('/unit', {}, function(res) {
units = res.result;
finish();
});
lf.query('/area', {}, function(res) {
areas = res.result;
finish();
});
lf.query('/area', {'member_id': state.user_id()}, function(res) {
memberships = res.result;
finish();
});
}
var news = function(state, render) {
var lastBallot, criticalQuorum, voters, votes;
// data output
var finish = function() {
if(lastBallot !== undefined && criticalQuorum !== undefined && voters !== undefined && votes !== undefined) {
var news = {};
if(!lastBallot) {
news.chart = {
'title': 'No ballot has completed recently',
'for': 0,
'fordelegated': 0,
'against': 0,
'againstdelegated': 0
};
} else {
var pVoters = lastBallot.positive_votes;
var nVoters = lastBallot.negative_votes;
var pDirect = 0, pIndirect = 0;
var nDirect = 0, nIndirect = 0;
var i, j;
for(i = 0; i < voters.length; ++i) {
var voter = voters[i];
if(voter.delegate_member_id) {
for(j = 0; j < votes.length; ++j) {
var vote = votes[j];
if(vote.member_id === voter.member_id) { // issue_id should be correct by query restriction
if(vote.grade > 0) {
pDirect = pDirect + 1;
pIndirect = pIndirect + voter.weight - 1;
}
if(vote.grade < 0) {
nDirect = nDirect + 1;
nIndirect = nIndirect + voter.weight - 1;
}
}
}
}
}
news.chart = {
'title': lastBallot.name,
'for': pDirect,
'fordelegated': pIndirect,
'against': nDirect,
'againstdelegated': nIndirect
};
}
if(!criticalQuorum) {
news.graph = {
'title': 'Currently no initiative is close to the quorum',
'quorum': 0,
'support': 0,
'potential': 0,
'uninvolved': 0,
'supporter': 0,
'potsupporter': 0,
'uninterested': 0
};
} else {
news.graph = criticalQuorum;
}
state.context.news = news;
render();
}
}
// query last ballot
// erst nach issue fragen, dann winning initiative für den issue abfragen -> weniger daten
// TODO nach eigenen areas einschränken
// 1. get issues with issue_state = finished_with_winner
// 2. Select initiative closed last (we only got ones with winners -> end of voting time)
// 3. get winner of that issue
lf.query('/issue', {'issue_state': 'finished_with_winner'}, function(res) {
var i;
var issues = res.result;
var last_timestamp = 0;
var last_issue;
console.log('Completed issues: ' + issues.length);
for(i = 0; i < issues.length; ++i) {
var issue = issues[i];
closing_time = Date.parse(issue.closed);
if(closing_time > last_timestamp) {
last_timestamp = closing_time;
last_issue = issue;
}
}
if(!last_issue) {
lastBallot = false;
voters = false;
votes = false;
finish();
} else {
lf.query('/initiative', {'initiative_winner': true, 'issue_id': last_issue.id}, function(res) {
var i;
lastBallot = res.result[0];
// TODO handle no result case (there should always be a result because of finished_with_winner restriction)
lf.query('/vote', {'initiative_id': lastBallot.id}, function(res) {
votes = res.result || false;
console.log('Vote: ' + voters);
finish();
});
});
lf.query('/voter', {'issue_id': last_issue.id}, function(res) {
voters = res.result || false;
console.log('Voter: ' + voters);
finish();
});
}
});
// query critical Quorum
console.error('Query for critical quorum: not implemented');
criticalQuorum = false;
finish();
};
// Ok, not really an index, but works.
var printIndex = function(state) {
// we need a valid user session...
if(!state.session_key()) {
state.sendToLogin();
return;
}
var finish = function() {
var ctx = state.context;
if(ctx.user !== undefined && ctx.news !== undefined
&& ctx.units !== undefined && ctx.delegations !== undefined) {
ejs.render(state, '/main.tpl');
}
}
lf.query('/member', {'member_id': state.user_id()}, function(res) {
lf_user = res.result[0];
state.context.user = {
'nick': lf_user.name,
'picbig': '/picbig/' + lf_user.id
};
finish();
} );
news(state, finish);
areas(state, finish);
delegations(state, finish);
}
var showProfile = function(state) {
ejs.render(state, '/profile.tpl');
}
var showContacts = function(state) {
ejs.render(state, '/contacts.tpl');
}
var invalidURL = function(state) {
var res = state.result;
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('Kuckst du woanders!\n');
}
var serverError = function(state, logmessage, errorcode) {
if(!errorcode) {
errorcode = 500;
}
console.error('ERROR: ' + logmessage + ' – Sent code ' + errorcode + ' to the client');
console.trace();
var res = state.result;
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('I feel blue. Guess I\'ll go swimming!\n');
}
var performLogin = function(state) {
// get form data
if(state.request.method !== 'POST') {
state.sendToLogin();
return;
}
// collect data
body = '';
state.request.on('data', function(chunk) {
body += chunk;
});
state.request.on('end', function() {
data = querystring.parse(body);
lf.perform('/session', { key: data.key }, function(res) {
state.session_key(res.session_key);
lf.query('/info', { session_key: state.session_key() }, function(res) {
state.user_id(res.current_member_id);
printIndex(state);
});
});
});
}
var performLogout = function(state) {
// *nom* *nom*
state.session_key(null);
state.user_id(null);
// and run
printIndex(state);
}
var serveStatic = function(state) {
// TODO guess content type
// stream from file to requestee
// TODO could probably be read chunkwise
filepath = __dirname + '/html' + state.request.url;
console.log('Serving: ' + filepath);
fs.readFile(filepath, function(err, data) {
if(err) {
invalidURL(state);
return;
}
state.result.end(data);
console.log('Served: ' + filepath);
});
}
var sendPicture = function(state) {
var user_id = state.request.url.slice('/picbig/'.length);
console.log('Retrieving portrait for user ' + user_id);
var query_obj = {
'type': 'portrait',
'member_id': user_id
}
if(state.session_key()) {
query_obj.session_key = state.session_key()
};
lf.query('/member_image', query_obj, function(result) {
var response = state.result;
if(result.status === 'ok' && result.result.length) {
var image = result.result[0];
response.setHeader("Content-Type", result.content_type);
response.end(result.data);
} else {
state.fail('No image found for user ' + user_id, 404);
}
});
}
var sendAvatar = function(state) {
var user_id = state.request.url.slice('/avatar/'.length);
console.log('Retrieving avatar for user ' + user_id);
var query_obj = {
'type': 'avatar',
'member_id': user_id
}
if(state.session_key()) {
query_obj.session_key = state.session_key()
};
lf.query('/member_image', query_obj, function(result) {
var response = state.result;
if(result.status === 'ok' && result.result.length) {
var image = result.result[0];
response.setHeader("Content-Type", result.content_type);
response.end(result.data);
} else {
state.fail('No avatar found for user ' + user_id, 404);
}
});
}
/**
* Mapping from URLs to functions
*/
var url_mapping = {
'/': printIndex,
'/index.html': printIndex,
'/login': performLogin,
'/logout': performLogout,
'/profile.html': showProfile,
'/contacts.html': showContacts
}
/**
* Mapping from patterns to functions
*/
var pattern_mapping = [
{ pattern: '/picbig/', mapped: sendPicture },
{ pattern: '/avatar/', mapped: sendAvatar },
{ pattern: '/css/', mapped: serveStatic },
{ pattern: '/js/', mapped: serveStatic },
{ pattern: '/img/', mapped: serveStatic },
{ pattern: '/content_img/', mapped: serveStatic },
{ pattern: /^\/\w+\.html/, mapped: serveStatic }
];
/**
* Function to Map URLs to functions
*/
mapU2F = function(state, url_mappings, pattern_mappings) {
// Forward decleration of variables as recommended by Crockford
var i;
var mapped;
var req = state.request;
console.log('Request url: ' + req.url);
// check whether the url has a direct mapping
mapped = url_mappings[req.url];
if (mapped) {
mapped(state);
} else {
// url did not match any of the direct mappingss
// check whether it matches anny of the patterns
for (i = 0; !mapped && i < pattern_mappings.length; i = i + 1) {
pattern = pattern_mappings[i].pattern;
//console.log('Testing pattern ' + i + ': ' + pattern + ' [' + typeof(pattern) + ']');
if( typeof(pattern) === 'string' && req.url.slice(0, pattern.length) === pattern ) {
mapped = pattern_mappings[i].mapped;
} else if ( pattern.test && typeof(pattern.test) === 'function' && pattern.test(req.url) ) {
// we can only assume it's a regexp, as typeof is not clear on it,
// but if it has a test function...
mapped = pattern_mappings[i].mapped;
}
}
if (mapped) {
mapped(state);
} else {
console.log(req.url + ' has not been mapped');
invalidURL(state);
}
}
}
/**
* Create an object that keeps the state during a full request / response
* cycle. This state will not be kept between request. It's purpose is to
* bundle all data collected while building the response to one request.
*/
var createState = function(req, res) {
var state = {
'request': req,
'result': res,
'fail': function(logmessage, errorcode) {
serverError(state, logmessage, errorcode);
},
'cookies': new Cookies(req, res),
'sendToLogin': function(message) {
// TODO pass message to template
ejs.render(state, '/login.tpl');
},
'context': {}
};
var session_key, user_id;
// convenience..
state.session_key = function(key) {
if(key === undefined) {
if(session_key === undefined) {
session_key = state.cookies.get('session_key');
}
return session_key;
} else {
session_key = key;
state.cookies.set('session_key', session_key);
return session_key;
}
}
state.user_id = function(id) {
if(id === undefined) {
if(user_id === undefined) {
user_id = state.cookies.get('user_id');
}
return user_id;
} else {
user_id = id;
state.cookies.set('user_id', user_id);
return user_id;
}
}
return state;
}
server = function() {
// make sure uncaught exceptions don't kill the server
process.on('uncaughtException', function (err) {
if(err.stack) {
err = err.stack;
}
console.error('ERROR: Exception not handled properly: ' + err);
});
lf.query('/info', {}, function(res) {
server = lf.getBaseURL();
console.log('Connected to ' + server.host + ':' + server.port);
console.log('Core Version: ' + res.core_version);
console.log('API Version: ' + res.api_version);
});
http.createServer(function (req, res) {
var state = createState(req, res);
mapU2F(state, url_mapping, pattern_mapping);
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
};
server();