-
Notifications
You must be signed in to change notification settings - Fork 0
/
smvm-registry.js
117 lines (103 loc) · 3.42 KB
/
smvm-registry.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
/* jshint strict:false */
/* global require, console, module */
var Promise = require('bluebird'),
dism = require('./smvm-components'),
colour = require('colors'),
castvote = (smop, config) => {
return (args) => {
var whitelist = config.whitelist, // "config" is set at configuration time
candidates = config.candidates,
ballot = args.ballot, // "Args" are passed in through REST calls
voter = args.auth_user;
return Promise.all([smop.getState('votes'), whitelist({id:voter})]).spread((votes, authorised) => {
// check candidate authorised through remote call to whitelist op
if (!authorised) {
throw Error("Not authorised to vote in this election");
}
// check validity of ballot
if (candidates.map((x) => x.id).indexOf(ballot) < 0) {
throw Error("Not a valid candidate, check your ballot again");
}
// store vote for later tallying. note that this creates secret information
// in the voter state, which persists with the instance.
votes = (votes || []).filter((x) => x[0] !== voter).concat([[voter, ballot]]);
return smop.setState({votes:votes}).then(() => "cool!");
});
};
},
tallyvote = (sm, config) => {
return () => {
// get state from the social machine instance
return sm.getState('votes').then((votes) => {
// now reduce into a counts
return votes.reduce((counts, votepair) => {
var voter = votepair[0], ballot = votepair[1];
counts[ballot] = counts[ballot] + 1 || 1;
return counts;
}, {});
});
};
},
whitelist = (sm, config, req) => {
return (args) => {
// auth_user is a special variable
return Promise.resolve(config.acl.indexOf(args.auth_user) >= 0);
};
};
module.exports = {
populate:(smvm) => {
// smvm.registerProtocols([closedVote, whitelist]);
this.makeElection(smvm);
},
getRegistry:() => {
return { castvote:castvote, whitelist:whitelist, tallyvote:tallyvote };
},
makeElection:(smvm) => {
return smvm.newSocialMachine().then((sm) => {
return sm.newOp('whitelist',
{
acl : ['http://hip.cat/emax', '[email protected]', '[email protected]']
}
).then((wl) => {
return sm.newOp('castvote',
{
whitelist:wl,
candidates: [
{id:'http://makeamericangreatagain.com/#trump', name:'donald trump'},
{id:'http://hillary.com/#clinton', name:'hillary clinton'}
]
}).then((cvote) => {
return sm.newOp('tallyvote').then((tvote) => {
return [wl, cvote, tvote].reduce((mapping, op) => {
console.log('op'.green, op.iid.cyan, op.getURLs());
mapping[op.protid] = op.getURLs();
return mapping;
}, {});
});
});
});
});
},
makeClosedElection :(smvm) => {
var election = new dism.SocialMachine(),
whitelist = new dism.SMOp('whitelist',
{ acl : ['http://hip.cat/emax', '[email protected]', '[email protected]'] }
),
castvote = new dism.SMOp('castvote', {
whitelist:whitelist,
candidates: [
{id:'http://makeamericangreatagain.com/#trump', name:'donald trump'},
{id:'http://hillary.com/#clinton', name:'hillary clinton'}
]
}),
tallyvote = new dism.SMOp('tallyvote');
smvm.addSM(election);
election.addOp(whitelist, castvote, tallyvote);
return election.load().then(() => {
return [whitelist, castvote, tallyvote].reduce((mapping, op) => {
mapping[op.protid] = op.getURLs();
return mapping;
}, {});
});
}
};