scheduled_messages: Rerender send later options at specific time.

Fixes: #25451.
This commit is contained in:
Daniil Fadeev
2023-05-10 16:10:47 +04:00
committed by Tim Abbott
parent 45c3f8aa96
commit e10997b0e6
3 changed files with 63 additions and 0 deletions

View File

@@ -835,6 +835,11 @@ export function initialize() {
overlays.open_modal("send_later_modal", { overlays.open_modal("send_later_modal", {
autoremove: true, autoremove: true,
on_show() { 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"); const $send_later_modal = $("#send_later_modal");
$send_later_modal.on("keydown", (e) => { $send_later_modal.on("keydown", (e) => {
if (e.key === "Enter") { if (e.key === "Enter") {
@@ -877,6 +882,9 @@ export function initialize() {
}, },
); );
}, },
on_hide() {
clearInterval(instance._interval);
},
}); });
}); });
}, },

View File

@@ -1,6 +1,7 @@
import $ from "jquery"; import $ from "jquery";
import render_success_message_scheduled_banner from "../templates/compose_banner/success_message_scheduled_banner.hbs"; 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 channel from "./channel";
import * as compose from "./compose"; import * as compose from "./compose";
@@ -16,6 +17,8 @@ import * as stream_data from "./stream_data";
import * as timerender from "./timerender"; import * as timerender from "./timerender";
export const MINIMUM_SCHEDULED_MESSAGE_DELAY_SECONDS = 5 * 60; export const MINIMUM_SCHEDULED_MESSAGE_DELAY_SECONDS = 5 * 60;
export const SCHEDULING_MODAL_UPDATE_INTERVAL_IN_MILLISECONDS = 60 * 1000;
export let scheduled_messages_data = []; export let scheduled_messages_data = [];
function compute_send_times(now = new Date()) { function compute_send_times(now = new Date()) {
@@ -322,3 +325,29 @@ export function initialize(scheduled_messages_params) {
e.stopPropagation(); 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),
);
}
}

View File

@@ -104,6 +104,13 @@ function get_expected_send_opts(day, expecteds) {
return modal_opts; 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", () => { run_test("scheduled_modal_opts", () => {
// Sunday thru Saturday // Sunday thru Saturday
const days = [ 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));
});