From f56d3807cc32f2843df1492b3c1e7be1e8f1ba5b Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Thu, 9 Feb 2017 18:18:57 -0800 Subject: [PATCH] Add people.update_email_in_reply_to() helper. This will be used for live updating. --- frontend_tests/node_tests/people.js | 28 ++++++++++++++++++++++++++ static/js/people.js | 31 +++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/frontend_tests/node_tests/people.js b/frontend_tests/node_tests/people.js index 04b852168b..8ccf120bf9 100644 --- a/frontend_tests/node_tests/people.js +++ b/frontend_tests/node_tests/people.js @@ -332,3 +332,31 @@ initialize(); assert(/Obsolete email.*FOO.*bar/.test(warning)); assert.equal(person.user_id, user_id); }()); + +initialize(); + +(function test_update_email_in_reply_to() { + var charles = { + email: 'charles@example.com', + user_id: 601, + full_name: 'Charles Dickens', + }; + var maria = { + email: 'athens@example.com', + user_id: 602, + full_name: 'Maria Athens', + }; + people.add(charles); + people.add(maria); + + var reply_to = ' charles@example.com, athens@example.com'; + assert.equal( + people.update_email_in_reply_to(reply_to, 9999, 'whatever'), + reply_to + ); + assert.equal( + people.update_email_in_reply_to(reply_to, maria.user_id, 'maria@example.com'), + 'charles@example.com,maria@example.com' + ); +}()); + diff --git a/static/js/people.js b/static/js/people.js index f346d181fa..602e3965d9 100644 --- a/static/js/people.js +++ b/static/js/people.js @@ -283,6 +283,37 @@ exports.pm_with_url = function (message) { return uri; }; +exports.update_email_in_reply_to = function (reply_to, user_id, new_email) { + // We try to replace an old email with a new email in a reply_to, + // but we try to avoid changing the reply_to if we don't have to, + // and we don't warn on any errors. + var emails = reply_to.split(','); + + var persons = _.map(emails, function (email) { + return people_dict.get(email.trim()); + }); + + if (!_.all(persons)) { + return reply_to; + } + + var needs_patch = _.any(persons, function (person) { + return person.user_id === user_id; + }); + + if (!needs_patch) { + return reply_to; + } + + emails = _.map(persons, function (person) { + if (person.user_id === user_id) { + return new_email; + } + return person.email; + }); + + return emails.join(','); +}; exports.pm_with_operand_ids = function (operand) { var emails = operand.split(',');