From 5baa0dc31384965576a0ddb8a48f8bf38b9d3c25 Mon Sep 17 00:00:00 2001 From: Aman Agrawal Date: Thu, 20 Apr 2023 02:34:08 +0000 Subject: [PATCH] compose: Use the new endpoint to create scheduled messages. --- web/src/compose.js | 39 ++++++++++++++++++------ web/src/popover_menus.js | 9 +++++- web/src/scheduled_messages.js | 33 ++++++++++++++++++++ web/src/scheduled_messages_overlay_ui.js | 2 +- web/src/tippyjs.js | 2 +- 5 files changed, 73 insertions(+), 12 deletions(-) diff --git a/web/src/compose.js b/web/src/compose.js index 486557ff61..c1a166446f 100644 --- a/web/src/compose.js +++ b/web/src/compose.js @@ -804,14 +804,35 @@ export function reset_compose_scheduling_state(reset_edit_state = true) { } function schedule_message_to_custom_date() { - const request = create_message_object(); - const selected_send_later_time = popover_menus.get_selected_send_later_time(); - request.content = `/schedule ${selected_send_later_time}\n` + request.content; - // If this is an edit request `scheduled_message_id` will be defined. - let scheduled_message_id; - if ($("#compose-textarea").attr("data-scheduled-message-id")) { - scheduled_message_id = $("#compose-textarea").attr("data-scheduled-message-id"); - $("#compose-textarea").removeAttr("data-scheduled-message-id"); + const compose_message_object = create_message_object(); + + const deliver_at = popover_menus.get_formatted_selected_send_later_time(); + const send_later_time = popover_menus.get_selected_send_later_time(); + const scheduled_delivery_timestamp = Math.floor(Date.parse(send_later_time) / 1000); + + const message_type = compose_message_object.type; + let req_type; + + if (message_type === "private") { + req_type = "direct"; + } else { + req_type = message_type; } - reminder.schedule_message(request, clear_compose_box, scheduled_message_id); + + const scheduled_message_data = { + type: req_type, + to: JSON.stringify(compose_message_object.to), + topic: compose_message_object.topic, + content: compose_message_object.content, + scheduled_delivery_timestamp, + }; + + // If this is an edit request `scheduled_message_id` will be defined. + if ($("#compose-textarea").attr("data-scheduled-message-id")) { + scheduled_message_data.scheduled_message_id = $("#compose-textarea").attr( + "data-scheduled-message-id", + ); + } + + scheduled_messages.send_request_to_schedule_message(scheduled_message_data, deliver_at); } diff --git a/web/src/popover_menus.js b/web/src/popover_menus.js index 1c5386740c..68ca6bc47a 100644 --- a/web/src/popover_menus.js +++ b/web/src/popover_menus.js @@ -82,6 +82,13 @@ export function is_time_selected_for_schedule() { } export function get_selected_send_later_time() { + if (!selected_send_later_time) { + return undefined; + } + return selected_send_later_time; +} + +export function get_formatted_selected_send_later_time() { if (!selected_send_later_time) { return undefined; } @@ -863,7 +870,7 @@ export function initialize() { possible_send_later_today = false; } - const formatted_send_later_time = get_selected_send_later_time(); + const formatted_send_later_time = get_formatted_selected_send_later_time(); instance.setContent( parse_html( diff --git a/web/src/scheduled_messages.js b/web/src/scheduled_messages.js index f4ba532bed..05293cb99f 100644 --- a/web/src/scheduled_messages.js +++ b/web/src/scheduled_messages.js @@ -3,9 +3,12 @@ import $ from "jquery"; import * as channel from "./channel"; import * as compose from "./compose"; import * as compose_actions from "./compose_actions"; +import * as compose_banner from "./compose_banner"; import * as compose_fade from "./compose_fade"; import * as compose_ui from "./compose_ui"; +import {$t} from "./i18n"; import * as narrow from "./narrow"; +import * as notifications from "./notifications"; import * as overlays from "./overlays"; import * as people from "./people"; import * as popover_menus from "./popover_menus"; @@ -68,6 +71,36 @@ export function edit_scheduled_message(scheduled_msg_id) { popover_menus.show_schedule_confirm_button(scheduled_msg.formatted_send_at_time, true); } +export function send_request_to_schedule_message(scheduled_message_data, deliver_at) { + const success = function () { + compose.clear_compose_box(); + notifications.notify_above_composebox( + $t({defaultMessage: `Your message has been scheduled for {deliver_at}.`}, {deliver_at}), + "scheduled_message_banner", + "/#scheduled", + "", + $t({defaultMessage: "View scheduled messages"}), + ); + }; + + const error = function (xhr) { + const response = channel.xhr_error_message("Error sending message", xhr); + compose_ui.hide_compose_spinner(); + compose_banner.show_error_message( + response, + compose_banner.CLASSNAMES.generic_compose_error, + $("#compose-textarea"), + ); + }; + + channel.post({ + url: "/json/scheduled_messages", + data: scheduled_message_data, + success, + error, + }); +} + export function delete_scheduled_message(scheduled_msg_id) { channel.del({ url: "/json/scheduled_messages/" + scheduled_msg_id, diff --git a/web/src/scheduled_messages_overlay_ui.js b/web/src/scheduled_messages_overlay_ui.js index ccb386f841..1f666bcddc 100644 --- a/web/src/scheduled_messages_overlay_ui.js +++ b/web/src/scheduled_messages_overlay_ui.js @@ -38,7 +38,7 @@ function format(scheduled_messages) { msg_render_context.is_stream = false; msg_render_context.recipients = people.get_recipients(msg.to.join(",")); } - const time = new Date(msg.deliver_at); + const time = new Date(msg.scheduled_delivery_timestamp * 1000); msg_render_context.full_date_time = timerender.get_full_datetime(time); msg_render_context.formatted_send_at_time = date_fns.format(time, "MMM d yyyy h:mm a"); formatted_msgs.push(msg_render_context); diff --git a/web/src/tippyjs.js b/web/src/tippyjs.js index 1facc181b7..831a6215ec 100644 --- a/web/src/tippyjs.js +++ b/web/src/tippyjs.js @@ -654,7 +654,7 @@ export function initialize() { return false; } - const send_at_time = popover_menus.get_selected_send_later_time(); + const send_at_time = popover_menus.get_formatted_selected_send_later_time(); instance.setContent( parse_html( $t(