popover: Extract with_first_message_id helper.

This commit is contained in:
Tim Abbott
2021-06-11 12:00:18 -07:00
committed by Tim Abbott
parent d48a0e8076
commit 639e2eef5e

View File

@@ -548,6 +548,43 @@ export function register_stream_handlers() {
}); });
} }
function with_first_message_id(stream_id, topic_name, success_cb, error_cb) {
// The API endpoint for editing messages to change their
// content, topic, or stream requires a message ID.
//
// Because we don't have full data in the browser client, it's
// possible that we might display a topic in the left sidebar
// (and thus expose the UI for moving its topic to another
// stream) without having a message ID that is definitely
// within the topic. (The comments in stream_topic_history.js
// discuss the tricky issues around message deletion that are
// involved here).
//
// To ensure this option works reliably at a small latency
// cost for a rare operation, we just ask the server for the
// latest message ID in the topic.
const data = {
anchor: "newest",
num_before: 1,
num_after: 0,
narrow: JSON.stringify([
{operator: "stream", operand: stream_id},
{operator: "topic", operand: topic_name},
]),
};
channel.get({
url: "/json/messages",
data,
idempotent: true,
success(data) {
const message_id = data.messages[0].id;
success_cb(message_id);
},
error_cb,
});
}
export function register_topic_handlers() { export function register_topic_handlers() {
// Mute the topic // Mute the topic
$("body").on("click", ".sidebar-popover-mute-topic", (e) => { $("body").on("click", ".sidebar-popover-mute-topic", (e) => {
@@ -668,38 +705,11 @@ export function register_topic_handlers() {
return; return;
} }
// The API endpoint for editing messages to change their
// content, topic, or stream requires a message ID.
//
// Because we don't have full data in the browser client, it's
// possible that we might display a topic in the left sidebar
// (and thus expose the UI for moving its topic to another
// stream) without having a message ID that is definitely
// within the topic. (The comments in stream_topic_history.js
// discuss the tricky issues around message deletion that are
// involved here).
//
// To ensure this option works reliably at a small latency
// cost for a rare operation, we just ask the server for the
// latest message ID in the topic.
const data = {
anchor: "newest",
num_before: 1,
num_after: 0,
narrow: JSON.stringify([
{operator: "stream", operand: current_stream_id},
{operator: "topic", operand: old_topic_name},
]),
};
message_edit.show_topic_move_spinner(); message_edit.show_topic_move_spinner();
channel.get({ with_first_message_id(
url: "/json/messages", current_stream_id,
data, old_topic_name,
idempotent: true, (message_id) => {
success(data) {
const message_id = data.messages[0].id;
if (old_topic_name.trim() === new_topic_name.trim()) { if (old_topic_name.trim() === new_topic_name.trim()) {
// We use `undefined` to tell the server that // We use `undefined` to tell the server that
// there has been no change in the topic name. // there has been no change in the topic name.
@@ -716,10 +726,10 @@ export function register_topic_handlers() {
); );
} }
}, },
error(xhr) { (xhr) => {
message_edit.hide_topic_move_spinner(); message_edit.hide_topic_move_spinner();
show_error_msg(xhr.responseJSON.msg); show_error_msg(xhr.responseJSON.msg);
}, },
}); );
}); });
} }