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";
|
2023-05-05 17:12:30 +02:00
|
|
|
import * as timerender from "./timerender";
|
2023-04-14 21:35:56 +02:00
|
|
|
|
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
|
|
|
|
2023-06-23 16:13:28 +02:00
|
|
|
// scheduled_messages_data is a dictionary where key=scheduled_message_id and value=scheduled_messages
|
|
|
|
export const scheduled_messages_data = {};
|
2023-04-14 21:35:56 +02:00
|
|
|
|
2023-10-04 00:18:21 +02:00
|
|
|
let selected_send_later_timestamp;
|
|
|
|
|
2023-05-05 17:12:30 +02:00
|
|
|
function compute_send_times(now = new Date()) {
|
|
|
|
const send_times = {};
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-05-01 14:29:57 +02:00
|
|
|
export function add_scheduled_messages(scheduled_messages) {
|
2023-06-23 16:13:28 +02:00
|
|
|
for (const scheduled_message of scheduled_messages) {
|
|
|
|
scheduled_messages_data[scheduled_message.scheduled_message_id] = scheduled_message;
|
|
|
|
}
|
2023-05-01 14:29:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function remove_scheduled_message(scheduled_message_id) {
|
2023-06-23 16:13:28 +02:00
|
|
|
if (scheduled_messages_data[scheduled_message_id] !== undefined) {
|
|
|
|
delete scheduled_messages_data[scheduled_message_id];
|
2023-05-01 14:29:57 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-14 21:35:56 +02:00
|
|
|
|
2023-05-01 14:29:57 +02:00
|
|
|
export function update_scheduled_message(scheduled_message) {
|
2023-06-23 16:13:28 +02:00
|
|
|
if (scheduled_messages_data[scheduled_message.scheduled_message_id] === undefined) {
|
2023-05-01 14:29:57 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-04-14 21:35:56 +02:00
|
|
|
|
2023-06-23 16:13:28 +02:00
|
|
|
scheduled_messages_data[scheduled_message.scheduled_message_id] = scheduled_message;
|
2023-05-01 14:29:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function delete_scheduled_message(scheduled_msg_id, success = () => {}) {
|
2023-04-14 21:35:56 +02:00
|
|
|
channel.del({
|
|
|
|
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
|
|
|
|
2023-05-02 21:08:15 +02:00
|
|
|
export function get_count() {
|
2023-06-23 16:13:28 +02:00
|
|
|
return Object.keys(scheduled_messages_data).length;
|
2023-05-02 21:08:15 +02:00
|
|
|
}
|
|
|
|
|
2023-05-04 16:00:27 +02:00
|
|
|
export function get_filtered_send_opts(date) {
|
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"}),
|
|
|
|
};
|
|
|
|
|
2023-05-04 16:00:27 +02:00
|
|
|
let possible_send_later_today = {};
|
|
|
|
let possible_send_later_monday = {};
|
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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-10-04 00:18:21 +02:00
|
|
|
export function get_selected_send_later_timestamp() {
|
|
|
|
if (!selected_send_later_timestamp) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return selected_send_later_timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function get_formatted_selected_send_later_time() {
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function set_selected_schedule_timestamp(timestamp) {
|
|
|
|
selected_send_later_timestamp = timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function reset_selected_schedule_timestamp() {
|
|
|
|
selected_send_later_timestamp = undefined;
|
|
|
|
}
|
|
|
|
|
2023-05-02 08:31:48 +02:00
|
|
|
export function initialize(scheduled_messages_params) {
|
2023-06-23 16:13:28 +02:00
|
|
|
add_scheduled_messages(scheduled_messages_params.scheduled_messages);
|
2023-05-01 14:29:57 +02:00
|
|
|
}
|