2020-02-13 22:34:29 +01:00
|
|
|
const util = require("./util");
|
2019-11-02 00:06:25 +01:00
|
|
|
const deferred_message_types = {
|
2018-02-06 22:54:53 +01:00
|
|
|
scheduled: {
|
|
|
|
delivery_type: 'send_later',
|
|
|
|
test: /^\/schedule/,
|
|
|
|
slash_command: '/schedule',
|
|
|
|
},
|
|
|
|
reminders: {
|
|
|
|
delivery_type: 'remind',
|
|
|
|
test: /^\/remind/,
|
|
|
|
slash_command: '/remind',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.deferred_message_types = deferred_message_types;
|
|
|
|
|
|
|
|
exports.is_deferred_delivery = function (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;
|
2018-06-06 18:19:09 +02:00
|
|
|
return reminders_test.test(message_content) ||
|
|
|
|
scheduled_test.test(message_content);
|
2018-02-06 22:54:53 +01:00
|
|
|
};
|
|
|
|
|
2018-02-09 00:53:32 +01:00
|
|
|
function patch_request_for_scheduling(request, message_content, deliver_at, delivery_type) {
|
|
|
|
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;
|
|
|
|
new_request.tz_guess = moment.tz.guess();
|
|
|
|
return new_request;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.schedule_message = function (request) {
|
|
|
|
if (request === undefined) {
|
|
|
|
request = compose.create_message_object();
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const raw_message = request.content.split('\n');
|
|
|
|
const command_line = raw_message[0];
|
|
|
|
const message = raw_message.slice(1).join('\n');
|
2018-02-06 22:54:53 +01:00
|
|
|
|
2020-02-08 03:33:46 +01:00
|
|
|
const deferred_message_type = deferred_message_types.filter(
|
2020-07-02 02:16:03 +02:00
|
|
|
(props) => command_line.match(props.test) !== null,
|
2020-02-08 03:33:46 +01:00
|
|
|
)[0];
|
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
|
|
|
|
|
|
|
if (message.trim() === '' || deliver_at.trim() === '' ||
|
|
|
|
command_line.slice(command.length, command.length + 1) !== ' ') {
|
|
|
|
|
|
|
|
$("#compose-textarea").attr('disabled', false);
|
|
|
|
if (command_line.slice(command.length, command.length + 1) !== ' ') {
|
|
|
|
compose.compose_error(i18n.t('Invalid slash command. Check if you are missing a space after the command.'), $('#compose-textarea'));
|
|
|
|
} else if (deliver_at.trim() === '') {
|
2018-02-07 01:13:11 +01:00
|
|
|
compose.compose_error(i18n.t('Please specify a date or time'), $('#compose-textarea'));
|
2018-02-06 22:54:53 +01:00
|
|
|
} else {
|
|
|
|
compose.compose_error(i18n.t('Your reminder note is empty!'), $('#compose-textarea'));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-09 00:53:32 +01:00
|
|
|
request = patch_request_for_scheduling(
|
2020-07-02 02:16:03 +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) {
|
|
|
|
notifications.notify_above_composebox('Scheduled your Message to be delivered at: ' + data.deliver_at);
|
|
|
|
}
|
2018-02-06 22:54:53 +01:00
|
|
|
$("#compose-textarea").attr('disabled', false);
|
|
|
|
compose.clear_compose_box();
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const error = function (response) {
|
2018-02-06 22:54:53 +01:00
|
|
|
$("#compose-textarea").attr('disabled', false);
|
|
|
|
compose.compose_error(response, $('#compose-textarea'));
|
|
|
|
};
|
|
|
|
/* We are adding a disable on compose under this block because we
|
|
|
|
want slash commands to be blocking in nature. */
|
|
|
|
$("#compose-textarea").attr('disabled', true);
|
|
|
|
|
2018-02-09 00:53:32 +01:00
|
|
|
transmit.send_message(request, success, error);
|
2018-02-06 22:54:53 +01:00
|
|
|
};
|
|
|
|
|
2019-08-28 17:46:10 +02:00
|
|
|
exports.do_set_reminder_for_message = function (message_id, timestamp) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = $("[zid='" + message_id + "']");
|
2018-02-07 01:13:11 +01:00
|
|
|
function error() {
|
|
|
|
row.find(".alert-msg")
|
|
|
|
.text(i18n.t("Reminder not set!"))
|
|
|
|
.css("display", "block")
|
|
|
|
.css("color", "#b94a48")
|
|
|
|
.delay(1000).fadeOut(300, function () {
|
|
|
|
$(this).css("color", "");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = current_msg_list.get(message_id);
|
2018-02-07 01:13:11 +01:00
|
|
|
|
|
|
|
if (!message.raw_content) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const msg_list = current_msg_list;
|
2018-02-07 01:13:11 +01:00
|
|
|
channel.get({
|
|
|
|
url: '/json/messages/' + message.id,
|
|
|
|
idempotent: true,
|
|
|
|
success: function (data) {
|
|
|
|
if (current_msg_list === msg_list) {
|
|
|
|
message.raw_content = data.raw_content;
|
2019-08-28 17:46:10 +02:00
|
|
|
exports.do_set_reminder_for_message(message_id, timestamp);
|
2018-02-07 01:13:11 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
error: error,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const link_to_msg = hash_util.by_conversation_and_time_uri(message);
|
|
|
|
const reminder_msg_content = message.raw_content + '\n\n[Link to conversation](' + link_to_msg + ')';
|
|
|
|
let reminder_message = {
|
2018-02-06 22:54:53 +01:00
|
|
|
type: "private",
|
|
|
|
sender_id: page_params.user_id,
|
|
|
|
stream: '',
|
|
|
|
};
|
2020-02-19 02:44:17 +01: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() {
|
|
|
|
row.find(".alert-msg")
|
|
|
|
.text(i18n.t("Reminder set!"))
|
|
|
|
.css("display", "block")
|
|
|
|
.delay(1000).fadeOut(300);
|
|
|
|
}
|
|
|
|
|
2018-02-09 00:53:32 +01:00
|
|
|
reminder_message = patch_request_for_scheduling(
|
|
|
|
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);
|
2018-02-06 22:54:53 +01:00
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.reminder = exports;
|