messages: Send stream_id for stream messages.

This saves a tiny bit of bandwidth, but more
importantly, it protects us against races for
stream name changes.  There's some argument that
if the user is thinking they're sending to
old_stream_name, and unbeknownst to them, the
stream has changed to new_stream_name, then we
should fail.  But I think 99% of the time the
user just wants the message to go that stream
despite any renames.

In order to verify the blueslip error, we
had to turn on error checking, which required
a tiny fix to a place where we left out
a stream_id for add_sub.
This commit is contained in:
Steve Howell 2020-02-07 20:18:20 +00:00 committed by Tim Abbott
parent 02511bff1c
commit 996d054fe9
2 changed files with 20 additions and 5 deletions

View File

@ -1,8 +1,6 @@
set_global('bridge', false);
set_global('blueslip', global.make_zblueslip({
error: false, // Ignore errors. We only check for warnings in this module.
}));
set_global('blueslip', global.make_zblueslip({}));
const noop = function () {};
@ -876,6 +874,7 @@ function test_raw_file_drop(raw_drop_func) {
run_test('warn_if_private_stream_is_linked', () => {
stream_data.add_sub(compose_state.stream_name(), {
subscribers: new LazySet([1, 2]),
stream_id: 99,
});
let denmark = {
@ -1660,7 +1659,15 @@ run_test('create_message_object', () => {
let message = compose.create_message_object();
assert.equal(message.to, 'social');
assert.equal(message.to, sub.stream_id);
assert.equal(message.topic, 'lunch');
assert.equal(message.content, 'burrito');
blueslip.set_test_data('error', 'Trying to send message with bad stream name: BOGUS STREAM');
page['#stream_message_recipient_stream'] = 'BOGUS STREAM';
message = compose.create_message_object();
assert.equal(message.to, 'BOGUS STREAM');
assert.equal(message.topic, 'lunch');
assert.equal(message.content, 'burrito');

View File

@ -195,11 +195,19 @@ function create_message_object() {
} else {
const stream_name = compose_state.stream_name();
message.to = stream_name;
message.stream = stream_name;
const sub = stream_data.get_sub(stream_name);
if (sub) {
message.stream_id = sub.stream_id;
message.to = sub.stream_id;
} else {
// We should be validating streams in calling code. We'll
// try to fall back to stream_name here just in case the
// user started composing to the old stream name and
// manually entered the stream name, and it got past
// validation. We should try to kill this code off eventually.
blueslip.error('Trying to send message with bad stream name: ' + stream_name);
message.to = stream_name;
}
util.set_message_topic(message, topic);
}