message_list: Deduplicate stream muting live update code path.

We can dramtically simplify the stream muting live-update code path
for updating home_msg_list by observing the only thing it was doing
intentionally differently from update_muting_and_rerender is replacing
_all_items with a new version computed from from all_messages_data,
and the rest of update_muting_and_rerender can handle the live update
correctly unmodified.

This deduplication means live-update of "Unmute topic" just requires
updating the `in:home` filter logic appropriately, without any special
live update work.
This commit is contained in:
Tim Abbott 2023-04-21 13:15:25 -07:00
parent 47b6bec0d4
commit ae314a3ca9
2 changed files with 22 additions and 57 deletions

View File

@ -1,6 +1,7 @@
import autosize from "autosize";
import $ from "jquery";
import {all_messages_data} from "./all_messages_data";
import * as blueslip from "./blueslip";
import {MessageListData} from "./message_list_data";
import {MessageListView} from "./message_list_view";
@ -426,6 +427,20 @@ export class MessageList {
}
update_muting_and_rerender() {
// For the home message list, we need to re-initialize
// _all_items for stream muting/topic unmuting from
// all_messages_data, since otherwise unmuting a previously
// muted stream won't work.
//
// TODO: The zhome conditional is a bit awkward, but a check
// for whether the filter excludes muted streams wouldn't be
// correct, because other narrows can't pull from
// all_messages.
if (this.table_name === "zhome") {
this.data.clear();
this.data.add_messages(all_messages_data.all_messages());
}
this.data.update_items_for_muting();
// We need to rerender whether or not the narrow hides muted
// topics, because we need to update recipient bars for topics

View File

@ -1,11 +1,4 @@
import {all_messages_data} from "./all_messages_data";
import * as message_lists from "./message_lists";
import * as message_scroll from "./message_scroll";
import * as message_util from "./message_util";
import * as message_viewport from "./message_viewport";
import * as narrow_state from "./narrow_state";
import * as navigate from "./navigate";
import * as overlays from "./overlays";
import * as settings_notifications from "./settings_notifications";
import * as stream_edit from "./stream_edit";
import * as stream_list from "./stream_list";
@ -14,57 +7,14 @@ import * as unread_ui from "./unread_ui";
export function update_is_muted(sub, value) {
sub.is_muted = value;
setTimeout(() => {
let msg_offset;
let saved_ypos;
// Save our current scroll position
if (overlays.is_active() || overlays.is_modal_open()) {
saved_ypos = message_viewport.scrollTop();
} else if (
narrow_state.is_message_feed_visible() &&
message_lists.current === message_lists.home &&
message_lists.current.selected_row().offset() !== null
) {
msg_offset = message_lists.current.selected_row().offset().top;
}
// TODO: In theory, other message lists whose behavior depends on
// stream muting might need to be live-updated as well, but the
// current _all_items design doesn't have a way to support that.
message_lists.home.update_muting_and_rerender();
message_lists.home.clear({clear_selected_id: false});
// Recreate the message_lists.home with the newly filtered all_messages_data
message_util.add_old_messages(all_messages_data.all_messages(), message_lists.home);
// Ensure we're still at the same scroll position
if (overlays.is_overlay_or_modal_open()) {
message_viewport.scrollTop(saved_ypos);
} else if (
narrow_state.is_message_feed_visible() &&
message_lists.current === message_lists.home
) {
// We pass use_closest to handle the case where the
// currently selected message is being hidden from the
// home view
message_lists.home.select_id(message_lists.home.selected_id(), {
use_closest: true,
empty_ok: true,
mark_read: false,
});
if (message_lists.current.selected_id() !== -1) {
message_lists.current.view.set_message_offset(msg_offset);
}
}
// In case we added messages to what's visible in the home
// view, we need to re-scroll to make sure the pointer is
// still visible. We don't want the auto-scroll handler to
// move our pointer to the old scroll location before we have
// a chance to update it.
navigate.plan_scroll_to_selected();
message_scroll.suppress_selection_update_on_next_scroll();
// Since muted streams aren't counted in visible unread
// counts, we need to update the rendering of them.
unread_ui.update_unread_counts();
}, 0);
// Since muted streams aren't counted in visible unread
// counts, we need to update the rendering of them.
unread_ui.update_unread_counts();
settings_notifications.update_muted_stream_state(sub);
stream_edit.update_muting_rendering(sub);