people.js: Change functions to return a list of ids instead of objects.

The get_active_humans and get_non_active_humans functions used
to return a list of user objects. The get_active_humans is used
on settings_users.js and settings_bots.js, and in both places the
only attributes needed of the person object are the user_id and
full_name.

To make the function return smaller, instead of a list of active
humans, we are returning a list of active human ids, saving memory.
With the ids we can call the people API to get the full_name attribute.
This commit is contained in:
Clara Dantas
2020-05-29 23:21:15 -03:00
committed by Steve Howell
parent 99bed7b702
commit e1e755c887
4 changed files with 25 additions and 16 deletions

View File

@@ -208,7 +208,7 @@ run_test('basics', () => {
realm_persons = people.get_realm_users(); realm_persons = people.get_realm_users();
assert.equal(realm_persons.length, 1); assert.equal(realm_persons.length, 1);
assert.equal(people.get_active_human_count(), 1); assert.equal(people.get_active_human_ids().length, 1);
const full_name = 'Isaac Newton'; const full_name = 'Isaac Newton';
const email = 'isaac@example.com'; const email = 'isaac@example.com';
@@ -251,7 +251,7 @@ run_test('basics', () => {
people.deactivate(isaac); people.deactivate(isaac);
person = people.get_active_user_for_email(email); person = people.get_active_user_for_email(email);
assert(!person); assert(!person);
assert.equal(people.get_non_active_humans().length, 1); assert.equal(people.get_non_active_human_ids().length, 1);
assert.equal(people.get_active_human_count(), 1); assert.equal(people.get_active_human_count(), 1);
assert.equal(people.is_active_user_for_popover(isaac.user_id), false); assert.equal(people.is_active_user_for_popover(isaac.user_id), false);
assert.equal(people.is_valid_email_for_compose(isaac.email), false); assert.equal(people.is_valid_email_for_compose(isaac.email), false);
@@ -310,11 +310,11 @@ run_test('basics', () => {
// Reactivating issac // Reactivating issac
people.add_active_user(isaac); people.add_active_user(isaac);
const active_humans = people.get_active_humans(); const active_humans = people.get_active_human_ids();
assert.equal(active_humans.length, 2); assert.equal(active_humans.length, 2);
assert.deepEqual( assert.deepEqual(
active_humans.sort((p) => p.user_id), active_humans.sort((p) => p.user_id),
[me, isaac]); [me.user_id, isaac.user_id]);
}); });
run_test('check_active_non_active_users', () => { run_test('check_active_non_active_users', () => {

View File

@@ -723,28 +723,28 @@ exports.get_realm_users = function () {
return Array.from(active_user_dict.values()); return Array.from(active_user_dict.values());
}; };
exports.get_active_humans = function () { exports.get_active_human_ids = function () {
const humans = []; const human_ids = [];
for (const user of active_user_dict.values()) { for (const user of active_user_dict.values()) {
if (!user.is_bot) { if (!user.is_bot) {
humans.push(user); human_ids.push(user.user_id);
} }
} }
return humans; return human_ids;
}; };
exports.get_non_active_humans = function () { exports.get_non_active_human_ids = function () {
const humans = []; const human_ids = [];
for (const user of non_active_user_dict.values()) { for (const user of non_active_user_dict.values()) {
if (!user.is_bot) { if (!user.is_bot) {
humans.push(user); human_ids.push(user.user_id);
} }
} }
return humans; return human_ids;
}; };
exports.get_active_human_count = function () { exports.get_active_human_count = function () {

View File

@@ -391,7 +391,12 @@ exports.set_up = function () {
const li = $(e.currentTarget).closest('li'); const li = $(e.currentTarget).closest('li');
const bot_id = parseInt(li.find('.bot_info').attr('data-user-id'), 10); const bot_id = parseInt(li.find('.bot_info').attr('data-user-id'), 10);
const bot = bot_data.get(bot_id); const bot = bot_data.get(bot_id);
const users_list = people.get_active_humans(); const user_ids = people.get_active_human_ids();
const users_list = user_ids.map(user_id => ({
name: people.get_full_name(user_id),
value: user_id.toString(),
}));
$("#edit_bot_modal").empty(); $("#edit_bot_modal").empty();
$("#edit_bot_modal").append(render_edit_bot({ $("#edit_bot_modal").append(render_edit_bot({
bot: bot, bot: bot,
@@ -404,7 +409,7 @@ exports.set_up = function () {
const opts = { const opts = {
widget_name: 'bot_owner', widget_name: 'bot_owner',
data: users_list.map(u => ({name: u.full_name, value: u.user_id.toString()})), data: users_list,
default_text: i18n.t("No owner"), default_text: i18n.t("No owner"),
value: bot.owner_id, value: bot.owner_id,
}; };

View File

@@ -464,10 +464,14 @@ function open_bot_form(person) {
// NOTE: building `owner_dropdown` is quite expensive! // NOTE: building `owner_dropdown` is quite expensive!
const owner_id = bot_data.get(person.user_id).owner_id; const owner_id = bot_data.get(person.user_id).owner_id;
const users_list = people.get_active_humans(); const user_ids = people.get_active_human_ids();
const users_list = user_ids.map(user_id => ({
name: people.get_full_name(user_id),
value: user_id.toString(),
}));
const opts = { const opts = {
widget_name: 'edit_bot_owner', widget_name: 'edit_bot_owner',
data: users_list.map(u => ({name: u.full_name, value: u.user_id.toString()})), data: users_list,
default_text: i18n.t("No owner"), default_text: i18n.t("No owner"),
value: owner_id, value: owner_id,
}; };