peer_data: Extend get_subscriber_count to allow excluding bots.

The new buddy list code path will want to be able to do this.
This commit is contained in:
Tim Abbott 2024-02-06 13:12:26 -08:00
parent a46c4f7b7e
commit 772ed724c0
2 changed files with 24 additions and 3 deletions

View File

@ -69,9 +69,18 @@ export function potential_subscribers(stream_id: number): User[] {
return people.filter_all_users(is_potential_subscriber);
}
export function get_subscriber_count(stream_id: number): number {
const subscribers = get_user_set(stream_id);
return subscribers.size;
export function get_subscriber_count(stream_id: number, include_bots = true): number {
if (include_bots) {
return get_user_set(stream_id).size;
}
let count = 0;
for (const user_id of get_user_set(stream_id).keys()) {
if (!people.is_valid_bot_user(user_id)) {
count += 1;
}
}
return count;
}
export function get_subscribers(stream_id: number): number[] {

View File

@ -214,6 +214,13 @@ test("get_subscriber_count", () => {
people.add_active_user(fred);
people.add_active_user(gail);
people.add_active_user(george);
const welcome_bot = {
email: "welcome-bot@example.com",
user_id: 40,
full_name: "Welcome Bot",
is_bot: true,
};
people.add_active_user(welcome_bot);
const india = {
stream_id: 102,
@ -235,6 +242,11 @@ test("get_subscriber_count", () => {
peer_data.remove_subscriber(india.stream_id, george.user_id);
assert.deepStrictEqual(peer_data.get_subscriber_count(india.stream_id), 1);
peer_data.add_subscriber(india.stream_id, welcome_bot.user_id);
assert.deepStrictEqual(peer_data.get_subscriber_count(india.stream_id), 2);
// Get the count without bots
assert.deepStrictEqual(peer_data.get_subscriber_count(india.stream_id, false), 1);
});
test("is_subscriber_subset", () => {