Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
message_list_view: Ignore moved messages for subscription dividers.
Pratik Chanda07257440ca59
diff --git a/web/src/message_list_view.ts b/web/src/message_list_view.tsindex 7fe536e068e..85f0d40bd82 100644--- a/web/src/message_list_view.ts+++ b/web/src/message_list_view.ts@@ -137,6 +137,11 @@ type RenderingPlan = {append_messages: MessageContainer[];};+type BoundaryMessageContainerInfo = {+ message_group: MessageGroup;+ message_container: MessageContainer;+};+function same_day(earlier_msg: Message | undefined, later_msg: Message | undefined): boolean {if (earlier_msg === undefined || later_msg === undefined) {return false;@@ -196,14 +201,37 @@ function clear_message_divider(message_container: MessageContainer): void {message_container.date_divider_html = undefined;}+function was_moved_from_another_channel(message: Message): boolean {+ // A moved message's `historical` flag reflects the channel it was+ // originally sent to, so it says nothing about the user's+ // subscription history in the current channel.+ if (message.type !== "stream") {+ return false;+ }+ // edit_history is newest-first, so the oldest stream-move entry's+ // prev_stream is the original channel; a message moved back there+ // has a meaningful flag again.+ const original_stream_id = message.edit_history?.findLast(+ (entry) => entry.prev_stream !== undefined,+ )?.prev_stream;+ return original_stream_id !== undefined && original_stream_id !== message.stream_id;+}+function wants_subscription_divider(prev_historical: boolean | undefined,message: Message,+ message_was_moved: boolean,): boolean {// A continuation message shows an inline subscription divider when its- // `historical` flag differs from the previous one, i.e. the user- // (un)subscribed between them.- return prev_historical !== undefined && prev_historical !== message.historical;+ // `historical` flag differs from the previous meaningful one, i.e. the+ // user (un)subscribed between them. A moved message's flag is+ // meaningless here, so it never shows one; callers pass the+ // already-computed moved status to avoid recomputing it.+ return (+ !message_was_moved &&+ prev_historical !== undefined &&+ prev_historical !== message.historical+ );}function update_date_divider(opts: {@@ -685,10 +713,12 @@ export class MessageListView {group: MessageGroup,prev_historical: boolean | undefined,first_message: Message,+ first_message_was_moved: boolean,): void {const markers = this.get_possible_group_subscription_markers(prev_historical,first_message,+ first_message_was_moved,);if (markers) {Object.assign(group, markers);@@ -698,6 +728,7 @@ export class MessageListView {get_possible_group_subscription_markers(prev_historical: boolean | undefined,first_message: Message,+ first_message_was_moved: boolean,): SubscriptionMarkers | undefined {// The `historical` flag is present on messages which were// sent a time when the current user was not subscribed to the@@ -710,7 +741,11 @@ export class MessageListView {if (!this.list.data.filter.has_operator("channel")) {return undefined;}- if (prev_historical === undefined) {+ // A moved message's flag reflects its original channel, so a+ // boundary involving one tells us nothing about subscription+ // changes here. prev_historical already skips moved messages;+ // guard the group's first message too.+ if (prev_historical === undefined || first_message_was_moved) {return undefined;}@@ -739,26 +774,95 @@ export class MessageListView {return undefined;}+ get_boundary_message_info_with_meaningful_historical(+ position: "oldest" | "newest",+ ): BoundaryMessageContainerInfo | undefined {+ // Returns the oldest or newest rendered message container info whose+ // `historical` flag is meaningful in the current channel, or+ // undefined if there is none.+ //+ // Moved messages are skipped because their `historical` flag does not+ // represent the subscription state of this channel. This method is+ // used when prepending/appending messages, so it walks from the+ // requested boundary rather than flattening all rendered containers;+ // in the common case it finds a meaningful message immediately.+ const message_groups =+ position === "oldest" ? this._message_groups : this._message_groups.toReversed();++ for (const message_group of message_groups) {+ const meaningful_container =+ position === "oldest"+ ? message_group.message_containers.find(+ (message_container) =>+ !was_moved_from_another_channel(message_container.msg),+ )+ : message_group.message_containers.findLast(+ (message_container) =>+ !was_moved_from_another_channel(message_container.msg),+ );++ if (meaningful_container !== undefined) {+ return {+ message_container: meaningful_container,+ message_group,+ };+ }+ }++ return undefined;+ }+update_oldest_rendered_subscription_representation(prev_historical: boolean): void {- // Set the oldest rendered container's divider and marker against- // the prepended messages; the set_subscription_representation pass over- // the new groups did not cover this boundary.- const oldest_rendered_group = this._message_groups[0];- const oldest_rendered_container = this._message_groups[0]?.message_containers[0];- if (oldest_rendered_group !== undefined && oldest_rendered_container !== undefined) {- this.maybe_add_subscription_marker_to_group(- oldest_rendered_group,- prev_historical,- oldest_rendered_container.msg,- );- // The oldest rendered group may merge with the prepended content, so- // set the divider speculatively; merge_message_groups() clears it if- // the merge does not happen.- oldest_rendered_container.want_subscription_status_divider = wants_subscription_divider(+ // Set the oldest rendered meaningful container's divider/marker against+ // the prepended messages; the walk over the new groups did not cover+ // this boundary.+ //+ // The container is meaningful, hence never moved, so it passes `false`+ // for message_was_moved below.+ const boundary_message_container_info =+ this.get_boundary_message_info_with_meaningful_historical("oldest");+ if (boundary_message_container_info === undefined) {+ return;+ }+ const {message_group, message_container} = boundary_message_container_info;++ const is_first_container_in_group =+ message_group.message_containers[0] === message_container;++ if (!is_first_container_in_group) {+ // A continuation within its group: shown as an inline divider.+ message_container.want_subscription_status_divider = wants_subscription_divider(prev_historical,- oldest_rendered_container.msg,+ message_container.msg,+ false,);+ return;+ }++ // Starts its group: the change is shown as a group marker.+ this.maybe_add_subscription_marker_to_group(+ message_group,+ prev_historical,+ message_container.msg,+ false,+ );++ const is_oldest_rendered_group = message_group === this._message_groups[0];++ if (!is_oldest_rendered_group) {+ // This group can't merge with the prepended content, so no divider.+ message_container.want_subscription_status_divider = false;+ return;}++ // The oldest rendered group may merge with the prepended content, so+ // set the divider speculatively; merge_message_groups() clears it if+ // the merge does not happen.+ message_container.want_subscription_status_divider = wants_subscription_divider(+ prev_historical,+ message_container.msg,+ false,+ );}set_subscription_dividers_and_markers(@@ -768,38 +872,62 @@ export class MessageListView {// Walk the newly built groups in order, setting each container's// inline subscription-status divider and each group's bookend marker.// Both mark the same thing -- the `historical` flag flipping between- // two adjacent messages, i.e. the user (un)subscribed+ // two adjacent meaningful messages, i.e. the user (un)subscribed// between them -- shown inline between two messages or as a bookend// at a group boundary.//- // prev_historical stores the previous message's flag as we walk.+ // prev_historical holds the last meaningful flag as we walk. A message+ // moved here from another channel carries its original channel's flag,+ // so it is skipped and does not advance prev_historical, preserving a+ // real transition around a moved message rather than hiding it.//// The boundary against the already-rendered messages is handled after// the loop, since that container may merge into a neighbouring group.const is_prepend = where === "top";- let prev_historical = is_prepend ? undefined : this.list.last_message_historical;+ let prev_historical = is_prepend+ ? undefined+ : this.get_boundary_message_info_with_meaningful_historical("newest")?.message_container+ .msg.historical;+ const newest_rendered_historical = is_prepend ? undefined : prev_historical;++ // The first new container is the boundary against the already-rendered+ // messages when appending; cache whether it was moved during the walk+ // so the post-loop boundary step need not recompute it.+ const first_message_container = new_message_groups[0]?.message_containers[0];+ let first_message_was_moved = false;// Record a container's subscription change as a bookend marker when it// starts its group, or as an inline divider when it is a continuation,- // then advance prev_historical.+ // then advance prev_historical past meaningful messages.const set_subscription_representation = (group: MessageGroup,message_container: MessageContainer,starts_message_group: boolean,): void => {const message = message_container.msg;+ const message_was_moved = was_moved_from_another_channel(message);+ if (message_container === first_message_container) {+ first_message_was_moved = message_was_moved;+ }if (starts_message_group) {- this.maybe_add_subscription_marker_to_group(group, prev_historical, message);+ this.maybe_add_subscription_marker_to_group(+ group,+ prev_historical,+ message,+ message_was_moved,+ );message_container.want_subscription_status_divider = false;} else {message_container.want_subscription_status_divider = wants_subscription_divider(prev_historical,message,+ message_was_moved,);}-- prev_historical = message.historical;+ if (!message_was_moved) {+ prev_historical = message.historical;+ }};for (const message_group of new_message_groups) {@@ -820,15 +948,12 @@ export class MessageListView {// speculative: merge_message_groups() clears it if no merge takes place.if (is_prepend && prev_historical !== undefined) {this.update_oldest_rendered_subscription_representation(prev_historical);- } else {- const first_message_container = new_message_groups[0]?.message_containers[0];- if (first_message_container !== undefined) {- first_message_container.want_subscription_status_divider =- wants_subscription_divider(- this.list.last_message_historical,- first_message_container.msg,- );- }+ } else if (first_message_container !== undefined) {+ first_message_container.want_subscription_status_divider = wants_subscription_divider(+ newest_rendered_historical,+ first_message_container.msg,+ first_message_was_moved,+ );}}diff --git a/web/tests/message_list_view.test.cjs b/web/tests/message_list_view.test.cjsindex d27b3222147..4ad95bfa417 100644--- a/web/tests/message_list_view.test.cjs+++ b/web/tests/message_list_view.test.cjs@@ -544,25 +544,15 @@ test("merge_message_groups", ({mock_template}) => {view._message_groups = message_groups;view.list.unsubscribed_bookend_content = noop;view.list.subscribed_bookend_content = noop;- // render() records the historical status of the last rendered- // message so subsequent appends can compute the subscription- // divider against it; mirror that here for the merge tests.- const last_group = message_groups.at(-1);- if (last_group !== undefined) {- view.list.last_message_historical = last_group.message_containers.at(-1).msg.historical;- }return view;}- // Mirror the order in which render() computes subscription dividers and- // markers, merges the new groups, and records the last rendered- // message's historical status for the next batch.+ // Mirror the order in which render() computes subscription-status+ // dividers and markers on the new groups before merging them into+ // the rendered list.function add_message_groups(list, new_message_groups, where) {list.set_subscription_dividers_and_markers(new_message_groups, where);- const result = list.merge_message_groups(new_message_groups, where);- const last_group = list._message_groups.at(-1);- list.list.last_message_historical = last_group.message_containers.at(-1).msg.historical;- return result;+ return list.merge_message_groups(new_message_groups, where);}function extract_message_ids(lst) {@@ -851,6 +841,396 @@ test("merge_message_groups", ({mock_template}) => {assert.ok(!list._message_groups[1].message_containers[0].want_subscription_status_divider);assert.ok(list._message_groups[1].message_containers[1].want_subscription_status_divider);})();++ // Messages moved here from another channel carry a `historical` flag+ // that is meaningless in this channel, so they must not trigger+ // subscription-status dividers or bookends.+ function build_moved_message_context(message = {}) {+ // prev_stream differs from the default stream_id (2), marking the+ // message as moved here from another channel.+ return build_message_context({+ edit_history: [{prev_stream: 999}],+ ...message,+ });+ }++ (function test_append_moved_message_skips_subscription_divider() {+ const moved_message = build_moved_message_context({historical: true});+ const message_group1 = build_message_group([moved_message]);++ const message2 = build_message_context({historical: false});+ const message_group2 = build_message_group([message2]);++ const list = build_list([message_group1]);+ add_message_groups(list, [message_group2], "bottom");++ assert.ok(!message2.want_subscription_status_divider);+ assert_message_groups_list_equal(list._message_groups, [+ build_message_group([moved_message, message2]),+ ]);+ })();++ (function test_moved_message_never_gets_subscription_divider() {+ const message1 = build_message_context({historical: false});+ const message_group1 = build_message_group([message1]);++ const moved_message = build_moved_message_context({historical: true});+ const message_group2 = build_message_group([moved_message]);++ const list = build_list([message_group1]);+ add_message_groups(list, [message_group2], "bottom");++ // Assert the groups joined, so the divider check can't pass+ // vacuously on an unset field.+ assert_message_groups_list_equal(list._message_groups, [+ build_message_group([message1, moved_message]),+ ]);+ assert.equal(moved_message.want_subscription_status_divider, false);+ })();++ (function test_moved_last_message_skips_subscription_bookend() {+ // When the groups do not join (different topic), the transition is+ // shown as a group bookend; a moved message must not trigger it.+ const moved_message = build_moved_message_context({historical: true});+ const message_group1 = build_message_group([moved_message]);++ const message2 = build_message_context({historical: false, topic: "Test topic 2"});+ const message_group2 = build_message_group([message2]);++ const list = build_list([message_group1]);+ add_message_groups(list, [message_group2], "bottom");++ assert.equal(message_group2.bookend_top, undefined);+ assert.equal(message_group2.subscribed, undefined);+ assert.equal(message_group2.just_unsubscribed, undefined);+ })();++ (function test_moved_first_message_skips_subscription_bookend() {+ const message1 = build_message_context({historical: false});+ const message_group1 = build_message_group([message1]);++ const moved_message = build_moved_message_context({+ historical: true,+ topic: "Test topic 2",+ });+ const message_group2 = build_message_group([moved_message]);++ const list = build_list([message_group1]);+ add_message_groups(list, [message_group2], "bottom");++ assert.equal(message_group2.bookend_top, undefined);+ assert.equal(message_group2.just_unsubscribed, undefined);+ })();++ (function test_message_moved_back_to_original_channel() {+ // A message moved away and then back to its current channel carries+ // a meaningful historical flag, so it is treated as a normal message.+ const moved_back_message = build_message_context({+ historical: true,+ edit_history: [{prev_stream: 2}],+ });+ const message_group1 = build_message_group([moved_back_message]);++ const message2 = build_message_context({historical: false});+ const message_group2 = build_message_group([message2]);++ const list = build_list([message_group1]);+ add_message_groups(list, [message_group2], "bottom");++ assert.ok(message2.want_subscription_status_divider);+ })();++ (function test_prepend_moved_message_skips_subscription_bookend() {+ const message1 = build_message_context({historical: false});+ const message_group1 = build_message_group([message1]);++ const moved_message = build_moved_message_context({+ historical: true,+ topic: "Test topic 2",+ });+ const message_group2 = build_message_group([moved_message]);++ const list = build_list([message_group1]);+ add_message_groups(list, [message_group2], "top");++ // The bookend, if any, is added to the existing (lower) group.+ assert.equal(message_group1.bookend_top, undefined);+ assert.equal(message_group1.subscribed, undefined);+ })();++ (function test_divider_shown_across_moved_message_in_batch() {+ // A moved message between two messages with meaningful flags must+ // not hide the real transition between them: the divider belongs+ // on the first meaningful message after the flip.+ const message1 = build_message_context({historical: true});+ const message_group1 = build_message_group([message1]);++ const moved_message = build_moved_message_context({historical: true});+ const message2 = build_message_context({historical: false});+ const message_group2 = build_message_group([moved_message, message2]);++ const list = build_list([message_group1]);+ add_message_groups(list, [message_group2], "bottom");++ assert_message_groups_list_equal(list._message_groups, [+ build_message_group([message1, moved_message, message2]),+ ]);+ assert.ok(!moved_message.want_subscription_status_divider);+ assert.ok(message2.want_subscription_status_divider);+ })();++ (function test_divider_shown_when_newest_rendered_message_was_moved() {+ // Likewise when the moved message was already rendered: the new+ // message is compared with the newest meaningful flag, not with+ // the moved message just above it.+ const message1 = build_message_context({historical: true});+ const moved_message = build_moved_message_context({historical: true});+ const message_group1 = build_message_group([message1, moved_message]);++ const message2 = build_message_context({historical: false});+ const message_group2 = build_message_group([message2]);++ const list = build_list([message_group1]);+ add_message_groups(list, [message_group2], "bottom");++ assert_message_groups_list_equal(list._message_groups, [+ build_message_group([message1, moved_message, message2]),+ ]);+ assert.ok(message2.want_subscription_status_divider);+ })();++ (function test_prepend_divider_shown_across_moved_message() {+ const message1 = build_message_context({historical: false});+ const message_group1 = build_message_group([message1]);++ const message2 = build_message_context({historical: true});+ const moved_message = build_moved_message_context({historical: true});+ const message_group2 = build_message_group([message2, moved_message]);++ const list = build_list([message_group1]);+ add_message_groups(list, [message_group2], "top");++ // The existing message joins below the prepended group; its+ // divider reflects the flip from message2's flag, not the moved+ // message's.+ assert_message_groups_list_equal(list._message_groups, [+ build_message_group([message2, moved_message, message1]),+ ]);+ assert.ok(message1.want_subscription_status_divider);+ assert.ok(!moved_message.want_subscription_status_divider);+ })();++ (function test_prepend_marker_when_oldest_rendered_group_was_moved() {+ // The oldest rendered group consists only of a message moved here from+ // another channel, so the first meaningful rendered message starts a+ // later group that the prepended messages can never merge into. A+ // subscription change at that boundary belongs on the group as a+ // bookend, not as an inline divider on its first message.+ const moved_message = build_moved_message_context({+ historical: true,+ topic: "Moved topic",+ });+ const moved_group = build_message_group([moved_message]);++ const message1 = build_message_context({historical: true, topic: "Subscribed topic"});+ const message_group1 = build_message_group([message1]);++ const message2 = build_message_context({historical: false, topic: "New topic"});+ const message_group2 = build_message_group([message2]);++ const list = build_list([moved_group, message_group1]);+ add_message_groups(list, [message_group2], "top");++ // No merge happens (distinct topics), so the flip from subscribed+ // (message2) to unsubscribed (message1) shows as a bookend on+ // message1's group, and message1 keeps no inline divider.+ assert.ok(message_group1.bookend_top);+ assert.equal(message_group1.just_unsubscribed, true);+ assert.ok(!message1.want_subscription_status_divider);+ })();++ (function test_prepend_inline_divider_when_oldest_rendered_group_starts_with_moved() {+ // The oldest rendered group starts with a moved message but+ // continues with a meaningful one. The boundary against the+ // prepended messages is shown as an inline divider on that+ // meaningful continuation message, since the moved message above it+ // in the same group makes it a continuation rather than the group's+ // first message.+ const moved_message = build_moved_message_context({historical: true});+ const message1 = build_message_context({historical: true});+ // Same recipient, so both messages live in one rendered group.+ const rendered_group = build_message_group([moved_message, message1]);++ const message2 = build_message_context({historical: false, topic: "New topic"});+ const message_group2 = build_message_group([message2]);++ const list = build_list([rendered_group]);+ add_message_groups(list, [message_group2], "top");++ // No merge happens (distinct topics), so the flip from message2+ // (subscribed) to message1 (unsubscribed) is shown inline on+ // message1, not on the moved message above it.+ assert.ok(message1.want_subscription_status_divider);+ assert.ok(!moved_message.want_subscription_status_divider);+ })();++ (function test_prepend_no_boundary_update_when_all_rendered_messages_moved() {+ // Every already-rendered message was moved here from another+ // channel, so none has a meaningful flag. The prepended-boundary+ // update finds no container to mark and leaves the rendered content+ // untouched.+ const moved_message = build_moved_message_context({historical: true});+ const rendered_group = build_message_group([moved_message]);++ const message1 = build_message_context({historical: false, topic: "New topic"});+ const message_group1 = build_message_group([message1]);++ const list = build_list([rendered_group]);+ add_message_groups(list, [message_group1], "top");++ assert.ok(!moved_message.want_subscription_status_divider);+ assert.equal(rendered_group.bookend_top, undefined);+ assert.equal(rendered_group.subscribed, undefined);+ assert.equal(rendered_group.just_unsubscribed, undefined);+ })();++ (function test_no_subscription_marker_without_historical_flip() {+ // Two non-merging groups in the same subscription state produce no+ // bookend: there is no historical-flag flip between them.+ const message1 = build_message_context({historical: false});+ const message_group1 = build_message_group([message1]);++ const message2 = build_message_context({historical: false, topic: "New topic"});+ const message_group2 = build_message_group([message2]);++ const list = build_list([message_group1]);+ add_message_groups(list, [message_group2], "bottom");++ assert.equal(message_group2.bookend_top, undefined);+ assert.equal(message_group2.subscribed, undefined);+ assert.equal(message_group2.just_unsubscribed, undefined);+ assert.ok(!message2.want_subscription_status_divider);+ })();+});++test("get_boundary_message_info_with_meaningful_historical", () => {+ // get_boundary_message_info_with_meaningful_historical("newest")+ // feeds the trailing bookend, which must infer subscription status from the newest+ // message whose `historical` flag is meaningful in this channel, skipping+ // messages moved here from another channel.+ function build_message_container(message = {}) {+ return {+ msg: {+ id: _.uniqueId("test_message_"),+ type: "stream",+ stream_id: 2,+ ...message,+ },+ };+ }++ function build_list(message_groups) {+ const list = new MessageListView({id: 1}, true, true);+ list._message_groups = message_groups;+ return list;+ }++ // The newest message's flag is used directly when it is meaningful.+ let list = build_list([+ {+ message_containers: [+ build_message_container({historical: false}),+ build_message_container({historical: true}),+ ],+ },+ ]);+ assert.equal(+ list.get_boundary_message_info_with_meaningful_historical("newest")?.message_container.msg+ .historical,+ true,+ );++ // Moved messages are skipped, across group boundaries.+ list = build_list([+ {message_containers: [build_message_container({historical: false})]},+ {+ message_containers: [+ build_message_container({historical: true, edit_history: [{prev_stream: 999}]}),+ ],+ },+ ]);+ assert.equal(+ list.get_boundary_message_info_with_meaningful_historical("newest")?.message_container.msg+ .historical,+ false,+ );++ // A message moved back to its original channel has a meaningful flag.+ list = build_list([+ {+ message_containers: [+ build_message_container({historical: true, edit_history: [{prev_stream: 2}]}),+ ],+ },+ ]);+ assert.equal(+ list.get_boundary_message_info_with_meaningful_historical("newest")?.message_container.msg+ .historical,+ true,+ );++ // With no meaningful flag at all, the status is unknown.+ list = build_list([+ {+ message_containers: [+ build_message_container({historical: true, edit_history: [{prev_stream: 999}]}),+ ],+ },+ ]);+ assert.equal(+ list.get_boundary_message_info_with_meaningful_historical("newest")?.message_container.msg+ .historical,+ undefined,+ );++ list = build_list([]);+ assert.equal(+ list.get_boundary_message_info_with_meaningful_historical("newest")?.message_container.msg+ .historical,+ undefined,+ );+});++test("set_subscription_dividers_and_markers ignores non-channel narrows", () => {+ // Subscription-status dividers and bookends only make sense in a+ // single-channel narrow. In other narrows (e.g. direct messages) the+ // pass must run without adding any divider or bookend, and a non-stream+ // message is never treated as moved.+ const filter = new Filter([{operator: "is", operand: "dm"}]);+ const list = new message_list.MessageList({+ data: new MessageListData({excludes_muted_topics: false, filter}),+ is_node_test: true,+ });+ const view = new MessageListView(list, true, true);++ const dm1 = {include_sender: true, msg: {id: 1, type: "private", to_user_ids: "5"}};+ const dm2 = {include_sender: true, msg: {id: 2, type: "private", to_user_ids: "5"}};+ const rendered_group = {message_containers: [dm1], message_group_id: "dm-group-1"};+ const new_group = {message_containers: [dm2], message_group_id: "dm-group-2"};+ view._message_groups = [rendered_group];++ view.set_subscription_dividers_and_markers([new_group], "bottom");++ assert.ok(!dm2.want_subscription_status_divider);+ assert.equal(new_group.bookend_top, undefined);+ assert.equal(new_group.subscribed, undefined);+ assert.equal(new_group.just_unsubscribed, undefined);++ // Appending no new groups is a no-op: there is no boundary container to+ // mark.+ assert.doesNotThrow(() => {+ view.set_subscription_dividers_and_markers([], "bottom");+ });});test("render_windows", ({mock_template}) => {