mirror of https://github.com/zulip/zulip.git
ts: Convert `poll_data` to typescript.
Used zod schemas to validate inbound data types and removed some over defensive code.
This commit is contained in:
parent
3fb27c9b1c
commit
ae6063807b
|
@ -44,7 +44,7 @@ def make_set(files: List[str]) -> Set[str]:
|
||||||
# We do not yet require 100% line coverage for these files:
|
# We do not yet require 100% line coverage for these files:
|
||||||
EXEMPT_FILES = make_set(
|
EXEMPT_FILES = make_set(
|
||||||
[
|
[
|
||||||
"web/shared/src/poll_data.js",
|
"web/shared/src/poll_data.ts",
|
||||||
"web/src/about_zulip.js",
|
"web/src/about_zulip.js",
|
||||||
"web/src/add_subscribers_pill.js",
|
"web/src/add_subscribers_pill.js",
|
||||||
"web/src/admin.js",
|
"web/src/admin.js",
|
||||||
|
|
|
@ -1,3 +1,74 @@
|
||||||
|
import assert from "minimalistic-assert";
|
||||||
|
import z from "zod";
|
||||||
|
|
||||||
|
export type PollDataConfig = {
|
||||||
|
message_sender_id: number;
|
||||||
|
current_user_id: number;
|
||||||
|
is_my_poll: boolean;
|
||||||
|
question: string;
|
||||||
|
options: string[];
|
||||||
|
comma_separated_names: (user_ids: number[]) => string;
|
||||||
|
report_error_function: (msg: string, more_info?: unknown) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type PollOptionData = {
|
||||||
|
option: string;
|
||||||
|
names: string;
|
||||||
|
count: number;
|
||||||
|
key: string;
|
||||||
|
current_user_vote: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
type PollOption = {
|
||||||
|
option: string;
|
||||||
|
user_id: number;
|
||||||
|
votes: Map<number, number>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type WidgetData = {
|
||||||
|
options: PollOptionData[];
|
||||||
|
question: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type InboundData = Record<string, unknown> & {type: string};
|
||||||
|
|
||||||
|
export type PollHandle = {
|
||||||
|
// Add generic key property to allow string indexing on PollHandle type in `handle_event` method.
|
||||||
|
[key: string]: {
|
||||||
|
outbound: (arg: string) => InboundData | undefined;
|
||||||
|
inbound: (sender_id: number, data: InboundData) => void;
|
||||||
|
};
|
||||||
|
new_option: {
|
||||||
|
outbound: (option: string) => {type: string; idx: number; option: string};
|
||||||
|
inbound: (sender_id: number, data: InboundData) => void;
|
||||||
|
};
|
||||||
|
question: {
|
||||||
|
outbound: (question: string) => {type: string; question: string} | undefined;
|
||||||
|
inbound: (sender_id: number, data: InboundData) => void;
|
||||||
|
};
|
||||||
|
vote: {
|
||||||
|
outbound: (key: string) => {type: string; key: string; vote: number};
|
||||||
|
inbound: (sender_id: number, data: InboundData) => void;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const inbound_option_schema = z.object({
|
||||||
|
idx: z.number(),
|
||||||
|
option: z.string(),
|
||||||
|
type: z.literal("new_option"),
|
||||||
|
});
|
||||||
|
|
||||||
|
const inbound_question_schema = z.object({
|
||||||
|
question: z.string(),
|
||||||
|
type: z.literal("question"),
|
||||||
|
});
|
||||||
|
|
||||||
|
const inbound_vote_schema = z.object({
|
||||||
|
key: z.string(),
|
||||||
|
type: z.literal("vote"),
|
||||||
|
vote: z.number(),
|
||||||
|
});
|
||||||
|
|
||||||
// Any single user should send add a finite number of options
|
// Any single user should send add a finite number of options
|
||||||
// to a poll. We arbitrarily pick this value.
|
// to a poll. We arbitrarily pick this value.
|
||||||
const MAX_IDX = 1000;
|
const MAX_IDX = 1000;
|
||||||
|
@ -8,8 +79,16 @@ export class PollData {
|
||||||
// should be represented for rendering, plus how the
|
// should be represented for rendering, plus how the
|
||||||
// server sends us data.
|
// server sends us data.
|
||||||
|
|
||||||
key_to_option = new Map();
|
key_to_option = new Map<string, PollOption>();
|
||||||
my_idx = 1;
|
my_idx = 1;
|
||||||
|
message_sender_id: number;
|
||||||
|
me: number;
|
||||||
|
is_my_poll: boolean;
|
||||||
|
poll_question: string;
|
||||||
|
input_mode: boolean;
|
||||||
|
comma_separated_names: (user_ids: number[]) => string;
|
||||||
|
report_error_function: (error_message: string) => void;
|
||||||
|
handle: PollHandle;
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
message_sender_id,
|
message_sender_id,
|
||||||
|
@ -19,7 +98,7 @@ export class PollData {
|
||||||
options,
|
options,
|
||||||
comma_separated_names,
|
comma_separated_names,
|
||||||
report_error_function,
|
report_error_function,
|
||||||
}) {
|
}: PollDataConfig) {
|
||||||
this.message_sender_id = message_sender_id;
|
this.message_sender_id = message_sender_id;
|
||||||
this.me = current_user_id;
|
this.me = current_user_id;
|
||||||
this.is_my_poll = is_my_poll;
|
this.is_my_poll = is_my_poll;
|
||||||
|
@ -28,6 +107,10 @@ export class PollData {
|
||||||
this.comma_separated_names = comma_separated_names;
|
this.comma_separated_names = comma_separated_names;
|
||||||
this.report_error_function = report_error_function;
|
this.report_error_function = report_error_function;
|
||||||
|
|
||||||
|
if (question) {
|
||||||
|
this.set_question(question);
|
||||||
|
}
|
||||||
|
|
||||||
this.handle = {
|
this.handle = {
|
||||||
new_option: {
|
new_option: {
|
||||||
outbound: (option) => {
|
outbound: (option) => {
|
||||||
|
@ -43,9 +126,11 @@ export class PollData {
|
||||||
},
|
},
|
||||||
|
|
||||||
inbound: (sender_id, data) => {
|
inbound: (sender_id, data) => {
|
||||||
|
const safe_data = inbound_option_schema.parse(data);
|
||||||
|
|
||||||
// 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 = safe_data.idx;
|
||||||
const option = data.option;
|
const option = safe_data.option;
|
||||||
const options = this.get_widget_data().options;
|
const options = this.get_widget_data().options;
|
||||||
|
|
||||||
// While the UI doesn't allow adding duplicate options
|
// While the UI doesn't allow adding duplicate options
|
||||||
|
@ -55,17 +140,12 @@ export class PollData {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Number.isInteger(idx) || idx < 0 || idx > MAX_IDX) {
|
if (idx < 0 || idx > MAX_IDX) {
|
||||||
this.report_error_function("poll widget: bad type for inbound option idx");
|
this.report_error_function("poll widget: idx out of bound");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof option !== "string") {
|
const key = `${sender_id},${idx}`;
|
||||||
this.report_error_function("poll widget: bad type for inbound option");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const key = sender_id + "," + idx;
|
|
||||||
const votes = new Map();
|
const votes = new Map();
|
||||||
|
|
||||||
this.key_to_option.set(key, {
|
this.key_to_option.set(key, {
|
||||||
|
@ -94,6 +174,8 @@ export class PollData {
|
||||||
},
|
},
|
||||||
|
|
||||||
inbound: (sender_id, data) => {
|
inbound: (sender_id, data) => {
|
||||||
|
const safe_data = inbound_question_schema.parse(data);
|
||||||
|
|
||||||
// Only the message author can edit questions.
|
// Only the message author can edit questions.
|
||||||
if (sender_id !== this.message_sender_id) {
|
if (sender_id !== this.message_sender_id) {
|
||||||
this.report_error_function(
|
this.report_error_function(
|
||||||
|
@ -102,12 +184,7 @@ export class PollData {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof data.question !== "string") {
|
this.set_question(safe_data.question);
|
||||||
this.report_error_function("poll widget: bad type for inbound question");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.set_question(data.question);
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -116,7 +193,8 @@ export class PollData {
|
||||||
let vote = 1;
|
let vote = 1;
|
||||||
|
|
||||||
// toggle
|
// toggle
|
||||||
if (this.key_to_option.get(key).votes.get(this.me)) {
|
assert(this.key_to_option.has(key), `option key not found: ${key}`);
|
||||||
|
if (this.key_to_option.get(key)!.votes.get(this.me)) {
|
||||||
vote = -1;
|
vote = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,16 +208,13 @@ export class PollData {
|
||||||
},
|
},
|
||||||
|
|
||||||
inbound: (sender_id, data) => {
|
inbound: (sender_id, data) => {
|
||||||
|
const safe_data = inbound_vote_schema.parse(data);
|
||||||
|
|
||||||
// All message readers may vote on poll options.
|
// All message readers may vote on poll options.
|
||||||
const key = data.key;
|
const key = safe_data.key;
|
||||||
const vote = data.vote;
|
const vote = safe_data.vote;
|
||||||
|
|
||||||
if (typeof key !== "string") {
|
if (!(vote === 1 || vote === -1)) {
|
||||||
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");
|
this.report_error_function("poll widget: bad value for inbound vote count");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -162,41 +237,38 @@ export class PollData {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
if (question) {
|
|
||||||
this.set_question(question);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const [i, option] of options.entries()) {
|
for (const [i, option] of options.entries()) {
|
||||||
this.handle.new_option.inbound("canned", {
|
this.handle.new_option.inbound(-1, {
|
||||||
idx: i,
|
idx: i,
|
||||||
option,
|
option,
|
||||||
|
type: "new_option",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
set_question(new_question) {
|
set_question(new_question: string): void {
|
||||||
this.input_mode = false;
|
this.input_mode = false;
|
||||||
this.poll_question = new_question;
|
this.poll_question = new_question;
|
||||||
}
|
}
|
||||||
|
|
||||||
get_question() {
|
get_question(): string {
|
||||||
return this.poll_question;
|
return this.poll_question;
|
||||||
}
|
}
|
||||||
|
|
||||||
set_input_mode() {
|
set_input_mode(): void {
|
||||||
this.input_mode = true;
|
this.input_mode = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
clear_input_mode() {
|
clear_input_mode(): void {
|
||||||
this.input_mode = false;
|
this.input_mode = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
get_input_mode() {
|
get_input_mode(): boolean {
|
||||||
return this.input_mode;
|
return this.input_mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
get_widget_data() {
|
get_widget_data(): WidgetData {
|
||||||
const options = [];
|
const options: PollOptionData[] = [];
|
||||||
|
|
||||||
for (const [key, obj] of this.key_to_option) {
|
for (const [key, obj] of this.key_to_option) {
|
||||||
const voters = [...obj.votes.keys()];
|
const voters = [...obj.votes.keys()];
|
||||||
|
@ -219,9 +291,9 @@ export class PollData {
|
||||||
return widget_data;
|
return widget_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
handle_event(sender_id, data) {
|
handle_event(sender_id: number, data: InboundData): void {
|
||||||
const type = data.type;
|
const type = data.type;
|
||||||
if (this.handle[type] && this.handle[type].inbound) {
|
if (this.handle[type]) {
|
||||||
this.handle[type].inbound(sender_id, data);
|
this.handle[type].inbound(sender_id, data);
|
||||||
} else {
|
} else {
|
||||||
this.report_error_function(`poll widget: unknown inbound type: ${type}`);
|
this.report_error_function(`poll widget: unknown inbound type: ${type}`);
|
||||||
|
@ -229,7 +301,7 @@ export class PollData {
|
||||||
}
|
}
|
||||||
|
|
||||||
// function to check whether option already exists
|
// function to check whether option already exists
|
||||||
is_option_present(data, latest_option) {
|
is_option_present(data: PollOptionData[], latest_option: string): boolean {
|
||||||
return data.some((el) => el.option === latest_option);
|
return data.some((el) => el.option === latest_option);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue