Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

narrow: Allow spectators to use is:resolved.

opmkumar1d33214b5556
diff --git a/web/src/filter.ts b/web/src/filter.ts
index 0377918f4bc..aac6113068f 100644
--- a/web/src/filter.ts
+++ b/web/src/filter.ts
@@ -865,7 +865,7 @@ export class Filter {
if (term.operand === undefined) {
return false;
}
- if (!hash_parser.is_an_allowed_web_public_narrow(term.operator)) {
+ if (!hash_parser.is_an_allowed_web_public_narrow(term.operator, term.operand)) {
return false;
}
}
diff --git a/web/src/hash_parser.ts b/web/src/hash_parser.ts
index dcc2ac5f50c..084804b1101 100644
--- a/web/src/hash_parser.ts
+++ b/web/src/hash_parser.ts
@@ -116,7 +116,10 @@ export function is_in_specified_hash_category(hash_categories: string[]): boolea
return hash_categories.includes(main_hash);
}
-export function is_an_allowed_web_public_narrow(operator: string): boolean {
+export function is_an_allowed_web_public_narrow(operator: string, operand: string): boolean {
+ if (operator === "is" && operand === "resolved") {
+ return true;
+ }
return allowed_web_public_narrow_operators.includes(operator);
}
@@ -161,10 +164,11 @@ export function is_spectator_compatible(hash: string): boolean {
if (main_hash === "narrow") {
let hash_section = get_hash_section(hash);
+ const second_hash_section = get_nth_hash_section(hash, 2);
if (hash_section.startsWith("-")) {
hash_section = hash_section.slice(1);
}
- if (!is_an_allowed_web_public_narrow(hash_section)) {
+ if (!is_an_allowed_web_public_narrow(hash_section, second_hash_section)) {
return false;
}
return true;
diff --git a/web/src/message_view.ts b/web/src/message_view.ts
index bf3934bb736..e41b7d46f1e 100644
--- a/web/src/message_view.ts
+++ b/web/src/message_view.ts
@@ -444,7 +444,8 @@ export let show = (raw_terms: NarrowTerm[], show_opts: ShowMessageViewOpts): voi
// policy?
!is_combined_feed_global_view &&
raw_terms.some(
- (raw_term) => !hash_parser.is_an_allowed_web_public_narrow(raw_term.operator),
+ (raw_term) =>
+ !hash_parser.is_an_allowed_web_public_narrow(raw_term.operator, raw_term.operand),
)
) {
spectators.login_to_access();
diff --git a/web/src/search_suggestion.ts b/web/src/search_suggestion.ts
index 6450adde19d..1a0065cd2a3 100644
--- a/web/src/search_suggestion.ts
+++ b/web/src/search_suggestion.ts
@@ -670,82 +670,114 @@ function get_channels_filter_suggestions(last: NarrowTerm, terms: NarrowTerm[]):
return get_special_filter_suggestions(last, terms, suggestions);
}
function get_is_filter_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Suggestion[] {
- const suggestions: SuggestionAndIncompatiblePatterns[] = [
- {
- search_string: "is:dm",
- description_html: "direct messages",
- is_people: false,
- incompatible_patterns: [
- {operator: "is", operand: "dm"},
- {operator: "is", operand: "resolved"},
- {operator: "channel"},
- {operator: "dm"},
- {operator: "in"},
- {operator: "topic"},
- ],
- },
- {
- search_string: "is:starred",
- description_html: "starred messages",
- is_people: false,
- incompatible_patterns: [{operator: "is", operand: "starred"}],
- },
- {
- search_string: "is:mentioned",
- description_html: "@-mentions",
- is_people: false,
- incompatible_patterns: [{operator: "is", operand: "mentioned"}],
- },
- {
- search_string: "is:followed",
- description_html: "followed topics",
- is_people: false,
- incompatible_patterns: [
- {operator: "is", operand: "followed"},
- {operator: "is", operand: "dm"},
- {operator: "dm"},
- {operator: "dm-including"},
- ],
- },
- {
- search_string: "is:alerted",
- description_html: "alerted messages",
- is_people: false,
- incompatible_patterns: [{operator: "is", operand: "alerted"}],
- },
- {
- search_string: "is:unread",
- description_html: "unread messages",
- is_people: false,
- incompatible_patterns: [{operator: "is", operand: "unread"}],
- },
- {
- search_string: "is:resolved",
- description_html: "resolved topics",
- is_people: false,
- incompatible_patterns: [
- {operator: "is", operand: "resolved"},
- {operator: "is", operand: "dm"},
- {operator: "dm"},
- {operator: "dm-including"},
- ],
- },
- {
- search_string: "-is:resolved",
- description_html: "unresolved topics",
- is_people: false,
- incompatible_patterns: [
- {operator: "is", operand: "resolved"},
- {operator: "is", operand: "dm"},
- {operator: "dm"},
- {operator: "dm-including"},
- ],
- },
- ];
+ let suggestions: SuggestionAndIncompatiblePatterns[];
+ if (page_params.is_spectator) {
+ suggestions = [
+ {
+ search_string: "is:resolved",
+ description_html: "resolved topics",
+ is_people: false,
+ incompatible_patterns: [
+ {operator: "is", operand: "resolved"},
+ {operator: "is", operand: "dm"},
+ {operator: "dm"},
+ {operator: "dm-including"},
+ ],
+ },
+ {
+ search_string: "-is:resolved",
+ description_html: "unresolved topics",
+ is_people: false,
+ incompatible_patterns: [
+ {operator: "is", operand: "resolved"},
+ {operator: "is", operand: "dm"},
+ {operator: "dm"},
+ {operator: "dm-including"},
+ ],
+ },
+ ];
+ } else {
+ suggestions = [
+ {
+ search_string: "is:dm",
+ description_html: "direct messages",
+ is_people: false,
+ incompatible_patterns: [
+ {operator: "is", operand: "dm"},
+ {operator: "is", operand: "resolved"},
+ {operator: "channel"},
+ {operator: "dm"},
+ {operator: "in"},
+ {operator: "topic"},
+ ],
+ },
+ {
+ search_string: "is:starred",
+ description_html: "starred messages",
+ is_people: false,
+ incompatible_patterns: [{operator: "is", operand: "starred"}],
+ },
+ {
+ search_string: "is:mentioned",
+ description_html: "@-mentions",
+ is_people: false,
+ incompatible_patterns: [{operator: "is", operand: "mentioned"}],
+ },
+ {
+ search_string: "is:followed",
+ description_html: "followed topics",
+ is_people: false,
+ incompatible_patterns: [
+ {operator: "is", operand: "followed"},
+ {operator: "is", operand: "dm"},
+ {operator: "dm"},
+ {operator: "dm-including"},
+ ],
+ },
+ {
+ search_string: "is:alerted",
+ description_html: "alerted messages",
+ is_people: false,
+ incompatible_patterns: [{operator: "is", operand: "alerted"}],
+ },
+ {
+ search_string: "is:unread",
+ description_html: "unread messages",
+ is_people: false,
+ incompatible_patterns: [{operator: "is", operand: "unread"}],
+ },
+ {
+ search_string: "is:resolved",
+ description_html: "resolved topics",
+ is_people: false,
+ incompatible_patterns: [
+ {operator: "is", operand: "resolved"},
+ {operator: "is", operand: "dm"},
+ {operator: "dm"},
+ {operator: "dm-including"},
+ ],
+ },
+ {
+ search_string: "-is:resolved",
+ description_html: "unresolved topics",
+ is_people: false,
+ incompatible_patterns: [
+ {operator: "is", operand: "resolved"},
+ {operator: "is", operand: "dm"},
+ {operator: "dm"},
+ {operator: "dm-including"},
+ ],
+ },
+ ];
+ }
const special_filtered_suggestions = get_special_filter_suggestions(last, terms, suggestions);
// Suggest "is:dm" to anyone with "is:private" in their muscle memory
const other_suggestions = [];
- if (last.operator === "is" && common.phrase_match(last.operand, "private")) {
+ if (
+ last.operator === "is" &&
+ common.phrase_match(last.operand, "private") &&
+ !page_params.is_spectator
+ ) {
const is_dm = format_as_suggestion([
{operator: last.operator, operand: "dm", negated: last.negated},
]);
@@ -1105,6 +1137,7 @@ export function get_search_result(
if (page_params.is_spectator) {
filterers = [
+ get_is_filter_suggestions,
get_channel_suggestions,
get_people("sender"),
get_people("from"),
diff --git a/web/tests/filter.test.cjs b/web/tests/filter.test.cjs
index cbd57ba14f8..a86da9efba8 100644
--- a/web/tests/filter.test.cjs
+++ b/web/tests/filter.test.cjs
@@ -2519,6 +2519,10 @@ test("error_cases", () => {
run_test("is_spectator_compatible", () => {
// tests same as test_is_spectator_compatible from test_message_fetch.py
assert.ok(Filter.is_spectator_compatible([]));
+ assert.ok(Filter.is_spectator_compatible([{operator: "is", operand: "resolved"}]));
+ assert.ok(
+ Filter.is_spectator_compatible([{operator: "is", operand: "resolved", negated: true}]),
+ );
assert.ok(Filter.is_spectator_compatible([{operator: "has", operand: "attachment"}]));
assert.ok(Filter.is_spectator_compatible([{operator: "has", operand: "image"}]));
assert.ok(Filter.is_spectator_compatible([{operator: "search", operand: "magic"}]));
diff --git a/web/tests/search_suggestion.test.cjs b/web/tests/search_suggestion.test.cjs
index 6c37a3b5f46..3d7cd99db0e 100644
--- a/web/tests/search_suggestion.test.cjs
+++ b/web/tests/search_suggestion.test.cjs
@@ -107,6 +107,8 @@ test("basic_get_suggestions_for_spectator", () => {
const query = "";
const suggestions = get_suggestions(query);
assert.deepEqual(suggestions.strings, [
+ "is:resolved",
+ "-is:resolved",
"has:link",
"has:image",
"has:attachment",
@@ -120,8 +122,8 @@ test("get_is_suggestions_for_spectator", () => {
const query = "is:";
const suggestions = get_suggestions(query);
- // The list of suggestions should be empty for a spectator
- assert.deepEqual(suggestions.strings, []);
+ // The list of suggestions should only contain "is:resolved" for a spectator
+ assert.deepEqual(suggestions.strings, ["is:resolved"]);
page_params.is_spectator = false;
});
diff --git a/zerver/lib/narrow.py b/zerver/lib/narrow.py
index 3e3a8ef123b..be82c931140 100644
--- a/zerver/lib/narrow.py
+++ b/zerver/lib/narrow.py
@@ -161,6 +161,10 @@ def is_spectator_compatible(narrow: Iterable[NarrowParameter]) -> bool:
]
for element in narrow:
operator = element.operator
+ operand = element.operand
+
+ if operator == "is" and operand == "resolved":
+ continue
if operator not in supported_operators:
return False
return True
@@ -389,7 +393,11 @@ class NarrowBuilder:
raise BadNarrowOperatorError("unknown 'in' operand " + operand)
def by_is(self, query: Select, operand: str, maybe_negate: ConditionTransform) -> Select:
- # This operator class does not support is_web_public_query.
+ # Only `resolved` operand of this class supports is_web_public_query.
+ if operand == "resolved":
+ cond = get_resolved_topic_condition_sa()
+ return query.where(maybe_negate(cond))
+
assert not self.is_web_public_query
assert self.user_profile is not None
@@ -421,9 +429,6 @@ class NarrowBuilder:
elif operand == "alerted":
cond = column("flags", Integer).op("&")(UserMessage.flags.has_alert_word.mask) != 0
return query.where(maybe_negate(cond))
- elif operand == "resolved":
- cond = get_resolved_topic_condition_sa()
- return query.where(maybe_negate(cond))
elif operand == "followed":
cond = get_followed_topic_condition_sa(self.user_profile.id)
return query.where(maybe_negate(cond))
diff --git a/zerver/tests/test_message_fetch.py b/zerver/tests/test_message_fetch.py
index 1a31479b89c..3505927cbed 100644
--- a/zerver/tests/test_message_fetch.py
+++ b/zerver/tests/test_message_fetch.py
@@ -1028,6 +1028,14 @@ class NarrowLibraryTest(ZulipTestCase):
def test_is_spectator_compatible(self) -> None:
self.assertTrue(is_spectator_compatible([]))
+ self.assertTrue(
+ is_spectator_compatible([NarrowParameter(operator="is", operand="resolved")])
+ )
+ self.assertTrue(
+ is_spectator_compatible(
+ [NarrowParameter(operator="is", operand="resolved", negated=True)]
+ )
+ )
self.assertTrue(
is_spectator_compatible([NarrowParameter(operator="has", operand="attachment")])
)
@@ -3602,13 +3610,29 @@ class GetOldMessagesTest(ZulipTestCase):
def test_get_messages_for_resolved_topics(self) -> None:
self.login("cordelia")
cordelia = self.example_user("cordelia")
+ self.subscribe(cordelia, "Rome")
- self.send_stream_message(cordelia, "Verona", "whatever1")
+ self.send_stream_message(cordelia, "Rome", "whatever1")
resolved_topic_name = RESOLVED_TOPIC_PREFIX + "foo"
- anchor = self.send_stream_message(cordelia, "Verona", "whatever2", resolved_topic_name)
- self.send_stream_message(cordelia, "Verona", "whatever3")
+ anchor = self.send_stream_message(cordelia, "Rome", "whatever2", resolved_topic_name)
+ self.send_stream_message(cordelia, "Rome", "whatever3")
- narrow = [dict(operator="is", operand="resolved")]
+ narrow = [
+ dict(operator="is", operand="resolved"),
+ dict(operator="channels", operand="public"),
+ ]
+ result = self.get_and_check_messages(
+ dict(narrow=orjson.dumps(narrow).decode(), anchor=anchor, num_before=0, num_after=0)
+ )
+ self.assert_length(result["messages"], 1)
+ self.assertEqual(result["messages"][0]["id"], anchor)
+
+ # "is:resolved" filter can be used by spectators as well.
+ self.logout()
+ narrow = [
+ dict(operator="is", operand="resolved"),
+ dict(operator="channels", operand="web-public"),
+ ]
result = self.get_and_check_messages(
dict(narrow=orjson.dumps(narrow).decode(), anchor=anchor, num_before=0, num_after=0)
)