diff --git a/tools/test-js-with-node b/tools/test-js-with-node index f7eee13b8a..c6a007c921 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -185,7 +185,7 @@ EXEMPT_FILES = make_set( "web/src/resize.js", "web/src/resize_handler.js", "web/src/rows.ts", - "web/src/scheduled_messages.js", + "web/src/scheduled_messages.ts", "web/src/scheduled_messages_feed_ui.js", "web/src/scheduled_messages_overlay_ui.js", "web/src/scheduled_messages_popover.js", diff --git a/web/src/scheduled_messages.js b/web/src/scheduled_messages.ts similarity index 73% rename from web/src/scheduled_messages.js rename to web/src/scheduled_messages.ts index d2d64ef456..31ae225db2 100644 --- a/web/src/scheduled_messages.js +++ b/web/src/scheduled_messages.ts @@ -2,15 +2,35 @@ import * as channel from "./channel"; import {$t} from "./i18n"; import * as timerender from "./timerender"; +type ScheduledMessage = { + scheduled_message_id: number; + type: string; + to: number[] | number; + topic: string; + content: string; + rendered_content: string; + scheduled_delivery_timestamp: number; + failed: boolean; +}; + +type TimeKey = + | "today_nine_am" + | "today_four_pm" + | "tomorrow_nine_am" + | "tomorrow_four_pm" + | "monday_nine_am"; + +type SendOption = {[key in TimeKey]?: {text: string; stamp: number}}; + export const MINIMUM_SCHEDULED_MESSAGE_DELAY_SECONDS = 5 * 60; -// scheduled_messages_data is a dictionary where key=scheduled_message_id and value=scheduled_messages -export const scheduled_messages_data = new Map(); +// scheduled_messages_data is a map where key=scheduled_message_id and value=scheduled_messages +export const scheduled_messages_data = new Map(); -let selected_send_later_timestamp; +let selected_send_later_timestamp: number | undefined; -function compute_send_times(now = new Date()) { - const send_times = {}; +function compute_send_times(now = new Date()): Record { + const send_times: Record = {}; const today = new Date(now); const tomorrow = new Date(new Date(now).setDate(now.getDate() + 1)); @@ -33,19 +53,19 @@ function compute_send_times(now = new Date()) { return send_times; } -export function add_scheduled_messages(scheduled_messages) { +export function add_scheduled_messages(scheduled_messages: ScheduledMessage[]): void { for (const scheduled_message of scheduled_messages) { scheduled_messages_data.set(scheduled_message.scheduled_message_id, scheduled_message); } } -export function remove_scheduled_message(scheduled_message_id) { +export function remove_scheduled_message(scheduled_message_id: number): void { if (scheduled_messages_data.has(scheduled_message_id)) { scheduled_messages_data.delete(scheduled_message_id); } } -export function update_scheduled_message(scheduled_message) { +export function update_scheduled_message(scheduled_message: ScheduledMessage): void { if (!scheduled_messages_data.has(scheduled_message.scheduled_message_id)) { return; } @@ -53,18 +73,23 @@ export function update_scheduled_message(scheduled_message) { scheduled_messages_data.set(scheduled_message.scheduled_message_id, scheduled_message); } -export function delete_scheduled_message(scheduled_msg_id, success = () => {}) { - channel.del({ +export function delete_scheduled_message(scheduled_msg_id: number, success?: () => void): void { + void channel.del({ url: "/json/scheduled_messages/" + scheduled_msg_id, success, }); } -export function get_count() { +export function get_count(): number { return scheduled_messages_data.size; } -export function get_filtered_send_opts(date) { +export function get_filtered_send_opts(date: Date): { + possible_send_later_today: SendOption | false; + send_later_tomorrow: SendOption; + possible_send_later_monday: SendOption | false; + send_later_custom: {text: string}; +} { const send_times = compute_send_times(date); const day = date.getDay(); // Starts with 0 for Sunday. @@ -142,8 +167,8 @@ export function get_filtered_send_opts(date) { text: $t({defaultMessage: "Custom"}), }; - let possible_send_later_today = {}; - let possible_send_later_monday = {}; + let possible_send_later_today: SendOption | false = {}; + let possible_send_later_monday: SendOption | false = {}; const minutes_into_day = date.getHours() * 60 + date.getMinutes(); // Show Today send options based on time of day @@ -171,28 +196,30 @@ export function get_filtered_send_opts(date) { }; } -export function get_selected_send_later_timestamp() { +export function get_selected_send_later_timestamp(): number | undefined { if (!selected_send_later_timestamp) { return undefined; } return selected_send_later_timestamp; } -export function get_formatted_selected_send_later_time() { +export function get_formatted_selected_send_later_time(): string | undefined { if (!selected_send_later_timestamp) { return undefined; } return timerender.get_full_datetime(new Date(selected_send_later_timestamp * 1000), "time"); } -export function set_selected_schedule_timestamp(timestamp) { +export function set_selected_schedule_timestamp(timestamp: number): void { selected_send_later_timestamp = timestamp; } -export function reset_selected_schedule_timestamp() { +export function reset_selected_schedule_timestamp(): void { selected_send_later_timestamp = undefined; } -export function initialize(scheduled_messages_params) { +export function initialize(scheduled_messages_params: { + scheduled_messages: ScheduledMessage[]; +}): void { add_scheduled_messages(scheduled_messages_params.scheduled_messages); }