From adf616d3f166d50687ba0c5942ad345808a1763d Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Thu, 18 Oct 2018 20:05:28 +0000 Subject: [PATCH] Add pm_perma_link helper. --- frontend_tests/node_tests/people.js | 4 +++ frontend_tests/node_tests/people_errors.js | 4 ++- static/js/people.js | 38 ++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/frontend_tests/node_tests/people.js b/frontend_tests/node_tests/people.js index ec768bfabb..ef9b98a3e4 100644 --- a/frontend_tests/node_tests/people.js +++ b/frontend_tests/node_tests/people.js @@ -403,6 +403,7 @@ run_test('message_methods', () => { sender_id: charles.user_id, }; assert.equal(people.pm_with_url(message), '#narrow/pm-with/451,452-group'); + assert.equal(people.pm_perma_link(message), '#narrow/pm-with/30,451,452-group'); assert.equal(people.pm_reply_to(message), 'Athens@example.com,charles@example.com'); assert.equal(people.small_avatar_url(message), @@ -417,6 +418,7 @@ run_test('message_methods', () => { avatar_url: 'legacy.png', }; assert.equal(people.pm_with_url(message), '#narrow/pm-with/452-athens'); + assert.equal(people.pm_perma_link(message), '#narrow/pm-with/30,452-pm'); assert.equal(people.pm_reply_to(message), 'Athens@example.com'); assert.equal(people.small_avatar_url(message), @@ -446,9 +448,11 @@ run_test('message_methods', () => { ], }; assert.equal(people.pm_with_url(message), '#narrow/pm-with/30-me'); + assert.equal(people.pm_perma_link(message), '#narrow/pm-with/30-pm'); message = { type: 'stream' }; assert.equal(people.pm_with_user_ids(message), undefined); + assert.equal(people.all_user_ids_in_pm(message), undefined); // Test undefined user_ids assert.equal(people.pm_reply_to(message), undefined); diff --git a/frontend_tests/node_tests/people_errors.js b/frontend_tests/node_tests/people_errors.js index 027f726399..ba12b0b0a5 100644 --- a/frontend_tests/node_tests/people_errors.js +++ b/frontend_tests/node_tests/people_errors.js @@ -82,7 +82,9 @@ run_test('blueslip', () => { blueslip.set_test_data('error', 'Empty recipient list in message'); people.pm_with_user_ids(message); people.group_pm_with_user_ids(message); - assert.equal(blueslip.get_test_logs('error').length, 2); + people.all_user_ids_in_pm(message); + assert.equal(people.pm_perma_link(message), undefined); + assert.equal(blueslip.get_test_logs('error').length, 4); blueslip.clear_test_data(); var charles = { diff --git a/static/js/people.js b/static/js/people.js index 062a4d40f0..3e41a50b56 100644 --- a/static/js/people.js +++ b/static/js/people.js @@ -341,6 +341,24 @@ exports.pm_lookup_key = function (user_ids_string) { return user_ids.join(','); }; +exports.all_user_ids_in_pm = function (message) { + if (message.type !== 'private') { + return; + } + + if (message.display_recipient.length === 0) { + blueslip.error('Empty recipient list in message'); + return; + } + + var user_ids = _.map(message.display_recipient, function (elem) { + return elem.user_id || elem.id; + }); + + user_ids = sort_numerically(user_ids); + return user_ids; +}; + exports.pm_with_user_ids = function (message) { if (message.type !== 'private') { return; @@ -382,6 +400,26 @@ exports.group_pm_with_user_ids = function (message) { return false; }; +exports.pm_perma_link = function (message) { + var user_ids = exports.all_user_ids_in_pm(message); + + if (!user_ids) { + return; + } + + var suffix; + + if (user_ids.length >= 3) { + suffix = 'group'; + } else { + suffix = 'pm'; + } + + var slug = user_ids.join(',') + '-' + suffix; + var uri = "#narrow/pm-with/" + slug; + return uri; +}; + exports.pm_with_url = function (message) { var user_ids = exports.pm_with_user_ids(message);