From 3f32ffc4eb1b5db7382ace1d45e64299bf017bb9 Mon Sep 17 00:00:00 2001 From: Priyank Patel Date: Thu, 23 May 2019 20:18:58 +0000 Subject: [PATCH] compose: Use new ID-based api for sending messages. This only happens if the realm is not a zephyr realm. Finishes part of #9474. --- frontend_tests/node_tests/compose.js | 12 +++++++++--- static/js/compose.js | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/frontend_tests/node_tests/compose.js b/frontend_tests/node_tests/compose.js index 0ada5a41b7..1e3544bd57 100644 --- a/frontend_tests/node_tests/compose.js +++ b/frontend_tests/node_tests/compose.js @@ -607,13 +607,14 @@ run_test('send_message', () => { queue_id: undefined, stream: '', topic: '', - to: '["alice@example.com"]', + to: `[${alice.user_id}]`, reply_to: 'alice@example.com', private_message_recipient: 'alice@example.com', to_user_ids: '31', local_id: 1, locally_echoed: true, }; + assert.deepEqual(payload, single_msg); payload.id = stub_state.local_id_counter; success(payload); @@ -1672,14 +1673,19 @@ run_test('create_message_object', () => { return 'private'; }; compose_state.recipient = function () { - return 'alice@example.com, bob@example.com'; + return 'alice@example.com, bob@example.com'; }; message = compose.create_message_object(); - assert.deepEqual(message.to, ['alice@example.com', 'bob@example.com']); + assert.deepEqual(message.to, [alice.user_id, bob.user_id]); assert.equal(message.to_user_ids, '31,32'); assert.equal(message.content, 'burrito'); + var { email_list_to_user_ids_string } = people; + people.email_list_to_user_ids_string = () => undefined; + message = compose.create_message_object(); + assert.deepEqual(message.to, [alice.email, bob.email]); + people.email_list_to_user_ids_string = email_list_to_user_ids_string; }); run_test('nonexistent_stream_reply_error', () => { diff --git a/static/js/compose.js b/static/js/compose.js index 171c2a55da..9e5dabb6d1 100644 --- a/static/js/compose.js +++ b/static/js/compose.js @@ -179,6 +179,20 @@ function create_message_object() { message.reply_to = recipient; message.private_message_recipient = recipient; message.to_user_ids = people.email_list_to_user_ids_string(emails); + + // Note: The `undefined` case is for situations like the + // is_zephyr_mirror_realm case where users may be + // automatically created when you try to send a private + // message to their email address. + if (message.to_user_ids !== undefined) { + // to_user_ids is a string containing a comma-separated + // list of user IDs for the recipients; convert this into + // an array of integers. + message.to = _.map(message.to_user_ids.split(','), function (id) { + return Number(id); + }); + } + } else { var stream_name = compose_state.stream_name(); message.to = stream_name;