drafts: Save stream_id along with stream names for drafts.

This commit changes snapshot_message to store stream_id for
drafts along with stream names. The stream_id field is
undefined if draft is for empty or invalid stream name.

After this change:
  - If draft has a valid stream_id stored and it maps to
    a stream, then we display the stream name from the
    obtained stream object.

  - If draft.stream_id is undefined or doesn't map to a stream,
    then we display the name stored in draft.stream, which can
    be invalid (no stream of this name existed ever), can be
    empty and can also be name of a deactivated stream.

This change helps us to show correct stream-name for drafts
in case of renaming a stream.

Fixes #15155.
This commit is contained in:
sahil839
2020-06-07 03:27:20 +05:30
committed by Tim Abbott
parent 0dace18f33
commit 238ba1c36f
2 changed files with 40 additions and 2 deletions

View File

@@ -32,7 +32,12 @@ mock_esm("../../static/js/stream_data", {
get_color() {
return "#FFFFFF";
},
get_sub(stream_name) {
assert.equal(stream_name, "stream");
return {stream_id: 30};
},
});
const sub_store = mock_esm("../../static/js/sub_store");
user_settings.twenty_four_hour_time = false;
const {localstorage} = zrequire("localstorage");
@@ -55,6 +60,7 @@ const compose_args_for_legacy_draft = {
const draft_1 = {
stream: "stream",
stream_id: 30,
topic: "topic",
type: "stream",
content: "Test stream message",
@@ -214,6 +220,7 @@ test("format_drafts", ({override, mock_template}) => {
topic: "topic",
type: "stream",
content: "Test stream message",
stream_id: 30,
updatedAt: feb12().getTime(),
};
const draft_2 = {
@@ -300,6 +307,11 @@ test("format_drafts", ({override, mock_template}) => {
const stub_render_now = timerender.render_now;
override(timerender, "render_now", (time) => stub_render_now(time, new Date(1549958107000)));
sub_store.get = function (stream_id) {
assert.equal(stream_id, 30);
return {name: "stream"};
};
mock_template("draft_table_body.hbs", false, (data) => {
// Tests formatting and sorting of drafts
assert.deepEqual(data.drafts, expected);
@@ -310,6 +322,19 @@ test("format_drafts", ({override, mock_template}) => {
override(drafts, "set_initial_element", noop);
$.create("#drafts_table .draft-row", {children: []});
drafts.launch();
$.clear_all_elements();
$.create("#drafts_table .draft-row", {children: []});
$("#draft_overlay").css = () => {};
sub_store.get = function (stream_id) {
assert.equal(stream_id, 30);
return {name: "stream-rename"};
};
expected[0].stream = "stream-rename";
drafts.launch();
timerender.__Rewire__("render_now", stub_render_now);
});

View File

@@ -20,6 +20,7 @@ import * as narrow from "./narrow";
import * as overlays from "./overlays";
import * as people from "./people";
import * as stream_data from "./stream_data";
import * as sub_store from "./sub_store";
import * as timerender from "./timerender";
import * as util from "./util";
@@ -107,6 +108,10 @@ export function snapshot_message() {
message.private_message_recipient = recipient;
} else {
message.stream = compose_state.stream_name();
const sub = stream_data.get_sub(message.stream);
if (sub) {
message.stream_id = sub.stream_id;
}
message.topic = compose_state.topic();
}
return message;
@@ -241,9 +246,17 @@ export function format_draft(draft) {
// In case there is no stream for the draft, we need a
// single space char for proper rendering of the stream label
const space_string = new Handlebars.SafeString(" ");
const stream = draft.stream.length > 0 ? draft.stream : space_string;
let stream = draft.stream.length > 0 ? draft.stream : space_string;
if (draft.stream_id) {
const sub = sub_store.get(draft.stream_id);
if (sub && sub.name !== stream) {
stream = sub.name;
draft.stream = stream;
draft_model.editDraft(id, draft);
}
}
let draft_topic = util.get_draft_topic(draft);
const draft_stream_color = stream_data.get_color(draft.stream);
const draft_stream_color = stream_data.get_color(stream);
if (draft_topic === "") {
draft_topic = compose.empty_topic_placeholder();