search: Extract people.huddle_concat().

This is a pure code extraction.  The current
code is buggy with respect to user_ids with
different lengths of digits, i.e. it does
a naive lexical sort instead of a numerical
sort.  We'll fix that in the next commit.
This commit is contained in:
Steve Howell 2020-05-26 16:46:23 +00:00 committed by Tim Abbott
parent be831e0085
commit 4803a12416
3 changed files with 40 additions and 2 deletions

View File

@ -538,6 +538,33 @@ run_test('multi_user_methods', () => {
initialize();
run_test('concat_huddle', () => {
/*
We assume that user_ids passed in
to concat_huddle have already been
validated, so we don't need actual
people for these tests to pass.
That may change in the future.
*/
const user_ids = [303, 301, 302];
assert.equal(
people.concat_huddle(user_ids, 304),
'301,302,303,304'
);
// This is broken, but reflects the current
// state of the code. We need a numerical sort.
assert.equal(
people.concat_huddle(user_ids, 99),
'301,302,303,99'
);
});
initialize();
run_test('message_methods', () => {
people.add(charles);
people.add(maria);

View File

@ -352,6 +352,17 @@ function sorted_other_user_ids(user_ids) {
return user_ids;
}
exports.concat_huddle = function (user_ids, user_id) {
/*
We assume user_ids and user_id have already
been validated by the caller.
The only logic we're encapsulating here is
how to encode huddles.
*/
return user_ids.concat(user_id).sort().join(',');
};
exports.pm_lookup_key = function (user_ids_string) {
/*
The server will sometimes include our own user id

View File

@ -60,8 +60,8 @@ function compare_by_huddle(huddle) {
}
return function (person1, person2) {
const huddle1 = huddle.concat(person1.user_id).sort().join(',');
const huddle2 = huddle.concat(person2.user_id).sort().join(',');
const huddle1 = people.concat_huddle(huddle, person1.user_id);
const huddle2 = people.concat_huddle(huddle, person2.user_id);
// If not in the dict, assign an arbitrarily high index
const score1 = huddle_dict.get(huddle1) || huddles.length + 1;