From 5c16bb9c990daad26d9d14b9e406c33aaeeed078 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sat, 9 May 2020 18:06:14 +0000 Subject: [PATCH] bot settings: Load bots independently. We no longer use `/json/users` in the codepath for bot settings (admin side). We also specifically don't load human users when we load bots, so you no longer have to pay for the server round trip as a side effect of loading bots. Instead, there is a dedicated `set_up_bots` entry point. We also get the bot ids directly from `bot_data` now. This commit, to some degree, builds on the prior commit that had us hydrate data from `people.js` instead of the payload from `/json/users`. --- frontend_tests/node_tests/bot_data.js | 6 ++++++ static/js/bot_data.js | 4 ++++ static/js/settings_sections.js | 5 ++++- static/js/settings_users.js | 23 ++++++++++++++--------- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/frontend_tests/node_tests/bot_data.js b/frontend_tests/node_tests/bot_data.js index e7195deaa46f3..683676825d9d3 100644 --- a/frontend_tests/node_tests/bot_data.js +++ b/frontend_tests/node_tests/bot_data.js @@ -113,6 +113,12 @@ run_test('test_basics', () => { assert.equal(bot.is_active, false); }()); + (function test_all_user_ids() { + const all_ids = bot_data.all_user_ids(); + all_ids.sort(); + assert.deepEqual(all_ids, [143, 314, 42, 43]); + }()); + (function test_delete() { let bot; diff --git a/static/js/bot_data.js b/static/js/bot_data.js index ea19d30263270..992316f09560d 100644 --- a/static/js/bot_data.js +++ b/static/js/bot_data.js @@ -23,6 +23,10 @@ const send_change_event = _.debounce(function () { settings_bots.render_bots(); }, 50); +exports.all_user_ids = function () { + return Array.from(bots.keys()); +}; + exports.add = function (bot) { const clean_bot = _.pick(bot, bot_fields); bots.set(bot.user_id, clean_bot); diff --git a/static/js/settings_sections.js b/static/js/settings_sections.js index 48d05a5cd3808..f451b670421bb 100644 --- a/static/js/settings_sections.js +++ b/static/js/settings_sections.js @@ -12,6 +12,8 @@ exports.get_group = function (section) { return 'org_misc'; case 'bot-list-admin': + return 'org_bots'; + case 'user-list-admin': case 'deactivated-users-admin': return 'org_users'; @@ -33,7 +35,8 @@ exports.initialize = function () { // org load_func_dict.set('org_misc', settings_org.set_up); - load_func_dict.set('org_users', settings_users.set_up); + load_func_dict.set('org_bots', settings_users.set_up_bots); + load_func_dict.set('org_users', settings_users.set_up_humans); load_func_dict.set('emoji-settings', settings_emoji.set_up); load_func_dict.set('default-streams-list', settings_streams.set_up); load_func_dict.set('filter-settings', settings_linkifiers.set_up); diff --git a/static/js/settings_users.js b/static/js/settings_users.js index ea36679eed5a8..426284931f07c 100644 --- a/static/js/settings_users.js +++ b/static/js/settings_users.js @@ -120,17 +120,19 @@ function get_status_field() { function failed_listing_users(xhr) { loading.destroy_indicator($('#subs_page_loading_indicator')); const status = get_status_field(); - ui_report.error(i18n.t("Error listing users or bots"), xhr, status); + ui_report.error(i18n.t("Error listing users"), xhr, status); } function populate_users(realm_people_data) { let active_users = []; let deactivated_users = []; - let bots = []; + for (const user of realm_people_data.members) { if (user.is_bot) { - bots.push(user); - } else if (user.is_active) { + continue; + } + + if (user.is_active) { active_users.push(user); } else { deactivated_users.push(user); @@ -139,11 +141,9 @@ function populate_users(realm_people_data) { active_users = _.sortBy(active_users, 'full_name'); deactivated_users = _.sortBy(deactivated_users, 'full_name'); - bots = _.sortBy(bots, 'full_name'); section.active.create_table(active_users); section.deactivated.create_table(deactivated_users); - section.bots.create_table(bots); } function reset_scrollbar($sel) { @@ -237,9 +237,10 @@ function human_info(person) { return info; } -section.bots.create_table = (bots) => { +section.bots.create_table = () => { const $bots_table = $("#admin_bots_table"); - const bot_user_ids = bots.map(b => b.user_id); + const bot_user_ids = bot_data.all_user_ids(); + list_render.create($bots_table, bot_user_ids, { name: "admin_bot_list", get_item: bot_info, @@ -660,11 +661,15 @@ section.bots.handle_events = () => { handle_bot_form(tbody, status_field); }; -exports.set_up = function () { +exports.set_up_humans = function () { start_data_load(); section.active.handle_events(); section.deactivated.handle_events(); +}; + +exports.set_up_bots = function () { section.bots.handle_events(); + section.bots.create_table(); }; window.settings_users = exports;