Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
buddy_list: Fetch all subscribers before showing search results.
Evy Kassirer0563a36b47d9
diff --git a/web/src/activity_ui.ts b/web/src/activity_ui.tsindex e05c6e64bff..cfcddbddcc8 100644--- a/web/src/activity_ui.ts+++ b/web/src/activity_ui.ts@@ -8,7 +8,9 @@ import * as buddy_data from "./buddy_data.ts";import {buddy_list} from "./buddy_list.ts";import * as keydown_util from "./keydown_util.ts";import {ListCursor} from "./list_cursor.ts";+import * as loading from "./loading.ts";import * as narrow_state from "./narrow_state.ts";+import * as peer_data from "./peer_data.ts";import * as people from "./people.ts";import * as pm_list from "./pm_list.ts";import * as popovers from "./popovers.ts";@@ -137,13 +139,68 @@ export function rewire_build_user_sidebar(value: typeof build_user_sidebar): voibuild_user_sidebar = value;}+export function remove_loading_indicator_for_search(): void {+ loading.destroy_indicator($("#buddy-list-loading-subscribers"));+ $("#buddy_list_wrapper").show();+}++// We need to make sure we have all subscribers before displaying+// users during search, because we show all matching users and+// sort them by if they're subscribed. We store all pending fetches,+// in case we navigate away from a stream and back to it and kick+// off another search. We also store the current pending fetch so+// we know if it's still relevant once it's completed.+let pending_fetch_for_search_stream_id: number | undefined;+const all_pending_fetches_for_search = new Map<number, Promise<void>>();++export async function await_pending_promise_for_testing(): Promise<void> {+ assert(pending_fetch_for_search_stream_id !== undefined);+ await all_pending_fetches_for_search.get(pending_fetch_for_search_stream_id);+}+function do_update_users_for_search(): void {// Hide all the popovers but not userlist sidebar// when the user is searching.popovers.hide_all();- build_user_sidebar();- assert(user_cursor !== undefined);- user_cursor.reset();++ const stream_id = narrow_state.stream_id(narrow_state.filter(), true);+ if (!stream_id || peer_data.has_full_subscriber_data(stream_id)) {+ pending_fetch_for_search_stream_id = undefined;+ build_user_sidebar();+ assert(user_cursor !== undefined);+ user_cursor.reset();+ return;+ }++ pending_fetch_for_search_stream_id = stream_id;++ // If we're already fetching for this stream, we don't need to wait for+ // another promise. The sidebar will be updated once that promise resolves.+ if (all_pending_fetches_for_search.has(stream_id)) {+ return;+ }++ all_pending_fetches_for_search.set(+ stream_id,+ (async () => {+ $("#buddy_list_wrapper").hide();+ loading.make_indicator($("#buddy-list-loading-subscribers"));+ await peer_data.maybe_fetch_stream_subscribers(stream_id);+ all_pending_fetches_for_search.delete(stream_id);++ // If we changed narrows during the fetch, don't rebuild the sidebar+ // anymore. Let the new narrow handle its own state. The loading indicator+ // should have already been removed on narrow change.+ if (pending_fetch_for_search_stream_id !== stream_id) {+ return;+ }+ remove_loading_indicator_for_search();+ pending_fetch_for_search_stream_id = undefined;+ build_user_sidebar();+ assert(user_cursor !== undefined);+ user_cursor.reset();+ })(),+ );}const update_users_for_search = _.throttle(do_update_users_for_search, 50);@@ -281,6 +338,7 @@ export function initiate_search(): void {export function clear_search(): void {if (user_filter) {user_filter.clear_search();+ remove_loading_indicator_for_search();}}diff --git a/web/src/buddy_list.ts b/web/src/buddy_list.tsindex caabd01341a..c6830aee862 100644--- a/web/src/buddy_list.ts+++ b/web/src/buddy_list.ts@@ -723,8 +723,9 @@ export class BuddyList extends BuddyListConf {// After the `await`, we might have changed to a different channel view.// If so, we shouldn't update the DOM anymore, and should let the newer `populate`- // call set things up with fresh data.- if (current_sub !== this.render_data.current_sub) {+ // call set things up with fresh data. We also want to make sure not to show+ // the links when searching users, which might have changed since fetching data.+ if (current_sub !== this.render_data.current_sub || buddy_data.get_is_searching_users()) {return;}diff --git a/web/styles/right_sidebar.css b/web/styles/right_sidebar.cssindex e6beee59641..44ea4a862b9 100644--- a/web/styles/right_sidebar.css+++ b/web/styles/right_sidebar.css@@ -646,3 +646,9 @@}}}++#buddy-list-loading-subscribers {+ margin: auto;+ padding-right: var(--width-simplebar-scroll-hover);+ padding-top: 30px;+}diff --git a/web/templates/right_sidebar.hbs b/web/templates/right_sidebar.hbsindex 00ff82e2da7..f0f0e4e254d 100644--- a/web/templates/right_sidebar.hbs+++ b/web/templates/right_sidebar.hbs@@ -34,6 +34,7 @@</a></div></div>+ <div id="buddy-list-loading-subscribers"></div></div></div></div>diff --git a/web/tests/user_search.test.cjs b/web/tests/user_search.test.cjsindex 95399a8d731..f532fe20457 100644--- a/web/tests/user_search.test.cjs+++ b/web/tests/user_search.test.cjs@@ -33,15 +33,19 @@ function mock_setTimeout() {});}+const channel = mock_esm("../src/channel");const popovers = mock_esm("../src/popovers");const presence = mock_esm("../src/presence");const sidebar_ui = mock_esm("../src/sidebar_ui");const activity_ui = zrequire("activity_ui");const buddy_data = zrequire("buddy_data");+const {Filter} = zrequire("../src/filter");+const message_lists = zrequire("message_lists");const muted_users = zrequire("muted_users");const people = zrequire("people");const {set_realm} = zrequire("state_data");+const stream_data = zrequire("stream_data");const realm = {};set_realm(realm);@@ -81,7 +85,7 @@ function test(label, f) {people.initialize_current_user(me.user_id);muted_users.set_muted_users([]);activity_ui.set_cursor_and_filter();- f(opts);+ return f(opts);});}@@ -95,10 +99,11 @@ function stub_buddy_list_empty_list_message_lengths() {$("#buddy-list-other-users .empty-list-message").length = 0;}-test("clear_search", ({override}) => {+test("clear_search with button", ({override}) => {override(presence, "get_status", () => "active");override(presence, "get_user_ids", () => all_user_ids);override(popovers, "hide_all", noop);+ $("#buddy-list-loading-subscribers").css = noop;stub_buddy_list_empty_list_message_lengths();@@ -119,6 +124,7 @@ test("clear_search", ({override}) => {test("clear_search", ({override}) => {override(realm, "realm_presence_disabled", true);+ $("#buddy-list-loading-subscribers").css = noop;override(popovers, "hide_all", noop);stub_buddy_list_empty_list_message_lengths();@@ -132,6 +138,69 @@ test("clear_search", ({override}) => {set_input_val("");});+test("fetch on search", async ({override}) => {+ override(presence, "get_user_ids", () => all_user_ids);+ override(presence, "get_status", () => "active");+ let populate_call_count = 0;+ override(fake_buddy_list, "populate", () => {+ populate_call_count += 1;+ });+ $("#buddy-list-loading-subscribers").css = noop;+ override(popovers, "hide_all", noop);+ stub_buddy_list_empty_list_message_lengths();++ const office = {stream_id: 23, name: "office", subscribed: true};+ stream_data.add_sub(office);+ message_lists.set_current({+ data: {+ filter: new Filter([{operator: "stream", operand: office.stream_id}]),+ },+ });+ let get_call_count = 0;+ channel.get = () => {+ get_call_count += 1;+ return {+ subscribers: [1, 2, 3, 4],+ };+ };+ // Only one fetch should happen.+ set_input_val("somevalu");+ set_input_val("somevalue");+ await activity_ui.await_pending_promise_for_testing();+ assert.equal(get_call_count, 1);+ assert.equal(populate_call_count, 1);++ // Now try updating the narrow and starting a new search, before the old search+ // is resolved. We should make two requests but only only update populate the+ // buddy list for the second fetch (the first fetch returns early).+ get_call_count = 0;+ populate_call_count = 0;+ const kitchen = {stream_id: 25, name: "kitchen", subscribed: true};+ stream_data.add_sub(kitchen);+ const living_room = {stream_id: 26, name: "living_room", subscribed: true};+ stream_data.add_sub(living_room);+ message_lists.set_current({+ data: {+ filter: new Filter([{operator: "stream", operand: kitchen.stream_id}]),+ },+ });+ set_input_val("somevalue");+ message_lists.set_current({+ data: {+ filter: new Filter([{operator: "stream", operand: living_room.stream_id}]),+ },+ });+ set_input_val("somevalue");+ await activity_ui.await_pending_promise_for_testing();+ assert.equal(get_call_count, 2);+ assert.equal(populate_call_count, 1);++ // We need to reset these because the unit tests aren't isolated from each other.+ message_lists.set_current(undefined);+ activity_ui.clear_search();+ set_input_val("");+});+test("blur search right", ({override}) => {override(sidebar_ui, "show_userlist_sidebar", noop);override(popovers, "hide_all", noop);