2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-02-28 00:51:22 +01:00
|
|
|
import * as channel from "./channel";
|
|
|
|
import * as people from "./people";
|
|
|
|
import * as reload from "./reload";
|
|
|
|
import * as reload_state from "./reload_state";
|
|
|
|
import * as sent_messages from "./sent_messages";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 00:51:22 +01:00
|
|
|
export function send_message(request, on_success, error) {
|
2018-02-20 13:08:50 +01:00
|
|
|
channel.post({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/messages",
|
2018-02-20 13:08:50 +01:00
|
|
|
data: request,
|
2020-04-09 23:57:52 +02:00
|
|
|
success: function success(data) {
|
|
|
|
// Call back to our callers to do things like closing the compose
|
|
|
|
// box and turning off spinners and reifying locally echoed messages.
|
|
|
|
on_success(data);
|
|
|
|
|
|
|
|
// Once everything is done, get ready to report times to the server.
|
|
|
|
sent_messages.report_server_ack(request.local_id);
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr, error_type) {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (error_type !== "timeout" && reload_state.is_pending()) {
|
2018-02-20 13:08:50 +01:00
|
|
|
// The error might be due to the server changing
|
2020-07-15 00:34:28 +02:00
|
|
|
reload.initiate({
|
|
|
|
immediate: true,
|
|
|
|
save_pointer: true,
|
|
|
|
save_narrow: true,
|
|
|
|
save_compose: true,
|
|
|
|
send_after_reload: true,
|
|
|
|
});
|
2018-02-20 13:08:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const response = channel.xhr_error_message("Error sending message", xhr);
|
2018-02-20 13:08:50 +01:00
|
|
|
error(response);
|
|
|
|
},
|
|
|
|
});
|
2021-02-28 00:51:22 +01:00
|
|
|
}
|
2018-02-20 13:08:50 +01:00
|
|
|
|
2021-02-28 00:51:22 +01:00
|
|
|
export function reply_message(opts) {
|
2018-02-23 16:18:27 +01:00
|
|
|
// This code does an application-triggered reply to a message (as
|
|
|
|
// opposed to the user themselves doing it). Its only use case
|
|
|
|
// for now is experimental widget-aware bots, so treat this as
|
|
|
|
// somewhat beta code. To understand the use case, think of a
|
|
|
|
// bot that wants to give users 3 or 4 canned replies to some
|
|
|
|
// choice, but it wants to front-end each of these options
|
|
|
|
// with a one-click button. This function is part of that architecture.
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = opts.message;
|
|
|
|
let content = opts.content;
|
2018-02-23 16:18:27 +01:00
|
|
|
|
|
|
|
function success() {
|
|
|
|
// TODO: If server response comes back before the message event,
|
|
|
|
// we could show it earlier, although that creates some
|
|
|
|
// complexity. For now do nothing. (Note that send_message
|
|
|
|
// already handles things like reporting times to the server.)
|
|
|
|
}
|
|
|
|
|
|
|
|
function error() {
|
|
|
|
// TODO: In our current use case, which is widgets, to meaningfully
|
|
|
|
// handle errors, we would want the widget to provide some
|
|
|
|
// kind of callback to us so it can do some appropriate UI.
|
|
|
|
// For now do nothing.
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const locally_echoed = false;
|
|
|
|
const local_id = sent_messages.get_new_local_id();
|
2018-02-23 16:18:27 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const reply = {
|
2018-02-23 16:18:27 +01:00
|
|
|
sender_id: page_params.user_id,
|
|
|
|
queue_id: page_params.queue_id,
|
2020-07-20 22:18:43 +02:00
|
|
|
local_id,
|
2018-02-23 16:18:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
sent_messages.start_tracking_message({
|
2020-07-20 22:18:43 +02:00
|
|
|
local_id,
|
|
|
|
locally_echoed,
|
2018-02-23 16:18:27 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (message.type === "stream") {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream = message.stream;
|
2018-02-23 16:18:27 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const mention = people.get_mention_syntax(message.sender_full_name, message.sender_id);
|
2018-02-23 16:18:27 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
content = mention + " " + content;
|
2018-02-23 16:18:27 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
reply.type = "stream";
|
2020-07-16 23:29:01 +02:00
|
|
|
reply.to = stream;
|
2018-02-23 16:18:27 +01:00
|
|
|
reply.content = content;
|
2020-02-19 02:44:17 +01:00
|
|
|
reply.topic = message.topic;
|
2018-02-23 16:18:27 +01:00
|
|
|
|
2021-02-28 00:51:22 +01:00
|
|
|
send_message(reply, success, error);
|
2018-02-23 16:18:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (message.type === "private") {
|
2019-11-02 00:06:25 +01:00
|
|
|
const pm_recipient = people.pm_reply_to(message);
|
2018-02-23 16:18:27 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
reply.type = "private";
|
|
|
|
reply.to = JSON.stringify(pm_recipient.split(","));
|
2018-02-23 16:18:27 +01:00
|
|
|
reply.content = content;
|
|
|
|
|
2021-02-28 00:51:22 +01:00
|
|
|
send_message(reply, success, error);
|
2018-02-23 16:18:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("unknown message type: " + message.type);
|
2021-02-28 00:51:22 +01:00
|
|
|
}
|