Skip to content

Commit

Permalink
bot settings: Load bots independently.
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
showell authored and timabbott committed May 11, 2020
1 parent f6b1176 commit 5c16bb9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
6 changes: 6 additions & 0 deletions frontend_tests/node_tests/bot_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 4 additions & 0 deletions static/js/bot_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion static/js/settings_sections.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down
23 changes: 14 additions & 9 deletions static/js/settings_users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;

0 comments on commit 5c16bb9

Please sign in to comment.