refactor: Simplify get_invite_stream_data().

This code is a bit simpler.

The previous code was concatenating two lists
and then removing duplicates by calling filter().

Now we just have two loops that append to a single
list, and the second loop detects duplicates
before inserting into the list.

We also now use `default_stream_ids` instead of
`page_params` data, which is convenient for two
reasons:

    - working with sets of ids is convenient
    - we don't need to maintain `page_params`
      data any more
This commit is contained in:
Steve Howell
2020-03-22 16:31:47 +00:00
committed by Tim Abbott
parent 6313917143
commit ba495e57eb

View File

@@ -333,26 +333,30 @@ exports.subscribed_streams = function () {
}; };
exports.get_invite_stream_data = function () { exports.get_invite_stream_data = function () {
const filter_stream_data = function (sub) { function get_data(sub) {
return { return {
name: sub.name, name: sub.name,
stream_id: sub.stream_id, stream_id: sub.stream_id,
invite_only: sub.invite_only, invite_only: sub.invite_only,
default_stream: default_stream_ids.has(sub.stream_id), default_stream: default_stream_ids.has(sub.stream_id),
}; };
}; }
const invite_stream_data = exports.subscribed_subs().map(filter_stream_data);
const default_stream_data = page_params.realm_default_streams.map(filter_stream_data);
// Since, union doesn't work on array of objects we are using filter const streams = [];
const is_included = new Set();
const streams = default_stream_data.concat(invite_stream_data).filter(sub => { // Invite users to all default streams...
if (is_included.has(sub.name)) { for (const stream_id of default_stream_ids) {
return false; const sub = subs_by_stream_id.get(stream_id);
streams.push(get_data(sub));
}
// ...plus all your subscribed streams (avoiding repeats).
for (const sub of exports.subscribed_subs()) {
if (!default_stream_ids.has(sub.stream_id)) {
streams.push(get_data(sub));
} }
is_included.add(sub.name); }
return true;
});
return streams; return streams;
}; };