stream_data: Add get_subscriber_count() function.

This commit is contained in:
Aditya Bansal
2017-06-29 19:05:34 +05:30
committed by showell
parent cc34e3d382
commit 7531a85c4d
2 changed files with 41 additions and 0 deletions

View File

@@ -452,3 +452,32 @@ var people = global.people;
assert(!stream_data.get_sub('Canada'));
assert(!stream_data.get_sub_by_id(canada.stream_id));
}());
(function test_get_subscriber_count() {
var india = {
stream_id: 102,
name: 'India',
subscribed: true,
};
stream_data.clear_subscriptions();
assert.equal(stream_data.get_subscriber_count('India'), undefined);
stream_data.add_sub('India', india);
assert.equal(stream_data.get_subscriber_count('India'), 0);
var fred = {
email: 'fred@zulip.com',
full_name: 'Fred',
user_id: 101,
};
people.add(fred);
stream_data.add_subscriber('India', 102);
assert.equal(stream_data.get_subscriber_count('India'), 1);
var george = {
email: 'george@zulip.com',
full_name: 'George',
user_id: 103,
};
people.add(george);
stream_data.add_subscriber('India', 103);
assert.equal(stream_data.get_subscriber_count('India'), 2);
}());

View File

@@ -137,6 +137,18 @@ exports.update_subscribers_count = function (sub) {
sub.subscriber_count = count;
};
exports.get_subscriber_count = function (stream_name) {
var sub = exports.get_sub_by_name(stream_name);
if (sub === undefined) {
blueslip.warn('We got a get_subscriber_count count call for a non-existent stream.');
return;
}
if (!sub.subscribers) {
return 0;
}
return sub.subscribers.num_items();
};
exports.render_stream_description = function (sub) {
if (sub.description) {
sub.rendered_description = marked(sub.description).replace('<p>', '').replace('</p>', '');