2024-06-21 09:40:15 +02:00
|
|
|
import type {z} from "zod";
|
|
|
|
|
2023-04-14 21:35:56 +02:00
|
|
|
import * as channel from "./channel";
|
2023-05-04 16:00:27 +02:00
|
|
|
import {$t} from "./i18n";
|
2024-06-21 09:40:15 +02:00
|
|
|
import type {StateData, scheduled_message_schema} from "./state_data";
|
2023-05-05 17:12:30 +02:00
|
|
|
import * as timerender from "./timerender";
|
2023-04-14 21:35:56 +02:00
|
|
|
|
2024-06-21 09:40:15 +02:00
|
|
|
export type ScheduledMessage = z.infer<typeof scheduled_message_schema>;
|
2024-01-10 17:13:59 +01:00
|
|
|
|
|
|
|
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}};
|
|
|
|
|
2023-05-08 17:55:34 +02:00
|
|
|
export const MINIMUM_SCHEDULED_MESSAGE_DELAY_SECONDS = 5 * 60;
|
2023-05-10 14:10:47 +02:00
|
|
|
|
2024-01-10 17:13:59 +01:00
|
|
|
// scheduled_messages_data is a map where key=scheduled_message_id and value=scheduled_messages
|
|
|
|
export const scheduled_messages_data = new Map<number, ScheduledMessage>();
|
2023-04-14 21:35:56 +02:00
|
|
|
|
2024-01-10 17:13:59 +01:00
|
|
|
let selected_send_later_timestamp: number | undefined;
|
2023-10-04 00:18:21 +02:00
|
|
|
|
2024-01-10 17:13:59 +01:00
|
|
|
function compute_send_times(now = new Date()): Record<TimeKey, number> {
|
|
|
|
const send_times: Record<string, number> = {};
|
2023-05-05 17:12:30 +02:00
|
|
|
|
|
|
|
const today = new Date(now);
|
|
|
|
const tomorrow = new Date(new Date(now).setDate(now.getDate() + 1));
|
|
|
|
// Find the next Monday by subtracting the current day (0-6) from 8
|
|
|
|
const monday = new Date(new Date(now).setDate(now.getDate() + 8 - now.getDay()));
|
|
|
|
|
|
|
|
// Since setHours returns a timestamp, it's safe to mutate the
|
|
|
|
// original date objects here.
|
|
|
|
//
|
|
|
|
// today at 9am
|
|
|
|
send_times.today_nine_am = today.setHours(9, 0, 0, 0);
|
|
|
|
// today at 4pm
|
|
|
|
send_times.today_four_pm = today.setHours(16, 0, 0, 0);
|
|
|
|
// tomorrow at 9am
|
|
|
|
send_times.tomorrow_nine_am = tomorrow.setHours(9, 0, 0, 0);
|
|
|
|
// tomorrow at 4pm
|
|
|
|
send_times.tomorrow_four_pm = tomorrow.setHours(16, 0, 0, 0);
|
|
|
|
// next Monday at 9am
|
|
|
|
send_times.monday_nine_am = monday.setHours(9, 0, 0, 0);
|
|
|
|
return send_times;
|
|
|
|
}
|
2023-05-04 16:00:27 +02:00
|
|
|
|
2024-01-10 17:13:59 +01:00
|
|
|
export function add_scheduled_messages(scheduled_messages: ScheduledMessage[]): void {
|
2023-06-23 16:13:28 +02:00
|
|
|
for (const scheduled_message of scheduled_messages) {
|
2024-01-10 14:54:15 +01:00
|
|
|
scheduled_messages_data.set(scheduled_message.scheduled_message_id, scheduled_message);
|
2023-06-23 16:13:28 +02:00
|
|
|
}
|
2023-05-01 14:29:57 +02:00
|
|
|
}
|
|
|
|
|
2024-01-10 17:13:59 +01:00
|
|
|
export function remove_scheduled_message(scheduled_message_id: number): void {
|
2024-01-10 14:54:15 +01:00
|
|
|
if (scheduled_messages_data.has(scheduled_message_id)) {
|
|
|
|
scheduled_messages_data.delete(scheduled_message_id);
|
2023-05-01 14:29:57 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-14 21:35:56 +02:00
|
|
|
|
2024-01-10 17:13:59 +01:00
|
|
|
export function update_scheduled_message(scheduled_message: ScheduledMessage): void {
|
2024-01-10 14:54:15 +01:00
|
|
|
if (!scheduled_messages_data.has(scheduled_message.scheduled_message_id)) {
|
2023-05-01 14:29:57 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-04-14 21:35:56 +02:00
|
|
|
|
2024-01-10 14:54:15 +01:00
|
|
|
scheduled_messages_data.set(scheduled_message.scheduled_message_id, scheduled_message);
|
2023-05-01 14:29:57 +02:00
|
|
|
}
|
|
|
|
|
2024-01-10 17:13:59 +01:00
|
|
|
export function delete_scheduled_message(scheduled_msg_id: number, success?: () => void): void {
|
|
|
|
void channel.del({
|
2023-04-14 21:35:56 +02:00
|
|
|
url: "/json/scheduled_messages/" + scheduled_msg_id,
|
2023-05-01 14:29:57 +02:00
|
|
|
success,
|
2023-04-14 21:35:56 +02:00
|
|
|
});
|
|
|
|
}
|
2023-05-01 14:29:57 +02:00
|
|
|
|
2024-01-10 17:13:59 +01:00
|
|
|
export function get_count(): number {
|
2024-01-10 14:54:15 +01:00
|
|
|
return scheduled_messages_data.size;
|
2023-05-02 21:08:15 +02:00
|
|
|
}
|
|
|
|
|
2024-01-10 17:13:59 +01:00
|
|
|
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};
|
|
|
|
} {
|
2023-05-07 18:32:31 +02:00
|
|
|
const send_times = compute_send_times(date);
|
2023-05-05 17:12:30 +02:00
|
|
|
|
2023-05-04 16:00:27 +02:00
|
|
|
const day = date.getDay(); // Starts with 0 for Sunday.
|
2023-05-05 17:12:30 +02:00
|
|
|
|
|
|
|
const send_later_today = {
|
|
|
|
today_nine_am: {
|
|
|
|
text: $t(
|
|
|
|
{defaultMessage: "Today at {time}"},
|
|
|
|
{
|
|
|
|
time: timerender.get_localized_date_or_time_for_format(
|
|
|
|
send_times.today_nine_am,
|
|
|
|
"time",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
stamp: send_times.today_nine_am,
|
|
|
|
},
|
|
|
|
today_four_pm: {
|
|
|
|
text: $t(
|
|
|
|
{defaultMessage: "Today at {time}"},
|
|
|
|
{
|
|
|
|
time: timerender.get_localized_date_or_time_for_format(
|
|
|
|
send_times.today_four_pm,
|
|
|
|
"time",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
stamp: send_times.today_four_pm,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const send_later_tomorrow = {
|
|
|
|
tomorrow_nine_am: {
|
|
|
|
text: $t(
|
|
|
|
{defaultMessage: "Tomorrow at {time}"},
|
|
|
|
{
|
|
|
|
time: timerender.get_localized_date_or_time_for_format(
|
|
|
|
send_times.tomorrow_nine_am,
|
|
|
|
"time",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
stamp: send_times.tomorrow_nine_am,
|
|
|
|
},
|
|
|
|
tomorrow_four_pm: {
|
|
|
|
text: $t(
|
|
|
|
{defaultMessage: "Tomorrow at {time}"},
|
|
|
|
{
|
|
|
|
time: timerender.get_localized_date_or_time_for_format(
|
|
|
|
send_times.tomorrow_four_pm,
|
|
|
|
"time",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
stamp: send_times.tomorrow_four_pm,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const send_later_monday = {
|
|
|
|
monday_nine_am: {
|
|
|
|
text: $t(
|
|
|
|
{defaultMessage: "Monday at {time}"},
|
|
|
|
{
|
|
|
|
time: timerender.get_localized_date_or_time_for_format(
|
|
|
|
send_times.monday_nine_am,
|
|
|
|
"time",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
stamp: send_times.monday_nine_am,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const send_later_custom = {
|
|
|
|
text: $t({defaultMessage: "Custom"}),
|
|
|
|
};
|
|
|
|
|
2024-01-10 17:13:59 +01:00
|
|
|
let possible_send_later_today: SendOption | false = {};
|
|
|
|
let possible_send_later_monday: SendOption | false = {};
|
2023-05-08 16:17:58 +02:00
|
|
|
|
|
|
|
const minutes_into_day = date.getHours() * 60 + date.getMinutes();
|
|
|
|
// Show Today send options based on time of day
|
|
|
|
if (minutes_into_day < 9 * 60 - MINIMUM_SCHEDULED_MESSAGE_DELAY_SECONDS / 60) {
|
|
|
|
// Allow Today at 9:00am only up to minimum scheduled message delay
|
2023-05-04 16:00:27 +02:00
|
|
|
possible_send_later_today = send_later_today;
|
2023-05-08 23:39:47 +02:00
|
|
|
} else if (minutes_into_day < (12 + 4) * 60 - MINIMUM_SCHEDULED_MESSAGE_DELAY_SECONDS / 60) {
|
2023-05-08 16:17:58 +02:00
|
|
|
// Allow Today at 4:00pm only up to minimum scheduled message delay
|
2023-05-04 16:00:27 +02:00
|
|
|
possible_send_later_today.today_four_pm = send_later_today.today_four_pm;
|
|
|
|
} else {
|
|
|
|
possible_send_later_today = false;
|
|
|
|
}
|
|
|
|
// Show send_later_monday options only on Fridays and Saturdays.
|
|
|
|
if (day >= 5) {
|
|
|
|
possible_send_later_monday = send_later_monday;
|
|
|
|
} else {
|
|
|
|
possible_send_later_monday = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
possible_send_later_today,
|
|
|
|
send_later_tomorrow,
|
|
|
|
possible_send_later_monday,
|
|
|
|
send_later_custom,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-01-10 17:13:59 +01:00
|
|
|
export function get_selected_send_later_timestamp(): number | undefined {
|
2023-10-04 00:18:21 +02:00
|
|
|
if (!selected_send_later_timestamp) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return selected_send_later_timestamp;
|
|
|
|
}
|
|
|
|
|
2024-01-10 17:13:59 +01:00
|
|
|
export function get_formatted_selected_send_later_time(): string | undefined {
|
2023-09-20 14:09:55 +02:00
|
|
|
if (!selected_send_later_timestamp) {
|
2023-10-04 00:18:21 +02:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return timerender.get_full_datetime(new Date(selected_send_later_timestamp * 1000), "time");
|
|
|
|
}
|
|
|
|
|
2024-01-10 17:13:59 +01:00
|
|
|
export function set_selected_schedule_timestamp(timestamp: number): void {
|
2023-10-04 00:18:21 +02:00
|
|
|
selected_send_later_timestamp = timestamp;
|
|
|
|
}
|
|
|
|
|
2024-01-10 17:13:59 +01:00
|
|
|
export function reset_selected_schedule_timestamp(): void {
|
2023-10-04 00:18:21 +02:00
|
|
|
selected_send_later_timestamp = undefined;
|
|
|
|
}
|
|
|
|
|
2024-06-21 09:40:15 +02:00
|
|
|
export function initialize(scheduled_messages_params: StateData["scheduled_messages"]): void {
|
2023-06-23 16:13:28 +02:00
|
|
|
add_scheduled_messages(scheduled_messages_params.scheduled_messages);
|
2023-05-01 14:29:57 +02:00
|
|
|
}
|