people: Clean up recip.id code.

When we are pulling data from message.display_recipient
for private messages, the user_id field is always
called 'id', not 'user_id', so we can simplify
some defensive code.
This commit is contained in:
Steve Howell 2020-01-01 12:02:34 +00:00 committed by Tim Abbott
parent 7630b859c3
commit bc5589c2a7
9 changed files with 48 additions and 33 deletions

View File

@ -541,25 +541,25 @@ run_test('predicate_basics', () => {
}));
assert(!predicate({
type: 'private',
display_recipient: [{user_id: steve.user_id}],
display_recipient: [{id: steve.user_id}],
}));
assert(!predicate({
type: 'private',
display_recipient: [{user_id: 999999}],
display_recipient: [{id: 999999}],
}));
assert(!predicate({type: 'stream'}));
predicate = get_predicate([['pm-with', 'Joe@example.com,steve@foo.com']]);
assert(predicate({
type: 'private',
display_recipient: [{user_id: joe.user_id}, {user_id: steve.user_id}],
display_recipient: [{id: joe.user_id}, {id: steve.user_id}],
}));
// Make sure your own email is ignored
predicate = get_predicate([['pm-with', 'Joe@example.com,steve@foo.com,me@example.com']]);
assert(predicate({
type: 'private',
display_recipient: [{user_id: joe.user_id}, {user_id: steve.user_id}],
display_recipient: [{id: joe.user_id}, {id: steve.user_id}],
}));
predicate = get_predicate([['pm-with', 'nobody@example.com']]);

View File

@ -140,7 +140,7 @@ run_test('test_by_conversation_and_time_uri', () => {
type: 'private',
display_recipient: [
{
user_id: hamlet.user_id,
id: hamlet.user_id,
},
],
id: 43,

View File

@ -66,12 +66,23 @@ people.add_in_realm(denise);
global.people.initialize_current_user(me.user_id);
function convert_recipients(people) {
// Display_recipient uses `id` for user_ids.
return _.map(people, (p) => {
return {
email: p.email,
id: p.user_id,
full_name: p.full_name,
};
});
}
run_test('add_message_metadata', () => {
let message = {
sender_email: 'me@example.com',
sender_id: me.user_id,
type: 'private',
display_recipient: [me, bob, cindy],
display_recipient: convert_recipients([me, bob, cindy]),
flags: ['has_alert_word'],
is_me_message: false,
id: 2067,
@ -172,7 +183,7 @@ run_test('errors', () => {
// Test a user that doesn't exist
let message = {
type: 'private',
display_recipient: [{user_id: 92714}],
display_recipient: [{id: 92714}],
};
blueslip.set_test_data('error', 'Unknown user_id in get_person_from_user_id: 92714');
@ -254,7 +265,7 @@ run_test('message_id_change', () => {
sender_email: 'me@example.com',
sender_id: me.user_id,
type: 'private',
display_recipient: [me, bob, cindy],
display_recipient: convert_recipients([me, bob, cindy]),
flags: ['has_alert_word'],
id: 401,
};

View File

@ -62,7 +62,7 @@ run_test('get_unread_ids', () => {
type: 'private',
unread: true,
display_recipient: [
{user_id: alice.user_id},
{id: alice.user_id},
],
};

View File

@ -465,7 +465,7 @@ run_test('message_methods', () => {
display_recipient: [
{id: maria.user_id},
{id: me.user_id},
{user_id: charles.user_id},
{id: charles.user_id},
],
sender_id: charles.user_id,
};
@ -480,7 +480,7 @@ run_test('message_methods', () => {
type: 'private',
display_recipient: [
{id: maria.user_id},
{user_id: me.user_id},
{id: me.user_id},
],
avatar_url: 'legacy.png',
};
@ -511,7 +511,7 @@ run_test('message_methods', () => {
message = {
type: 'private',
display_recipient: [
{user_id: me.user_id},
{id: me.user_id},
],
};
assert.equal(people.pm_with_url(message), '#narrow/pm-with/30-me');
@ -621,17 +621,14 @@ run_test('maybe_incr_recipient_count', () => {
user_id: 452,
full_name: 'Maria Athens',
};
people.add_in_realm(maria);
const unknown_user = {
email: 'unknown@example.com',
user_id: 500,
unknown_local_echo_user: true,
const maria_recip = {
id: maria.user_id,
};
people.add_in_realm(maria);
let message = {
type: 'private',
display_recipient: [maria],
display_recipient: [maria_recip],
sent_by_me: true,
};
assert.equal(people.get_recipient_count(maria), 0);
@ -643,15 +640,21 @@ run_test('maybe_incr_recipient_count', () => {
message = {
type: 'private',
sent_by_me: false,
display_recipient: [maria],
display_recipient: [maria_recip],
};
people.maybe_incr_recipient_count(message);
assert.equal(people.get_recipient_count(maria), 1);
const unknown_recip = {
email: 'unknown@example.com',
id: 500,
unknown_local_echo_user: true,
};
message = {
type: 'private',
sent_by_me: true,
display_recipient: [unknown_user],
display_recipient: [unknown_recip],
};
people.maybe_incr_recipient_count(message);
assert.equal(people.get_recipient_count(maria), 1);

View File

@ -106,7 +106,7 @@ run_test('blueslip', () => {
display_recipient: [
{id: maria.user_id},
{id: 42},
{user_id: charles.user_id},
{id: charles.user_id},
],
sender_id: charles.user_id,
};

View File

@ -224,7 +224,7 @@ run_test('reply_message_private', () => {
const pm_message = {
type: 'private',
display_recipient: [
{user_id: fred.user_id},
{id: fred.user_id},
],
};

View File

@ -408,7 +408,7 @@ run_test('private_messages', () => {
id: 15,
type: 'private',
display_recipient: [
{user_id: anybody.user_id},
{id: anybody.user_id},
{id: me.user_id},
],
unread: true,

View File

@ -386,8 +386,8 @@ exports.all_user_ids_in_pm = function (message) {
return;
}
let user_ids = _.map(message.display_recipient, function (elem) {
return elem.user_id || elem.id;
let user_ids = _.map(message.display_recipient, function (recip) {
return recip.id;
});
user_ids = sort_numerically(user_ids);
@ -404,8 +404,8 @@ exports.pm_with_user_ids = function (message) {
return;
}
const user_ids = _.map(message.display_recipient, function (elem) {
return elem.user_id || elem.id;
const user_ids = _.map(message.display_recipient, function (recip) {
return recip.id;
});
return sorted_other_user_ids(user_ids);
@ -420,8 +420,9 @@ exports.group_pm_with_user_ids = function (message) {
blueslip.error('Empty recipient list in message');
return;
}
const user_ids = _.map(message.display_recipient, function (elem) {
return elem.user_id || elem.id;
const user_ids = _.map(message.display_recipient, function (recip) {
return recip.id;
});
const is_user_present = _.some(user_ids, function (user_id) {
return exports.is_my_user_id(user_id);
@ -1047,13 +1048,13 @@ exports.maybe_incr_recipient_count = function (message) {
}
// Track the number of PMs we've sent to this person to improve autocomplete
_.each(message.display_recipient, function (person) {
_.each(message.display_recipient, function (recip) {
if (person.unknown_local_echo_user) {
if (recip.unknown_local_echo_user) {
return;
}
const user_id = person.user_id || person.id;
const user_id = recip.id;
exports.incr_recipient_count(user_id);
});
};