2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2021-04-10 02:26:13 +02:00
|
|
|
import _ from "lodash";
|
2021-03-11 05:43:45 +01:00
|
|
|
|
2021-02-28 01:16:59 +01:00
|
|
|
import * as channel from "./channel";
|
|
|
|
import * as compose from "./compose";
|
2021-06-24 19:30:01 +02:00
|
|
|
import * as compose_error from "./compose_error";
|
2021-02-28 01:16:59 +01:00
|
|
|
import * as hash_util from "./hash_util";
|
2021-04-13 06:51:54 +02:00
|
|
|
import {$t, $t_html} from "./i18n";
|
2021-03-30 02:21:21 +02:00
|
|
|
import * as message_lists from "./message_lists";
|
2021-02-28 01:16:59 +01:00
|
|
|
import * as notifications from "./notifications";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 01:16:59 +01:00
|
|
|
import * as people from "./people";
|
|
|
|
import * as transmit from "./transmit";
|
|
|
|
import * as util from "./util";
|
|
|
|
|
|
|
|
export const deferred_message_types = {
|
2018-02-06 22:54:53 +01:00
|
|
|
scheduled: {
|
2020-07-15 01:29:15 +02:00
|
|
|
delivery_type: "send_later",
|
2018-02-06 22:54:53 +01:00
|
|
|
test: /^\/schedule/,
|
2020-07-15 01:29:15 +02:00
|
|
|
slash_command: "/schedule",
|
2018-02-06 22:54:53 +01:00
|
|
|
},
|
|
|
|
reminders: {
|
2020-07-15 01:29:15 +02:00
|
|
|
delivery_type: "remind",
|
2018-02-06 22:54:53 +01:00
|
|
|
test: /^\/remind/,
|
2020-07-15 01:29:15 +02:00
|
|
|
slash_command: "/remind",
|
2018-02-06 22:54:53 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-02-28 01:16:59 +01:00
|
|
|
export function is_deferred_delivery(message_content) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const reminders_test = deferred_message_types.reminders.test;
|
|
|
|
const scheduled_test = deferred_message_types.scheduled.test;
|
2020-07-15 00:34:28 +02:00
|
|
|
return reminders_test.test(message_content) || scheduled_test.test(message_content);
|
2021-02-28 01:16:59 +01:00
|
|
|
}
|
2018-02-06 22:54:53 +01:00
|
|
|
|
2021-11-11 14:39:07 +01:00
|
|
|
export function patch_request_for_scheduling(request, message_content, deliver_at, delivery_type) {
|
2018-02-09 00:53:32 +01:00
|
|
|
if (request.type === "private") {
|
|
|
|
request.to = JSON.stringify(request.to);
|
|
|
|
} else {
|
|
|
|
request.to = JSON.stringify([request.to]);
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const new_request = request;
|
2018-02-09 00:53:32 +01:00
|
|
|
new_request.content = message_content;
|
|
|
|
new_request.deliver_at = deliver_at;
|
|
|
|
new_request.delivery_type = delivery_type;
|
2020-09-29 22:20:46 +02:00
|
|
|
new_request.tz_guess = new Intl.DateTimeFormat().resolvedOptions().timeZone;
|
2018-02-09 00:53:32 +01:00
|
|
|
return new_request;
|
|
|
|
}
|
|
|
|
|
2021-03-24 21:44:43 +01:00
|
|
|
export function schedule_message(request = compose.create_message_object()) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const raw_message = request.content.split("\n");
|
2019-11-02 00:06:25 +01:00
|
|
|
const command_line = raw_message[0];
|
2020-07-15 01:29:15 +02:00
|
|
|
const message = raw_message.slice(1).join("\n");
|
2018-02-06 22:54:53 +01:00
|
|
|
|
2021-11-10 14:54:26 +01:00
|
|
|
const deferred_message_type = Object.values(deferred_message_types).find(
|
2020-07-02 02:16:03 +02:00
|
|
|
(props) => command_line.match(props.test) !== null,
|
2020-10-07 11:24:24 +02:00
|
|
|
);
|
2019-11-02 00:06:25 +01:00
|
|
|
const command = command_line.match(deferred_message_type.test)[0];
|
2018-02-06 22:54:53 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const deliver_at = command_line.slice(command.length + 1);
|
2018-02-06 22:54:53 +01:00
|
|
|
|
2021-11-11 14:39:07 +01:00
|
|
|
let error_message;
|
|
|
|
if (command_line.slice(command.length, command.length + 1) !== " ") {
|
|
|
|
error_message = $t({
|
|
|
|
defaultMessage:
|
|
|
|
"Invalid slash command. Check if you are missing a space after the command.",
|
|
|
|
});
|
|
|
|
} else if (deliver_at.trim() === "") {
|
|
|
|
error_message = $t({defaultMessage: "Please specify a date or time."});
|
|
|
|
} else if (message.trim() === "") {
|
|
|
|
error_message = $t({defaultMessage: "You have nothing to send!"});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error_message) {
|
|
|
|
compose_error.show(error_message, $("#compose-textarea"));
|
2020-07-22 02:59:06 +02:00
|
|
|
$("#compose-textarea").prop("disabled", false);
|
2018-02-06 22:54:53 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-09 00:53:32 +01:00
|
|
|
request = patch_request_for_scheduling(
|
2020-07-15 00:34:28 +02:00
|
|
|
request,
|
|
|
|
message,
|
|
|
|
deliver_at,
|
|
|
|
deferred_message_type.delivery_type,
|
2018-02-09 00:53:32 +01:00
|
|
|
);
|
2018-02-06 22:54:53 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const success = function (data) {
|
2018-02-09 00:53:32 +01:00
|
|
|
if (request.delivery_type === deferred_message_types.scheduled.delivery_type) {
|
2021-11-11 14:39:07 +01:00
|
|
|
const deliver_at = data.deliver_at;
|
2020-07-15 00:34:28 +02:00
|
|
|
notifications.notify_above_composebox(
|
2021-11-11 14:39:07 +01:00
|
|
|
$t_html({defaultMessage: `Message scheduled for {deliver_at}`}, {deliver_at}),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2018-02-09 00:53:32 +01:00
|
|
|
}
|
2020-07-22 02:59:06 +02:00
|
|
|
$("#compose-textarea").prop("disabled", false);
|
2018-02-06 22:54:53 +01:00
|
|
|
compose.clear_compose_box();
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const error = function (response) {
|
2020-07-22 02:59:06 +02:00
|
|
|
$("#compose-textarea").prop("disabled", false);
|
2021-06-24 19:30:01 +02:00
|
|
|
compose_error.show(_.escape(response), $("#compose-textarea"));
|
2018-02-06 22:54:53 +01:00
|
|
|
};
|
|
|
|
/* We are adding a disable on compose under this block because we
|
|
|
|
want slash commands to be blocking in nature. */
|
2020-07-22 02:59:06 +02:00
|
|
|
$("#compose-textarea").prop("disabled", true);
|
2018-02-06 22:54:53 +01:00
|
|
|
|
2021-11-10 15:21:36 +01:00
|
|
|
const future_message = true;
|
|
|
|
transmit.send_message(request, success, error, future_message);
|
2021-02-28 01:16:59 +01:00
|
|
|
}
|
2018-02-06 22:54:53 +01:00
|
|
|
|
2021-02-28 01:16:59 +01:00
|
|
|
export function do_set_reminder_for_message(message_id, timestamp) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $row = $(`[zid='${CSS.escape(message_id)}']`);
|
2018-02-07 01:13:11 +01:00
|
|
|
function error() {
|
2022-01-25 11:36:19 +01:00
|
|
|
$row.find(".alert-msg")
|
2021-04-13 06:51:54 +02:00
|
|
|
.text($t({defaultMessage: "Reminder not set!"}))
|
2018-02-07 01:13:11 +01:00
|
|
|
.css("display", "block")
|
|
|
|
.css("color", "#b94a48")
|
2020-07-15 00:34:28 +02:00
|
|
|
.delay(1000)
|
|
|
|
.fadeOut(300, function () {
|
2018-02-07 01:13:11 +01:00
|
|
|
$(this).css("color", "");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-30 02:21:21 +02:00
|
|
|
const message = message_lists.current.get(message_id);
|
2018-02-07 01:13:11 +01:00
|
|
|
|
|
|
|
if (!message.raw_content) {
|
2021-03-30 02:21:21 +02:00
|
|
|
const msg_list = message_lists.current;
|
2018-02-07 01:13:11 +01:00
|
|
|
channel.get({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/messages/" + message.id,
|
2020-07-20 22:18:43 +02:00
|
|
|
success(data) {
|
2021-03-30 02:21:21 +02:00
|
|
|
if (message_lists.current === msg_list) {
|
2018-02-07 01:13:11 +01:00
|
|
|
message.raw_content = data.raw_content;
|
2021-02-28 01:16:59 +01:00
|
|
|
do_set_reminder_for_message(message_id, timestamp);
|
2018-02-07 01:13:11 +01:00
|
|
|
}
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error,
|
2018-02-07 01:13:11 +01:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-01 19:14:26 +01:00
|
|
|
const link_to_msg = hash_util.by_conversation_and_time_url(message);
|
2020-07-15 00:34:28 +02:00
|
|
|
const reminder_msg_content =
|
|
|
|
message.raw_content + "\n\n[Link to conversation](" + link_to_msg + ")";
|
2019-11-02 00:06:25 +01:00
|
|
|
let reminder_message = {
|
2018-02-06 22:54:53 +01:00
|
|
|
type: "private",
|
|
|
|
sender_id: page_params.user_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
stream: "",
|
2018-02-06 22:54:53 +01:00
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
reminder_message.topic = "";
|
2018-12-22 18:44:54 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const recipient = page_params.email;
|
|
|
|
const emails = util.extract_pm_recipients(recipient);
|
2018-02-06 22:54:53 +01:00
|
|
|
reminder_message.to = emails;
|
|
|
|
reminder_message.reply_to = recipient;
|
|
|
|
reminder_message.private_message_recipient = recipient;
|
|
|
|
reminder_message.to_user_ids = people.email_list_to_user_ids_string(emails);
|
|
|
|
|
|
|
|
function success() {
|
2022-01-25 11:36:19 +01:00
|
|
|
$row.find(".alert-msg")
|
2021-04-13 06:51:54 +02:00
|
|
|
.text($t({defaultMessage: "Reminder set!"}))
|
2018-02-06 22:54:53 +01:00
|
|
|
.css("display", "block")
|
2020-07-15 00:34:28 +02:00
|
|
|
.delay(1000)
|
|
|
|
.fadeOut(300);
|
2018-02-06 22:54:53 +01:00
|
|
|
}
|
|
|
|
|
2018-02-09 00:53:32 +01:00
|
|
|
reminder_message = patch_request_for_scheduling(
|
2020-07-15 00:34:28 +02:00
|
|
|
reminder_message,
|
|
|
|
reminder_msg_content,
|
|
|
|
timestamp,
|
2020-07-02 02:16:03 +02:00
|
|
|
deferred_message_types.reminders.delivery_type,
|
2018-02-09 00:53:32 +01:00
|
|
|
);
|
|
|
|
transmit.send_message(reminder_message, success, error);
|
2021-02-28 01:16:59 +01:00
|
|
|
}
|