diff --git a/frontend_tests/node_tests/util.js b/frontend_tests/node_tests/util.js index cd3634d61f..58868cfbbe 100644 --- a/frontend_tests/node_tests/util.js +++ b/frontend_tests/node_tests/util.js @@ -25,6 +25,11 @@ var _ = global._; }()); +(function test_extract_pm_recipients() { + assert.equal(util.extract_pm_recipients('bob@foo.com, alice@foo.com').length, 2); + assert.equal(util.extract_pm_recipients('bob@foo.com, ').length, 1); +}()); + (function test_lower_bound() { var arr = [10, 20, 30, 40, 50]; assert.equal(util.lower_bound(arr, 5), 0); diff --git a/static/js/echo.js b/static/js/echo.js index 1e78bbd75d..e9fa05f38b 100644 --- a/static/js/echo.js +++ b/static/js/echo.js @@ -146,8 +146,11 @@ function insert_local_message(message_request, local_id) { if (message.type === 'stream') { message.display_recipient = message.stream; } else { - // Build a display recipient with the full names of each recipient - var emails = message_request.private_message_recipient.split(','); + // Build a display recipient with the full names of each + // recipient. Note that it's important that use + // util.extract_pm_recipients, which filters out any spurious + // ", " at the end of the recipient list + var emails = util.extract_pm_recipients(message_request.private_message_recipient); message.display_recipient = _.map(emails, function (email) { email = email.trim(); var person = people.get_by_email(email); diff --git a/static/js/util.js b/static/js/util.js index 0f00040338..972a9d0ae3 100644 --- a/static/js/util.js +++ b/static/js/util.js @@ -70,7 +70,9 @@ exports.is_pm_recipient = function (email, message) { }; exports.extract_pm_recipients = function (recipients) { - return recipients.split(/\s*[,;]\s*/); + return _.filter(recipients.split(/\s*[,;]\s*/), function (recipient) { + return recipient.trim() !== ""; + }); }; exports.same_major_recipient = function (a, b) {