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.

scheduled_messages: Rerender send later options at specific time.

Daniil Fadeeve10997b0e623
diff --git a/web/src/popover_menus.js b/web/src/popover_menus.js
index 209623c838f..1bc23e516b0 100644
--- a/web/src/popover_menus.js
+++ b/web/src/popover_menus.js
@@ -835,6 +835,11 @@ export function initialize() {
overlays.open_modal("send_later_modal", {
autoremove: true,
on_show() {
+ instance._interval = setInterval(
+ scheduled_messages.update_send_later_options,
+ scheduled_messages.SCHEDULING_MODAL_UPDATE_INTERVAL_IN_MILLISECONDS,
+ );
+
const $send_later_modal = $("#send_later_modal");
$send_later_modal.on("keydown", (e) => {
if (e.key === "Enter") {
@@ -877,6 +882,9 @@ export function initialize() {
},
);
},
+ on_hide() {
+ clearInterval(instance._interval);
+ },
});
});
},
diff --git a/web/src/scheduled_messages.js b/web/src/scheduled_messages.js
index 7c03d4cd1cd..0f7ff3e8812 100644
--- a/web/src/scheduled_messages.js
+++ b/web/src/scheduled_messages.js
@@ -1,6 +1,7 @@
import $ from "jquery";
import render_success_message_scheduled_banner from "../templates/compose_banner/success_message_scheduled_banner.hbs";
+import render_send_later_modal_options from "../templates/send_later_modal_options.hbs";
import * as channel from "./channel";
import * as compose from "./compose";
@@ -16,6 +17,8 @@ import * as stream_data from "./stream_data";
import * as timerender from "./timerender";
export const MINIMUM_SCHEDULED_MESSAGE_DELAY_SECONDS = 5 * 60;
+export const SCHEDULING_MODAL_UPDATE_INTERVAL_IN_MILLISECONDS = 60 * 1000;
+
export let scheduled_messages_data = [];
function compute_send_times(now = new Date()) {
@@ -322,3 +325,29 @@ export function initialize(scheduled_messages_params) {
e.stopPropagation();
});
}
+
+// This function is exported for unit testing purposes.
+export function should_update_send_later_options(date) {
+ const current_minute = date.getMinutes();
+ const current_hour = date.getHours();
+
+ if (current_hour === 0 && current_minute === 0) {
+ // We need to rerender the available options at midnight,
+ // since Monday could become in range.
+ return true;
+ }
+
+ // Rerender at MINIMUM_SCHEDULED_MESSAGE_DELAY_SECONDS before the
+ // hour, so we don't offer a 4:00PM send time at 3:59 PM.
+ return current_minute === 60 - MINIMUM_SCHEDULED_MESSAGE_DELAY_SECONDS / 60;
+}
+
+export function update_send_later_options() {
+ const now = new Date();
+ if (should_update_send_later_options(now)) {
+ const filtered_send_opts = get_filtered_send_opts(now);
+ $("#send_later_modal .send_later_options").replaceWith(
+ render_send_later_modal_options(filtered_send_opts),
+ );
+ }
+}
diff --git a/web/tests/scheduled_messages.test.js b/web/tests/scheduled_messages.test.js
index efe26741dec..4fc6962c816 100644
--- a/web/tests/scheduled_messages.test.js
+++ b/web/tests/scheduled_messages.test.js
@@ -104,6 +104,13 @@ function get_expected_send_opts(day, expecteds) {
return modal_opts;
}
+function get_minutes_to_hour(minutes) {
+ const date = new Date();
+ date.setMinutes(minutes);
+ date.setSeconds(0);
+ return date;
+}
+
run_test("scheduled_modal_opts", () => {
// Sunday thru Saturday
const days = [
@@ -159,3 +166,22 @@ run_test("missing_or_expired_timestamps", () => {
),
);
});
+
+run_test("should_update_send_later_options", () => {
+ // We should rerender if it is 5 minutes before the hour
+ for (let minute = 0; minute < 60; minute += 1) {
+ const current_time = get_minutes_to_hour(minute);
+ if (minute === 55) {
+ // Should rerender
+ assert.ok(scheduled_messages.should_update_send_later_options(current_time));
+ } else {
+ // Should not rerender
+ assert.ok(!scheduled_messages.should_update_send_later_options(current_time));
+ }
+ }
+
+ // We should rerender at midnight
+ const start_of_the_day = new Date();
+ start_of_the_day.setHours(0, 0);
+ assert.ok(scheduled_messages.should_update_send_later_options(start_of_the_day));
+});