compose_box: Prepopulate stream if possible.

When users are only subbed to a single stream, this autofills the stream
field of the compose box.
Fixes #12507.
This commit is contained in:
YashRE42 2019-07-27 11:42:14 +05:30 committed by Tim Abbott
parent 997fd714d1
commit 7a6f4630dc
2 changed files with 52 additions and 0 deletions

View File

@ -21,6 +21,7 @@ zrequire('compose');
zrequire('util');
zrequire('compose_state');
zrequire('compose_actions');
zrequire('stream_data');
set_global('document', 'document-stub');
@ -137,6 +138,52 @@ run_test('start', () => {
assert.equal(compose_state.get_message_type(), 'stream');
assert(compose_state.composing());
// Autofill stream field for single subscription
var denmark = {
subscribed: true,
color: 'blue',
name: 'Denmark',
stream_id: 1,
};
stream_data.add_sub('Denmark', denmark);
global.narrow_state.set_compose_defaults = function () {
var opts = {};
opts.trigger = "new topic button";
return opts;
};
opts = {};
start('stream', opts);
assert.equal($('#stream_message_recipient_stream').val(), 'Denmark');
assert.equal($('#stream_message_recipient_topic').val(), '');
global.narrow_state.set_compose_defaults = function () {
var opts = {};
opts.trigger = "compose_hotkey";
return opts;
};
opts = {};
start('stream', opts);
assert.equal($('#stream_message_recipient_stream').val(), 'Denmark');
assert.equal($('#stream_message_recipient_topic').val(), '');
var social = {
subscribed: true,
color: 'red',
name: 'social',
stream_id: 2,
};
stream_data.add_sub('social', social);
// More than 1 subscription, do not autofill
opts = {};
start('stream', opts);
assert.equal($('#stream_message_recipient_stream').val(), '');
assert.equal($('#stream_message_recipient_topic').val(), '');
stream_data.clear_subscriptions();
// Start PM
global.narrow_state.set_compose_defaults = function () {
var opts = {};

View File

@ -221,6 +221,11 @@ exports.start = function (msg_type, opts) {
opts.private_message_recipient = '';
}
var subbed_streams = stream_data.subscribed_subs();
if (subbed_streams.length === 1 && (opts.trigger === "new topic button" || opts.trigger === "compose_hotkey" && msg_type === "stream")) {
opts.stream = subbed_streams[0].name;
}
if (compose_state.composing() && !same_recipient_as_before(msg_type, opts)) {
// Clear the compose box if the existing message is to a different recipient
clear_box();