-
Notifications
You must be signed in to change notification settings - Fork 1
/
ComposePoll.gs
174 lines (126 loc) · 6.2 KB
/
ComposePoll.gs
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
const CHOICE_CUTOFF_LENGTH = 32;
var PollComposer = function() {
this.split_choice_description = function(choice_text) {
let description_marker_index = choice_text.indexOf('->');
if (description_marker_index > -1)
return { value: choice_text.substring(0, description_marker_index), description: choice_text.substring(description_marker_index + 2) };
if (choice_text.length > CHOICE_CUTOFF_LENGTH) // TODO: Choose a better way to introduce a hard break if the choice was too long
return { value: choice_text.substring(0, CHOICE_CUTOFF_LENGTH), description: choice_text.substring(CHOICE_CUTOFF_LENGTH + 1) };
return { value: choice_text, description: "" }
}
this.compose_vote_button = function(choice_value, poll, index, expired) {
return {
textButton: {
text: choice_value,
disabled: expired,
onClick: {
action: {
actionMethodName: "update-vote",
parameters: [
{ key: "poll", value: JSON.stringify(poll) },
{ key: "index", value: index.toString() }
]
}
}
}
}
}
this.compose_vote_section = function(poll, index, total_votes, expired) {
let response = poll.responses[index];
let choice_text = poll.choices[index];
let { value, description } = this.split_choice_description(choice_text);
let widgets = [{ buttons: [this.compose_vote_button(value, poll, index, expired)] }];
widgets.push({ textParagraph: { text: `<i><font color='#3b78e7'>${description}</font></i>` }});
let vote_percent = total_votes > 0 ? 100 * response.voters.length / total_votes : 0;
widgets.push({ textParagraph: { text: Utilities.formatString("%.2f\% / (%d)", vote_percent, response.voters.length) }});
if (!poll.options.anonymous)
{
let voters_list = response.voters.join(", ");
widgets.push({ textParagraph: { text: `<i><font color='#808080'>${voters_list}</font></i>` }});
}
return { widgets: widgets };
}
this.compose_footer_section = function(total_votes, expired) {
let widgets = [{ textParagraph: { text: `<b>Total votes: ${total_votes}<b>` }}];
if (expired)
widgets.push({ textParagraph: { text: "<font color='#FF0000'>Poll has expired!</font>" }});
return { widgets: widgets };
}
this.compose_message_body = function(poll, total_votes, expired, action_response_type) {
let sections = [];
let poll_options = `[Type: ${poll.options.single_choice ? "Single Choice" : "Multi Choice"}, Anonymous: ${poll.options.anonymous ? "Yes" : "No"}]`;
sections.push({ widgets: [{ textParagraph: { text: poll_options }}] });
let initiated_on = new Date(poll.initiated_on);
let expires_on = new Date(initiated_on.getTime() + 1000 * poll.options.expiration_time_in_seconds);
initiated_on = Utilities.formatDate(initiated_on, "GMT", "yyyy-MM-dd HH:mm:ss z"); // TODO: Set the zone correctly per each user
expires_on = Utilities.formatDate(expires_on, "GMT", "yyyy-MM-dd HH:mm:ss z"); // TODO: Set the zone correctly per each user
let poll_expiration = `[Initiated On: ${initiated_on}, Expires On: ${poll.options.expiration_time_in_seconds == 0.0 ? "Unbounded" : expires_on}]`;
sections.push({ widgets: [{ textParagraph: { text: poll_expiration }}] });
for (let i = 0; i < poll.responses.length; ++i) {
sections.push(this.compose_vote_section(poll, i, total_votes, expired));
}
sections.push(this.compose_footer_section(total_votes, expired));
return {
actionResponse: { type: action_response_type },
cards: [{
header: { title: poll.question, subtitle : `Poll created by ${poll.poller}.`, imageUrl : DEFAULT_IMAGE_URL },
sections: sections
}]
};
}
this.compose = function(poll_details) {
let poll = poll_details;
// Add a placeholder to record votes
poll.responses = [];
for (let choice_index in poll.choices) {
poll.responses.push({ voters: [] });
}
poll.initiated_on = new Date();
return this.compose_message_body(poll, 0, false, "NEW_MESSAGE");
}
this.update_poll_multi_choice = function(voter, poll, index) {
let response = poll.responses[index];
let voter_index = response.voters.indexOf(voter);
if (voter_index > -1)
response.voters.splice(voter_index, 1); // Already voted so remove
else
response.voters.push(voter); // Didn't use this response yet
}
this.update_poll_single_choice = function(voter, poll, index) {
// if the user clicked on the same choice then remove.
let voter_response = poll.responses[index];
let voter_index = voter_response.voters.indexOf(voter);
if (voter_index > -1) {
voter_response.voters.splice(voter_index, 1);
return;
}
// search all other responses and see if the voter has already voted .. if so remove and then add the new selection
for (let response of poll.responses) {
if (response == voter_response)
continue;
let voter_index = response.voters.indexOf(voter);
if (voter_index > -1) {
response.voters.splice(voter_index, 1);
break;
}
}
voter_response.voters.push(voter); // Set the new selection
}
this.is_poll_still_active = function(poll) {
if (poll.options.expiration_time_in_seconds == 0.0)
return true;
let current_time = new Date();
let initiated_on = new Date(poll.initiated_on);
let date_diff_in_milliseconds = current_time - initiated_on;
return date_diff_in_milliseconds <= 1000 * poll.options.expiration_time_in_seconds;
}
this.update = function(voter, poll, index) {
let poll_active = this.is_poll_still_active(poll);
if (poll_active) {
let poll_update_method = poll.options.single_choice ? this.update_poll_single_choice : this.update_poll_multi_choice;
poll_update_method(voter, poll, index);
}
let total_votes = poll.responses.reduce(function(total, response) { return total + response.voters.length; }, 0);
return this.compose_message_body(poll, total_votes, !poll_active, "UPDATE_MESSAGE");
}
}