people.js: Add get_mention_syntax to conditionally get @user|id syntax.

This is intended to replace all function calls for generating mention
syntax for a target user.
This commit is contained in:
Rohitt Vashishtha 2018-10-13 00:25:38 +00:00 committed by Tim Abbott
parent 608173657d
commit b18f9def06
2 changed files with 41 additions and 0 deletions

View File

@ -698,6 +698,35 @@ run_test('track_duplicate_full_names', () => {
assert(!people.is_duplicate_full_name('Stephen King JP')); assert(!people.is_duplicate_full_name('Stephen King JP'));
}); });
run_test('track_duplicate_full_names', () => {
var stephen1 = {
email: 'stephen-the-author@example.com',
user_id: 601,
full_name: 'Stephen King',
};
var stephen2 = {
email: 'stephen-the-explorer@example.com',
user_id: 602,
full_name: 'Stephen King',
};
var maria = {
email: 'athens@example.com',
user_id: 603,
full_name: 'Maria Athens',
};
people.add(stephen1);
people.add(stephen2);
people.add(maria);
blueslip.set_test_data('warn', 'get_mention_syntax called without user_id.');
assert.equal(people.get_mention_syntax('Stephen King'), '@**Stephen King**');
assert.equal(blueslip.get_test_logs('warn').length, 1);
blueslip.clear_test_data();
assert.equal(people.get_mention_syntax('Stephen King', 601), '@**Stephen King|601**');
assert.equal(people.get_mention_syntax('Stephen King', 602), '@**Stephen King|602**');
assert.equal(people.get_mention_syntax('Maria Athens', 603), '@**Maria Athens**');
});
run_test('initialize', () => { run_test('initialize', () => {
people.init(); people.init();

View File

@ -773,6 +773,18 @@ exports.is_duplicate_full_name = function (full_name) {
return false; return false;
}; };
exports.get_mention_syntax = function (full_name, user_id) {
var mention = '@**' + full_name;
if (!user_id) {
blueslip.warn('get_mention_syntax called without user_id.');
}
if (people.is_duplicate_full_name(full_name) && user_id) {
mention += '|' + user_id;
}
mention += '**';
return mention;
};
exports.add = function add(person) { exports.add = function add(person) {
if (person.user_id) { if (person.user_id) {
people_by_user_id_dict.set(person.user_id, person); people_by_user_id_dict.set(person.user_id, person);