forked from SaftigeKumquat/Bombay-Crushed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelegations.js
125 lines (114 loc) · 3.53 KB
/
delegations.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
/**
* Utility functions for querying delegations
*/
var lf = require('./lfcli.js');
var userFunc = require('./user.js');
var logger = require('./logger.js');
// Basic functions on JS objects
var registerFunctions = function() {
/**
* checks if an object is contained in an array
*
* @param obj object to be checked
*/
Array.prototype.contains = function(obj) {
for (var i = 0; i < this.length; i++) {
if (this[i] === obj) {
return true;
}
}
return false;
}
}
/**
* Query all data required for the delegations box on the overview page
* and the delegations display on the contacts page.
*
* The resolved delegations are stored in `state.context.delegations`.
*
* @param state The state object of the current HTTP-Request
* @param render The callback to notify once all data has been retrieved.
*/
exports.lastActions = function(state, render) {
var delegations;
registerFunctions();
/**
* Wrapper for the external callback that stores the results into
* the state object before invoking the external callback.
*/
var finish = function() {
if(delegations !== undefined) {
state.context.delegations = delegations;
render();
}
}
// Query all outgoing delegations of the current user
lf.query('/delegation', {
'member_id': state.user_id(),
'direction': 'out'
}, state, function(res) {
var links = res.result;
var i, resolved = 0;
var resolved_delegations = [];
var pending_resolves = 0;
var trustees = [];
// follow the delegations and resolve the user information
if(links.length) {
for(i = 0; i < links.length; i++) {
// check we don't query a trustee twice
if(trustees.contains(links[i].trustee_id)) {
continue;
}
else {
trustees.push(links[i].trustee_id);
pending_resolves++; // don't finish until we know who it is and what he did
logger(2, 'Query trustee name: ' + links[i].trustee_id);
}
lf.query('/member', {'member_id': links[i].trustee_id }, state, function(res) {
var delegate = res.result[0];
logger(3, JSON.stringify(delegate));
var info_obj = {
'user': userFunc.getUserBasic(delegate)
};
// get last action of user
lf.query('/vote', {'member_id': delegate.id,
'issue_state': 'finished_with_winner,finished_without_winner'
}, state, function(res) {
if(res.result && res.result.length > 0) {
logger(3, JSON.stringify(res));
// TODO sort these properly instead of taking an arbitrary one
vote = res.result[0];
if(vote.grade > 0) {
info_obj.action = 'for';
} else {
info_obj.action = 'against';
}
// get information about the initiative the last action was performed on
lf.query('/initiative', {'initiative_id': vote.initiative_id}, state, function(res) {
info_obj.title = res.result[0].name;
info_obj.initiative_id = res.result[0].id;
resolved_delegations.push(info_obj);
pending_resolves--; // we now know what she did
// if all delegations have been handled finish it up
if(pending_resolves === 0) {
delegations = resolved_delegations;
finish();
}
});
} else {
pending_resolves--; // we now know that this guy didn't do anything
// if all delegations have been handled finish it up
if(pending_resolves === 0) {
delegations = resolved_delegations;
finish();
}
}
});
});
}
} else {
delegations = [];
finish();
}
});
};