stream_data: Add get_invite_stream_data helper function.

This function unlike `invite_streams()` returns an array of objects having
various info (name, stream_id, invite_only, default_stream) related to
streams rather than an array of names of streams.
This commit is contained in:
Shubham Dhama 2019-01-10 22:27:35 +05:30 committed by Tim Abbott
parent a7fb7d1f5c
commit fbd73ba637
2 changed files with 65 additions and 0 deletions

View File

@ -745,3 +745,44 @@ run_test('edge_cases', () => {
// just make sure we don't explode
stream_data.sort_for_stream_settings(bad_stream_ids);
});
run_test('get_invite_stream_data', () => {
// add default stream
var orie = {
name: 'Orie',
stream_id: 320,
invite_only: false,
subscribed: true,
};
// clear all the data form stream_data, and people
stream_data.clear_subscriptions();
people.init();
stream_data.add_sub('Orie', orie);
stream_data.set_realm_default_streams([orie]);
var expected_list = [{
name: 'Orie',
stream_id: 320,
invite_only: false,
default_stream: true,
}];
assert.deepEqual(stream_data.get_invite_stream_data(), expected_list);
var inviter = {
name: 'Inviter',
stream_id: 25,
invite_only: true,
subscribed: true,
};
stream_data.add_sub('Inviter', inviter);
expected_list.push({
name: 'Inviter',
stream_id: 25,
invite_only: true,
default_stream: false,
});
assert.deepEqual(stream_data.get_invite_stream_data(), expected_list);
});

View File

@ -187,6 +187,30 @@ exports.subscribed_streams = function () {
return _.pluck(exports.subscribed_subs(), 'name');
};
exports.get_invite_stream_data = function () {
var filter_stream_data = function (sub) {
return {
name: sub.name,
stream_id: sub.stream_id,
invite_only: sub.invite_only,
default_stream: stream_data.get_default_status(sub.name),
};
};
var invite_stream_data = _.map(stream_data.subscribed_subs(), filter_stream_data);
var default_stream_data = _.map(page_params.realm_default_streams, filter_stream_data);
// Since, union doesn't work on array of objects we are using filter
var is_included = {};
var streams = _.filter(default_stream_data.concat(invite_stream_data), function (sub) {
if (is_included[sub.name]) {
return false;
}
is_included[sub.name] = true;
return true;
});
return streams;
};
exports.invite_streams = function () {
var invite_list = exports.subscribed_streams();
var default_list = _.pluck(page_params.realm_default_streams, 'name');