Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

poll_data: Move `handler` class property inside the constructor.

Lalit3fb27c9b1c67
diff --git a/web/shared/src/poll_data.js b/web/shared/src/poll_data.js
index 8399884b88e..25df9d25780 100644
--- a/web/shared/src/poll_data.js
+++ b/web/shared/src/poll_data.js
@@ -28,6 +28,140 @@ export class PollData {
this.comma_separated_names = comma_separated_names;
this.report_error_function = report_error_function;
+ this.handle = {
+ new_option: {
+ outbound: (option) => {
+ const event = {
+ type: "new_option",
+ idx: this.my_idx,
+ option,
+ };
+
+ this.my_idx += 1;
+
+ return event;
+ },
+
+ inbound: (sender_id, data) => {
+ // All message readers may add a new option to the poll.
+ const idx = data.idx;
+ const option = data.option;
+ const options = this.get_widget_data().options;
+
+ // While the UI doesn't allow adding duplicate options
+ // to an existing poll, the /poll command syntax to create
+ // them does not prevent duplicates, so we suppress them here.
+ if (this.is_option_present(options, option)) {
+ return;
+ }
+
+ if (!Number.isInteger(idx) || idx < 0 || idx > MAX_IDX) {
+ this.report_error_function("poll widget: bad type for inbound option idx");
+ return;
+ }
+
+ if (typeof option !== "string") {
+ this.report_error_function("poll widget: bad type for inbound option");
+ return;
+ }
+
+ const key = sender_id + "," + idx;
+ const votes = new Map();
+
+ this.key_to_option.set(key, {
+ option,
+ user_id: sender_id,
+ votes,
+ });
+
+ // I may have added a poll option from another device.
+ if (sender_id === this.me && this.my_idx <= idx) {
+ this.my_idx = idx + 1;
+ }
+ },
+ },
+
+ question: {
+ outbound: (question) => {
+ const event = {
+ type: "question",
+ question,
+ };
+ if (this.is_my_poll) {
+ return event;
+ }
+ return undefined;
+ },
+
+ inbound: (sender_id, data) => {
+ // Only the message author can edit questions.
+ if (sender_id !== this.message_sender_id) {
+ this.report_error_function(
+ `user ${sender_id} is not allowed to edit the question`,
+ );
+ return;
+ }
+
+ if (typeof data.question !== "string") {
+ this.report_error_function("poll widget: bad type for inbound question");
+ return;
+ }
+
+ this.set_question(data.question);
+ },
+ },
+
+ vote: {
+ outbound: (key) => {
+ let vote = 1;
+
+ // toggle
+ if (this.key_to_option.get(key).votes.get(this.me)) {
+ vote = -1;
+ }
+
+ const event = {
+ type: "vote",
+ key,
+ vote,
+ };
+
+ return event;
+ },
+
+ inbound: (sender_id, data) => {
+ // All message readers may vote on poll options.
+ const key = data.key;
+ const vote = data.vote;
+
+ if (typeof key !== "string") {
+ this.report_error_function("poll widget: bad type for inbound vote key");
+ return;
+ }
+
+ if (!Number.isInteger(vote) || !(vote === 1 || vote === -1)) {
+ this.report_error_function("poll widget: bad value for inbound vote count");
+ return;
+ }
+
+ const option = this.key_to_option.get(key);
+
+ if (option === undefined) {
+ this.report_error_function("unknown key for poll: " + key);
+ return;
+ }
+
+ const votes = option.votes;
+
+ if (vote === 1) {
+ votes.set(sender_id, 1);
+ } else {
+ votes.delete(sender_id);
+ }
+ },
+ },
+ };
+
if (question) {
this.set_question(question);
}
@@ -85,140 +219,6 @@ export class PollData {
return widget_data;
}
- handle = {
- new_option: {
- outbound: (option) => {
- const event = {
- type: "new_option",
- idx: this.my_idx,
- option,
- };
-
- this.my_idx += 1;
-
- return event;
- },
-
- inbound: (sender_id, data) => {
- // All message readers may add a new option to the poll.
- const idx = data.idx;
- const option = data.option;
- const options = this.get_widget_data().options;
-
- // While the UI doesn't allow adding duplicate options
- // to an existing poll, the /poll command syntax to create
- // them does not prevent duplicates, so we suppress them here.
- if (this.is_option_present(options, option)) {
- return;
- }
-
- if (!Number.isInteger(idx) || idx < 0 || idx > MAX_IDX) {
- this.report_error_function("poll widget: bad type for inbound option idx");
- return;
- }
-
- if (typeof option !== "string") {
- this.report_error_function("poll widget: bad type for inbound option");
- return;
- }
-
- const key = sender_id + "," + idx;
- const votes = new Map();
-
- this.key_to_option.set(key, {
- option,
- user_id: sender_id,
- votes,
- });
-
- // I may have added a poll option from another device.
- if (sender_id === this.me && this.my_idx <= idx) {
- this.my_idx = idx + 1;
- }
- },
- },
-
- question: {
- outbound: (question) => {
- const event = {
- type: "question",
- question,
- };
- if (this.is_my_poll) {
- return event;
- }
- return undefined;
- },
-
- inbound: (sender_id, data) => {
- // Only the message author can edit questions.
- if (sender_id !== this.message_sender_id) {
- this.report_error_function(
- `user ${sender_id} is not allowed to edit the question`,
- );
- return;
- }
-
- if (typeof data.question !== "string") {
- this.report_error_function("poll widget: bad type for inbound question");
- return;
- }
-
- this.set_question(data.question);
- },
- },
-
- vote: {
- outbound: (key) => {
- let vote = 1;
-
- // toggle
- if (this.key_to_option.get(key).votes.get(this.me)) {
- vote = -1;
- }
-
- const event = {
- type: "vote",
- key,
- vote,
- };
-
- return event;
- },
-
- inbound: (sender_id, data) => {
- // All message readers may vote on poll options.
- const key = data.key;
- const vote = data.vote;
-
- if (typeof key !== "string") {
- this.report_error_function("poll widget: bad type for inbound vote key");
- return;
- }
-
- if (!Number.isInteger(vote) || !(vote === 1 || vote === -1)) {
- this.report_error_function("poll widget: bad value for inbound vote count");
- return;
- }
-
- const option = this.key_to_option.get(key);
-
- if (option === undefined) {
- this.report_error_function("unknown key for poll: " + key);
- return;
- }
-
- const votes = option.votes;
-
- if (vote === 1) {
- votes.set(sender_id, 1);
- } else {
- votes.delete(sender_id);
- }
- },
- },
- };
-
handle_event(sender_id, data) {
const type = data.type;
if (this.handle[type] && this.handle[type].inbound) {