poll: Handle duplicate poll options.

Fixes #20164.
This commit is contained in:
isakhagg 2021-11-09 16:48:42 +01:00 committed by Tim Abbott
parent 7a431a690d
commit 7edbf673e4
2 changed files with 29 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import render_widgets_poll_widget from "../templates/widgets/poll_widget.hbs";
import render_widgets_poll_widget_results from "../templates/widgets/poll_widget_results.hbs"; import render_widgets_poll_widget_results from "../templates/widgets/poll_widget_results.hbs";
import * as blueslip from "./blueslip"; import * as blueslip from "./blueslip";
import {$t} from "./i18n";
import * as people from "./people"; import * as people from "./people";
export function activate({ export function activate({
@ -154,11 +155,13 @@ export function activate({
elem.find("button.poll-option").on("click", (e) => { elem.find("button.poll-option").on("click", (e) => {
e.stopPropagation(); e.stopPropagation();
check_option_button();
submit_option(); submit_option();
}); });
elem.find("input.poll-option").on("keydown", (e) => { elem.find("input.poll-option").on("keyup", (e) => {
e.stopPropagation(); e.stopPropagation();
check_option_button();
if (e.key === "Enter") { if (e.key === "Enter") {
submit_option(); submit_option();
@ -172,6 +175,23 @@ export function activate({
}); });
} }
function check_option_button() {
const poll_option_input = elem.find("input.poll-option");
const option = poll_option_input.val().trim();
const options = poll_data.get_widget_data().options;
if (poll_data.is_option_present(options, option)) {
elem.find("button.poll-option").attr("disabled", true);
elem.find("button.poll-option").attr(
"title",
$t({defaultMessage: "Option already present."}),
);
} else {
elem.find("button.poll-option").attr("disabled", false);
elem.find("button.poll-option").removeAttr("title");
}
}
function render_results() { function render_results() {
const widget_data = poll_data.get_widget_data(); const widget_data = poll_data.get_widget_data();

View File

@ -103,6 +103,14 @@ export class PollData {
// All message readers may add a new option to the poll. // All message readers may add a new option to the poll.
const idx = data.idx; const idx = data.idx;
const option = data.option; 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) { if (!Number.isInteger(idx) || idx < 0 || idx > MAX_IDX) {
this.report_error_function("poll widget: bad type for inbound option idx"); this.report_error_function("poll widget: bad type for inbound option idx");