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.

resolve topic: Add `is:resolved` search keyword/filtering support.

akshatdaltonf5c4d51ed209
diff --git a/zerver/lib/narrow.py b/zerver/lib/narrow.py
index 2408ac7dc99..662038514ea 100644
--- a/zerver/lib/narrow.py
+++ b/zerver/lib/narrow.py
@@ -5,7 +5,7 @@ from django.conf import settings
from django.utils.translation import gettext as _
from zerver.lib.request import JsonableError
-from zerver.lib.topic import get_topic_from_message_info
+from zerver.lib.topic import RESOLVED_TOPIC_PREFIX, get_topic_from_message_info
stop_words_list: Optional[List[str]] = None
@@ -93,6 +93,12 @@ def build_narrow_filter(narrow: Collection[Sequence[str]]) -> Callable[[Mapping[
elif operator == "is" and operand in ["alerted", "mentioned"]:
if "mentioned" not in flags:
return False
+ elif operator == "is" and operand == "resolved":
+ if message["type"] != "stream":
+ return False
+ topic_name = get_topic_from_message_info(message)
+ if not topic_name.startswith(RESOLVED_TOPIC_PREFIX):
+ return False
return True
diff --git a/zerver/lib/topic.py b/zerver/lib/topic.py
index f30e927cb02..e800b3ce30a 100644
--- a/zerver/lib/topic.py
+++ b/zerver/lib/topic.py
@@ -82,6 +82,11 @@ def topic_match_sa(topic_name: str) -> "ColumnElement[bool]":
return topic_cond
+def get_resolved_topic_condition_sa() -> "ColumnElement[bool]":
+ resolved_topic_cond = column("subject", Text).startswith(RESOLVED_TOPIC_PREFIX)
+ return resolved_topic_cond
+
+
def topic_column_sa() -> "ColumnElement[str]":
return column("subject", Text)
diff --git a/zerver/tests/fixtures/narrow.json b/zerver/tests/fixtures/narrow.json
index 08ae7bf50ae..926476b6c12 100644
--- a/zerver/tests/fixtures/narrow.json
+++ b/zerver/tests/fixtures/narrow.json
@@ -263,5 +263,37 @@
"unread"
]
]
+ },
+ {
+ "accept_events":[
+ {
+ "message":{
+ "type":"stream",
+ "subject":"✔ python"
+ },
+ "flags":null
+ }
+ ],
+ "reject_events":[
+ {
+ "message":{
+ "type":"private"
+ },
+ "flags":null
+ },
+ {
+ "message":{
+ "type":"stream",
+ "subject":"java"
+ },
+ "flags":null
+ }
+ ],
+ "narrow":[
+ [
+ "is",
+ "resolved"
+ ]
+ ]
}
]
diff --git a/zerver/tests/test_message_fetch.py b/zerver/tests/test_message_fetch.py
index 15b95236558..4195f112f03 100644
--- a/zerver/tests/test_message_fetch.py
+++ b/zerver/tests/test_message_fetch.py
@@ -34,7 +34,7 @@ from zerver.lib.sqlalchemy_utils import get_sqlalchemy_connection
from zerver.lib.streams import StreamDict, create_streams_if_needed, get_public_streams_queryset
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import HostRequestMock, get_user_messages, queries_captured
-from zerver.lib.topic import MATCH_TOPIC, TOPIC_NAME
+from zerver.lib.topic import MATCH_TOPIC, RESOLVED_TOPIC_PREFIX, TOPIC_NAME
from zerver.lib.topic_mutes import set_topic_mutes
from zerver.lib.types import DisplayRecipientT
from zerver.lib.upload import create_attachment
@@ -248,6 +248,14 @@ class NarrowBuilderTest(ZulipTestCase):
)
self._do_add_term_test(term, where_clause, params)
+ def test_add_term_using_is_operator_for_resolved_topics(self) -> None:
+ term = dict(operator="is", operand="resolved")
+ self._do_add_term_test(term, "WHERE (subject LIKE %(subject_1)s || '%%'")
+
+ def test_add_term_using_is_operator_for_negated_resolved_topics(self) -> None:
+ term = dict(operator="is", operand="resolved", negated=True)
+ self._do_add_term_test(term, "WHERE (subject NOT LIKE %(subject_1)s || '%%'")
+
def test_add_term_using_non_supported_operator_should_raise_error(self) -> None:
term = dict(operator="is", operand="non_supported")
self.assertRaises(BadNarrowOperator, self._build_query, term)
@@ -527,7 +535,7 @@ class NarrowLibraryTest(ZulipTestCase):
fixtures_path = os.path.join(os.path.dirname(__file__), "fixtures/narrow.json")
with open(fixtures_path, "rb") as f:
scenarios = orjson.loads(f.read())
- self.assert_length(scenarios, 9)
+ self.assert_length(scenarios, 10)
for scenario in scenarios:
narrow = scenario["narrow"]
accept_events = scenario["accept_events"]
@@ -663,6 +671,11 @@ class IncludeHistoryTest(ZulipTestCase):
dict(operator="is", operand="alerted"),
]
self.assertFalse(ok_to_include_history(narrow, user_profile, False))
+ narrow = [
+ dict(operator="streams", operand="public"),
+ dict(operator="is", operand="resolved"),
+ ]
+ self.assertFalse(ok_to_include_history(narrow, user_profile, False))
# simple True case
narrow = [
@@ -2495,6 +2508,22 @@ class GetOldMessagesTest(ZulipTestCase):
)
self.assert_length(result["messages"], 0)
+ def test_get_messages_for_resolved_topics(self) -> None:
+ self.login("cordelia")
+ cordelia = self.example_user("cordelia")
+
+ self.send_stream_message(cordelia, "Verona", "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")
+
+ narrow = [dict(operator="is", operand="resolved")]
+ result = self.get_and_check_messages(
+ dict(narrow=orjson.dumps(narrow).decode(), anchor=anchor, num_before=1, num_after=1)
+ )
+ self.assert_length(result["messages"], 1)
+ self.assertEqual(result["messages"][0]["id"], anchor)
+
def test_get_visible_messages_with_anchor(self) -> None:
def messages_matches_ids(messages: List[Dict[str, Any]], message_ids: List[int]) -> None:
self.assert_length(messages, len(message_ids))
diff --git a/zerver/views/message_fetch.py b/zerver/views/message_fetch.py
index 8eed1b433b4..72309e39b4f 100644
--- a/zerver/views/message_fetch.py
+++ b/zerver/views/message_fetch.py
@@ -47,7 +47,13 @@ from zerver.lib.streams import (
get_stream_by_narrow_operand_access_unchecked,
get_web_public_streams_queryset,
)
-from zerver.lib.topic import DB_TOPIC_NAME, MATCH_TOPIC, topic_column_sa, topic_match_sa
+from zerver.lib.topic import (
+ DB_TOPIC_NAME,
+ MATCH_TOPIC,
+ get_resolved_topic_condition_sa,
+ topic_column_sa,
+ topic_match_sa,
+)
from zerver.lib.topic_mutes import exclude_topic_mutes
from zerver.lib.types import Validator
from zerver.lib.utils import statsd
@@ -234,6 +240,9 @@ 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))
raise BadNarrowOperator("unknown 'is' operand " + operand)
_alphanum = frozenset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")