From de0db7ad1a1d9f933538a908db2ca3e1f2d292a4 Mon Sep 17 00:00:00 2001 From: Aman Agrawal Date: Wed, 14 Feb 2024 07:52:00 +0000 Subject: [PATCH] filter: Make excludes_muted_topics a method of the `Filter` class. --- web/src/filter.ts | 13 +++++++++++++ web/src/narrow.js | 2 +- web/src/narrow_state.ts | 9 --------- web/tests/narrow_local.test.js | 4 ++-- web/tests/narrow_state.test.js | 30 +++++++++++++++++------------- 5 files changed, 33 insertions(+), 25 deletions(-) diff --git a/web/src/filter.ts b/web/src/filter.ts index dbd1d5fe28..5c8eea3d00 100644 --- a/web/src/filter.ts +++ b/web/src/filter.ts @@ -1208,4 +1208,17 @@ export class Filter { } return false; } + + excludes_muted_topics(): boolean { + return ( + // not narrowed to a topic + !(this.has_operator("stream") && this.has_operator("topic")) && + // not narrowed to search + !this.is_keyword_search() && + // not narrowed to dms + !(this.has_operator("dm") || this.has_operand("is", "dm")) && + // not narrowed to starred messages + !this.has_operand("is", "starred") + ); + } } diff --git a/web/src/narrow.js b/web/src/narrow.js index 9abd13628d..a3ec120d1d 100644 --- a/web/src/narrow.js +++ b/web/src/narrow.js @@ -410,7 +410,7 @@ export function activate(raw_terms, opts) { // From here on down, any calls to the narrow_state API will // reflect the upcoming narrow. narrow_state.set_has_shown_message_list_view(); - const excludes_muted_topics = narrow_state.excludes_muted_topics(filter); + const excludes_muted_topics = filter.excludes_muted_topics(); let msg_data = new MessageListData({ filter, diff --git a/web/src/narrow_state.ts b/web/src/narrow_state.ts index 1787ebdf82..1cdc9ae64a 100644 --- a/web/src/narrow_state.ts +++ b/web/src/narrow_state.ts @@ -378,15 +378,6 @@ export function narrowed_to_starred(current_filter: Filter | undefined = filter( return current_filter.has_operand("is", "starred"); } -export function excludes_muted_topics(filter: Filter): boolean { - return ( - !narrowed_to_topic(filter) && - !narrowed_to_search(filter) && - !narrowed_to_pms(filter) && - !narrowed_to_starred(filter) - ); -} - export function is_for_stream_id(stream_id: number, filter?: Filter): boolean { // This is not perfect, since we still track narrows by // name, not id, but at least the interface is good going diff --git a/web/tests/narrow_local.test.js b/web/tests/narrow_local.test.js index b4d06ba5a1..55d2903a2c 100644 --- a/web/tests/narrow_local.test.js +++ b/web/tests/narrow_local.test.js @@ -33,9 +33,9 @@ function test_with(fixture) { } } - const excludes_muted_topics = narrow_state.excludes_muted_topics(); + const excludes_muted_topics = filter.excludes_muted_topics(); const msg_data = new MessageListData({ - filter: narrow_state.filter(), + filter, excludes_muted_topics, }); const id_info = { diff --git a/web/tests/narrow_state.test.js b/web/tests/narrow_state.test.js index 8227ee2e5e..92cdc08b1d 100644 --- a/web/tests/narrow_state.test.js +++ b/web/tests/narrow_state.test.js @@ -17,11 +17,14 @@ function set_filter(raw_terms) { operator: op[0], operand: op[1], })); + const filter = new Filter(terms); message_lists.set_current({ data: { - filter: new Filter(terms), + filter, }, }); + + return filter; } function test(label, f) { @@ -160,26 +163,27 @@ test("terms", () => { }); test("excludes_muted_topics", () => { - set_filter([["stream", "devel"]]); - assert.ok(narrow_state.excludes_muted_topics()); + let filter = set_filter([["stream", "devel"]]); + assert.ok(filter.excludes_muted_topics()); - message_lists.current = undefined; // not narrowed, basically - assert.ok(narrow_state.excludes_muted_topics()); + // All messages view. + filter = set_filter([["in", "home"]]); + assert.ok(filter.excludes_muted_topics()); - set_filter([ + filter = set_filter([ ["stream", "devel"], ["topic", "mac"], ]); - assert.ok(!narrow_state.excludes_muted_topics()); + assert.ok(!filter.excludes_muted_topics()); - set_filter([["search", "whatever"]]); - assert.ok(!narrow_state.excludes_muted_topics()); + filter = set_filter([["search", "whatever"]]); + assert.ok(!filter.excludes_muted_topics()); - set_filter([["is", "private"]]); - assert.ok(!narrow_state.excludes_muted_topics()); + filter = set_filter([["is", "private"]]); + assert.ok(!filter.excludes_muted_topics()); - set_filter([["is", "starred"]]); - assert.ok(!narrow_state.excludes_muted_topics()); + filter = set_filter([["is", "starred"]]); + assert.ok(!filter.excludes_muted_topics()); }); test("set_compose_defaults", () => {