Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

refactor: Extract method to create and update message lists in narrow.

roanster0073b73f19719e0
diff --git a/web/src/narrow.js b/web/src/narrow.js
index c9567b1175f..69d96a112ce 100644
--- a/web/src/narrow.js
+++ b/web/src/narrow.js
@@ -94,6 +94,83 @@ export function update_hash_to_match_filter(filter, trigger) {
changehash(new_hash, trigger);
}
+function create_and_update_message_list(filter, id_info, opts) {
+ const excludes_muted_topics = filter.excludes_muted_topics();
+
+ // Check if we already have a rendered message list for the `filter`.
+ // TODO: If we add a message list other than `is_in_home` to be save as rendered,
+ // we need to add a `is_equal` function to `Filter` to compare the filters.
+ let msg_list;
+ let restore_rendered_list = false;
+ for (const list of message_lists.all_rendered_message_lists()) {
+ if (filter.is_in_home() && list.preserve_rendered_state) {
+ assert(list.data.filter.is_in_home());
+ msg_list = list;
+ restore_rendered_list = true;
+ break;
+ }
+ }
+
+ if (!restore_rendered_list) {
+ let msg_data = new MessageListData({
+ filter,
+ excludes_muted_topics,
+ });
+
+ // Populate the message list if we can apply our filter locally (i.e.
+ // with no backend help) and we have the message we want to select.
+ // Also update id_info accordingly.
+ maybe_add_local_messages({
+ id_info,
+ msg_data,
+ });
+
+ if (!id_info.local_select_id) {
+ // If we're not actually ready to select an ID, we need to
+ // trash the `MessageListData` object that we just constructed
+ // and pass an empty one to MessageList, because the block of
+ // messages in the MessageListData built inside
+ // maybe_add_local_messages is likely not be contiguous with
+ // the block we're about to request from the server instead.
+ msg_data = new MessageListData({
+ filter,
+ excludes_muted_topics,
+ });
+ }
+
+ msg_list = new message_list.MessageList({
+ data: msg_data,
+ });
+ }
+ assert(msg_list !== undefined);
+
+ // Put the narrow terms in the URL fragment/hash.
+ //
+ // opts.change_hash will be false when the URL fragment was
+ // the source of this narrow, and the fragment was not a link to
+ // a specific target message ID that has been moved.
+ //
+ // This needs to be called at the same time as updating the
+ // current message list so that we don't need to think about
+ // bugs related to the URL fragment/hash being desynced from
+ // message_lists.current.
+ //
+ // It's fine for the hash change to happen anytime before updating
+ // the current message list as we are trying to emulate the `hashchange`
+ // workflow we have which calls `narrow.activate` after hash is updated.
+ if (opts.change_hash) {
+ update_hash_to_match_filter(filter, opts.trigger);
+ }
+
+ // Show the new set of messages. It is important to set message_lists.current to
+ // the view right as it's being shown, because we rely on message_lists.current
+ // being shown for deciding when to condense messages.
+ // From here on down, any calls to the narrow_state API will
+ // reflect the requested narrow.
+ message_lists.update_current_message_list(msg_list);
+ return {msg_list, restore_rendered_list};
+}
+
export function activate(raw_terms, opts) {
/* Main entry point for switching to a new view / message list.
@@ -417,79 +494,11 @@ export function activate(raw_terms, opts) {
}
}
- const excludes_muted_topics = filter.excludes_muted_topics();
-
- // Check if we already have a rendered message list for the `filter`.
- // TODO: If we add a message list other than `is_in_home` to be save as rendered,
- // we need to add a `is_equal` function to `Filter` to compare the filters.
- let msg_list;
- let restore_rendered_list = false;
- for (const list of message_lists.all_rendered_message_lists()) {
- if (is_combined_feed_global_view && list.preserve_rendered_state) {
- assert(list.data.filter.is_in_home());
- msg_list = list;
- restore_rendered_list = true;
- break;
- }
- }
-
- if (!restore_rendered_list) {
- let msg_data = new MessageListData({
- filter,
- excludes_muted_topics,
- });
-
- // Populate the message list if we can apply our filter locally (i.e.
- // with no backend help) and we have the message we want to select.
- // Also update id_info accordingly.
- maybe_add_local_messages({
- id_info,
- msg_data,
- });
-
- if (!id_info.local_select_id) {
- // If we're not actually ready to select an ID, we need to
- // trash the `MessageListData` object that we just constructed
- // and pass an empty one to MessageList, because the block of
- // messages in the MessageListData built inside
- // maybe_add_local_messages is likely not be contiguous with
- // the block we're about to request from the server instead.
- msg_data = new MessageListData({
- filter,
- excludes_muted_topics,
- });
- }
-
- msg_list = new message_list.MessageList({
- data: msg_data,
- });
- }
- assert(msg_list !== undefined);
-
- // Put the narrow terms in the URL fragment/hash.
- //
- // opts.change_hash will be false when the URL fragment was
- // the source of this narrow, and the fragment was not a link to
- // a specific target message ID that has been moved.
- //
- // This needs to be called at the same time as updating the
- // current message list so that we don't need to think about
- // bugs related to the URL fragment/hash being desynced from
- // message_lists.current.
- //
- // It's fine for the hash change to happen anytime before updating
- // the current message list as we are trying to emulate the `hashchange`
- // workflow we have which calls `narrow.activate` after hash is updated.
- if (opts.change_hash) {
- update_hash_to_match_filter(filter, opts.trigger);
- }
-
- // Show the new set of messages. It is important to set message_lists.current to
- // the view right as it's being shown, because we rely on message_lists.current
- // being shown for deciding when to condense messages.
- // From here on down, any calls to the narrow_state API will
- // reflect the requested narrow.
- message_lists.update_current_message_list(msg_list);
+ const {msg_list, restore_rendered_list} = create_and_update_message_list(
+ filter,
+ id_info,
+ opts,
+ );
let select_immediately;
let select_opts;