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.

user_topics: Update lib functions to work with any visibility_policy.

Prakhar Pratyush0ec9ff17ce66
diff --git a/zerver/lib/message.py b/zerver/lib/message.py
index 588711c8e60..e9e2a75c753 100644
--- a/zerver/lib/message.py
+++ b/zerver/lib/message.py
@@ -52,7 +52,7 @@ from zerver.lib.timestamp import datetime_to_timestamp
from zerver.lib.topic import DB_TOPIC_NAME, MESSAGE__TOPIC, TOPIC_LINKS, TOPIC_NAME
from zerver.lib.types import DisplayRecipientT, EditHistoryEvent, UserDisplayRecipient
from zerver.lib.url_preview.types import UrlEmbedData
-from zerver.lib.user_topics import build_topic_mute_checker, topic_is_muted
+from zerver.lib.user_topics import build_topic_mute_checker, topic_has_visibility_policy
from zerver.models import (
MAX_TOPIC_NAME_LENGTH,
Message,
@@ -64,6 +64,7 @@ from zerver.models import (
Subscription,
UserMessage,
UserProfile,
+ UserTopic,
get_display_recipient_by_id,
get_usermessage_by_message_id,
query_for_ids,
@@ -1293,7 +1294,9 @@ def apply_unread_message_event(
if (
stream_id not in state["muted_stream_ids"]
# This next check hits the database.
- and not topic_is_muted(user_profile, stream_id, topic)
+ and not topic_has_visibility_policy(
+ user_profile, stream_id, topic, UserTopic.VisibilityPolicy.MUTED
+ )
):
state["unmuted_stream_msgs"].add(message_id)
diff --git a/zerver/lib/user_topics.py b/zerver/lib/user_topics.py
index 15ef8a227dc..c36d5857858 100644
--- a/zerver/lib/user_topics.py
+++ b/zerver/lib/user_topics.py
@@ -76,10 +76,11 @@ def get_topic_mutes(
]
-def set_topic_mutes(
+def set_topic_visibility_policy(
user_profile: UserProfile,
- muted_topics: List[List[str]],
- date_muted: Optional[datetime.datetime] = None,
+ topics: List[List[str]],
+ visibility_policy: int,
+ last_updated: Optional[datetime.datetime] = None,
) -> None:
"""
This is only used in tests.
@@ -87,12 +88,12 @@ def set_topic_mutes(
UserTopic.objects.filter(
user_profile=user_profile,
- visibility_policy=UserTopic.VisibilityPolicy.MUTED,
+ visibility_policy=visibility_policy,
).delete()
- if date_muted is None:
- date_muted = timezone_now()
- for stream_name, topic_name in muted_topics:
+ if last_updated is None:
+ last_updated = timezone_now()
+ for stream_name, topic_name in topics:
stream = get_stream(stream_name, user_profile.realm)
recipient_id = stream.recipient_id
assert recipient_id is not None
@@ -102,8 +103,8 @@ def set_topic_mutes(
stream_id=stream.id,
recipient_id=recipient_id,
topic_name=topic_name,
- visibility_policy=UserTopic.VisibilityPolicy.MUTED,
- last_updated=date_muted,
+ visibility_policy=visibility_policy,
+ last_updated=last_updated,
)
@@ -167,14 +168,16 @@ def set_user_topic_visibility_policy_in_database(
return True
-def topic_is_muted(user_profile: UserProfile, stream_id: int, topic_name: str) -> bool:
- is_muted = UserTopic.objects.filter(
+def topic_has_visibility_policy(
+ user_profile: UserProfile, stream_id: int, topic_name: str, visibility_policy: int
+) -> bool:
+ has_visibility_policy = UserTopic.objects.filter(
user_profile=user_profile,
stream_id=stream_id,
topic_name__iexact=topic_name,
- visibility_policy=UserTopic.VisibilityPolicy.MUTED,
+ visibility_policy=visibility_policy,
).exists()
- return is_muted
+ return has_visibility_policy
def exclude_topic_mutes(
diff --git a/zerver/tests/test_message_edit.py b/zerver/tests/test_message_edit.py
index 4d2bd9bfacb..0ec2a5a096d 100644
--- a/zerver/tests/test_message_edit.py
+++ b/zerver/tests/test_message_edit.py
@@ -24,8 +24,8 @@ from zerver.lib.topic import RESOLVED_TOPIC_PREFIX, TOPIC_NAME
from zerver.lib.user_topics import (
get_topic_mutes,
get_users_muting_topic,
- set_topic_mutes,
- topic_is_muted,
+ set_topic_visibility_policy,
+ topic_has_visibility_policy,
)
from zerver.lib.utils import assert_is_not_none
from zerver.models import (
@@ -1316,8 +1316,8 @@ class EditMessageTest(EditMessageTestCase):
[stream_name, "Topic2"],
[stream_name, already_muted_topic],
]
- set_topic_mutes(hamlet, muted_topics)
- set_topic_mutes(cordelia, muted_topics)
+ set_topic_visibility_policy(hamlet, muted_topics, UserTopic.VisibilityPolicy.MUTED)
+ set_topic_visibility_policy(cordelia, muted_topics, UserTopic.VisibilityPolicy.MUTED)
# Returns the users that need to be notified when a message topic is changed
def notify(user_id: int) -> Dict[str, Any]:
@@ -1357,15 +1357,51 @@ class EditMessageTest(EditMessageTestCase):
user["muted_topics"] = get_topic_mutes(muting_user)
break
- self.assertFalse(topic_is_muted(hamlet, stream.id, "Topic1"))
- self.assertFalse(topic_is_muted(cordelia, stream.id, "Topic1"))
- self.assertFalse(topic_is_muted(aaron, stream.id, "Topic1"))
- self.assertTrue(topic_is_muted(hamlet, stream.id, "Topic2"))
- self.assertTrue(topic_is_muted(cordelia, stream.id, "Topic2"))
- self.assertFalse(topic_is_muted(aaron, stream.id, "Topic2"))
- self.assertTrue(topic_is_muted(hamlet, stream.id, change_all_topic_name))
- self.assertTrue(topic_is_muted(cordelia, stream.id, change_all_topic_name))
- self.assertFalse(topic_is_muted(aaron, stream.id, change_all_topic_name))
+ self.assertFalse(
+ topic_has_visibility_policy(
+ hamlet, stream.id, "Topic1", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertFalse(
+ topic_has_visibility_policy(
+ cordelia, stream.id, "Topic1", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertFalse(
+ topic_has_visibility_policy(
+ aaron, stream.id, "Topic1", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertTrue(
+ topic_has_visibility_policy(
+ hamlet, stream.id, "Topic2", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertTrue(
+ topic_has_visibility_policy(
+ cordelia, stream.id, "Topic2", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertFalse(
+ topic_has_visibility_policy(
+ aaron, stream.id, "Topic2", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertTrue(
+ topic_has_visibility_policy(
+ hamlet, stream.id, change_all_topic_name, UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertTrue(
+ topic_has_visibility_policy(
+ cordelia, stream.id, change_all_topic_name, UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertFalse(
+ topic_has_visibility_policy(
+ aaron, stream.id, change_all_topic_name, UserTopic.VisibilityPolicy.MUTED
+ )
+ )
change_later_topic_name = "Topic 1 edited again"
check_update_message(
@@ -1378,8 +1414,16 @@ class EditMessageTest(EditMessageTestCase):
send_notification_to_new_thread=False,
content=None,
)
- self.assertFalse(topic_is_muted(hamlet, stream.id, change_all_topic_name))
- self.assertTrue(topic_is_muted(hamlet, stream.id, change_later_topic_name))
+ self.assertFalse(
+ topic_has_visibility_policy(
+ hamlet, stream.id, change_all_topic_name, UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertTrue(
+ topic_has_visibility_policy(
+ hamlet, stream.id, change_later_topic_name, UserTopic.VisibilityPolicy.MUTED
+ )
+ )
# Make sure we safely handle the case of the new topic being already muted.
with self.assertLogs(level="INFO") as info_logs:
@@ -1400,8 +1444,16 @@ class EditMessageTest(EditMessageTestCase):
f"INFO:root:User {cordelia.id} tried to set visibility_policy to its current value of {UserTopic.VisibilityPolicy.MUTED}",
},
)
- self.assertFalse(topic_is_muted(hamlet, stream.id, change_later_topic_name))
- self.assertTrue(topic_is_muted(hamlet, stream.id, already_muted_topic))
+ self.assertFalse(
+ topic_has_visibility_policy(
+ hamlet, stream.id, change_later_topic_name, UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertTrue(
+ topic_has_visibility_policy(
+ hamlet, stream.id, already_muted_topic, UserTopic.VisibilityPolicy.MUTED
+ )
+ )
change_one_topic_name = "Topic 1 edited change_one"
check_update_message(
@@ -1414,8 +1466,16 @@ class EditMessageTest(EditMessageTestCase):
send_notification_to_new_thread=False,
content=None,
)
- self.assertTrue(topic_is_muted(hamlet, stream.id, change_one_topic_name))
- self.assertFalse(topic_is_muted(hamlet, stream.id, change_later_topic_name))
+ self.assertTrue(
+ topic_has_visibility_policy(
+ hamlet, stream.id, change_one_topic_name, UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertFalse(
+ topic_has_visibility_policy(
+ hamlet, stream.id, change_later_topic_name, UserTopic.VisibilityPolicy.MUTED
+ )
+ )
# Move topic between two public streams.
desdemona = self.example_user("desdemona")
@@ -1428,8 +1488,8 @@ class EditMessageTest(EditMessageTestCase):
muted_topics = [
[stream_name, "New topic"],
]
- set_topic_mutes(desdemona, muted_topics)
- set_topic_mutes(cordelia, muted_topics)
+ set_topic_visibility_policy(desdemona, muted_topics, UserTopic.VisibilityPolicy.MUTED)
+ set_topic_visibility_policy(cordelia, muted_topics, UserTopic.VisibilityPolicy.MUTED)
with self.assert_database_query_count(30):
check_update_message(
@@ -1442,12 +1502,36 @@ class EditMessageTest(EditMessageTestCase):
content=None,
)
- self.assertFalse(topic_is_muted(desdemona, stream.id, "New topic"))
- self.assertFalse(topic_is_muted(cordelia, stream.id, "New topic"))
- self.assertFalse(topic_is_muted(aaron, stream.id, "New topic"))
- self.assertTrue(topic_is_muted(desdemona, new_public_stream.id, "New topic"))
- self.assertTrue(topic_is_muted(cordelia, new_public_stream.id, "New topic"))
- self.assertFalse(topic_is_muted(aaron, new_public_stream.id, "New topic"))
+ self.assertFalse(
+ topic_has_visibility_policy(
+ desdemona, stream.id, "New topic", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertFalse(
+ topic_has_visibility_policy(
+ cordelia, stream.id, "New topic", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertFalse(
+ topic_has_visibility_policy(
+ aaron, stream.id, "New topic", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertTrue(
+ topic_has_visibility_policy(
+ desdemona, new_public_stream.id, "New topic", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertTrue(
+ topic_has_visibility_policy(
+ cordelia, new_public_stream.id, "New topic", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertFalse(
+ topic_has_visibility_policy(
+ aaron, new_public_stream.id, "New topic", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
# Move topic to a private stream.
message_id = self.send_stream_message(
@@ -1459,8 +1543,8 @@ class EditMessageTest(EditMessageTestCase):
muted_topics = [
[stream_name, "New topic"],
]
- set_topic_mutes(desdemona, muted_topics)
- set_topic_mutes(cordelia, muted_topics)
+ set_topic_visibility_policy(desdemona, muted_topics, UserTopic.VisibilityPolicy.MUTED)
+ set_topic_visibility_policy(cordelia, muted_topics, UserTopic.VisibilityPolicy.MUTED)
with self.assert_database_query_count(31):
check_update_message(
@@ -1476,12 +1560,36 @@ class EditMessageTest(EditMessageTestCase):
# Cordelia is not subscribed to the private stream, so
# Cordelia should have had the topic unmuted, while Desdemona
# should have had her muted topic record moved.
- self.assertFalse(topic_is_muted(desdemona, stream.id, "New topic"))
- self.assertFalse(topic_is_muted(cordelia, stream.id, "New topic"))
- self.assertFalse(topic_is_muted(aaron, stream.id, "New topic"))
- self.assertTrue(topic_is_muted(desdemona, new_private_stream.id, "New topic"))
- self.assertFalse(topic_is_muted(cordelia, new_private_stream.id, "New topic"))
- self.assertFalse(topic_is_muted(aaron, new_private_stream.id, "New topic"))
+ self.assertFalse(
+ topic_has_visibility_policy(
+ desdemona, stream.id, "New topic", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertFalse(
+ topic_has_visibility_policy(
+ cordelia, stream.id, "New topic", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertFalse(
+ topic_has_visibility_policy(
+ aaron, stream.id, "New topic", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertTrue(
+ topic_has_visibility_policy(
+ desdemona, new_private_stream.id, "New topic", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertFalse(
+ topic_has_visibility_policy(
+ cordelia, new_private_stream.id, "New topic", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertFalse(
+ topic_has_visibility_policy(
+ aaron, new_private_stream.id, "New topic", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
# Move topic between two public streams with change in topic name.
desdemona = self.example_user("desdemona")
@@ -1492,8 +1600,8 @@ class EditMessageTest(EditMessageTestCase):
muted_topics = [
[stream_name, "New topic 2"],
]
- set_topic_mutes(desdemona, muted_topics)
- set_topic_mutes(cordelia, muted_topics)
+ set_topic_visibility_policy(desdemona, muted_topics, UserTopic.VisibilityPolicy.MUTED)
+ set_topic_visibility_policy(cordelia, muted_topics, UserTopic.VisibilityPolicy.MUTED)
with self.assert_database_query_count(30):
check_update_message(
@@ -1507,12 +1615,42 @@ class EditMessageTest(EditMessageTestCase):
content=None,
)
- self.assertFalse(topic_is_muted(desdemona, stream.id, "New topic 2"))
- self.assertFalse(topic_is_muted(cordelia, stream.id, "New topic 2"))
- self.assertFalse(topic_is_muted(aaron, stream.id, "New topic 2"))
- self.assertTrue(topic_is_muted(desdemona, new_public_stream.id, "changed topic name"))
- self.assertTrue(topic_is_muted(cordelia, new_public_stream.id, "changed topic name"))
- self.assertFalse(topic_is_muted(aaron, new_public_stream.id, "changed topic name"))
+ self.assertFalse(
+ topic_has_visibility_policy(
+ desdemona, stream.id, "New topic 2", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertFalse(
+ topic_has_visibility_policy(
+ cordelia, stream.id, "New topic 2", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertFalse(
+ topic_has_visibility_policy(
+ aaron, stream.id, "New topic 2", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertTrue(
+ topic_has_visibility_policy(
+ desdemona,
+ new_public_stream.id,
+ "changed topic name",
+ UserTopic.VisibilityPolicy.MUTED,
+ )
+ )
+ self.assertTrue(
+ topic_has_visibility_policy(
+ cordelia,
+ new_public_stream.id,
+ "changed topic name",
+ UserTopic.VisibilityPolicy.MUTED,
+ )
+ )
+ self.assertFalse(
+ topic_has_visibility_policy(
+ aaron, new_public_stream.id, "changed topic name", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
# Moving only half the messages doesn't move MutedTopic records.
second_message_id = self.send_stream_message(
@@ -1530,12 +1668,45 @@ class EditMessageTest(EditMessageTestCase):
content=None,
)
- self.assertTrue(topic_is_muted(desdemona, new_public_stream.id, "changed topic name"))
- self.assertTrue(topic_is_muted(cordelia, new_public_stream.id, "changed topic name"))
- self.assertFalse(topic_is_muted(aaron, new_public_stream.id, "changed topic name"))
- self.assertFalse(topic_is_muted(desdemona, new_public_stream.id, "final topic name"))
- self.assertFalse(topic_is_muted(cordelia, new_public_stream.id, "final topic name"))
- self.assertFalse(topic_is_muted(aaron, new_public_stream.id, "final topic name"))
+ self.assertTrue(
+ topic_has_visibility_policy(
+ desdemona,
+ new_public_stream.id,
+ "changed topic name",
+ UserTopic.VisibilityPolicy.MUTED,
+ )
+ )
+ self.assertTrue(
+ topic_has_visibility_policy(
+ cordelia,
+ new_public_stream.id,
+ "changed topic name",
+ UserTopic.VisibilityPolicy.MUTED,
+ )
+ )
+ self.assertFalse(
+ topic_has_visibility_policy(
+ aaron, new_public_stream.id, "changed topic name", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertFalse(
+ topic_has_visibility_policy(
+ desdemona,
+ new_public_stream.id,
+ "final topic name",
+ UserTopic.VisibilityPolicy.MUTED,
+ )
+ )
+ self.assertFalse(
+ topic_has_visibility_policy(
+ cordelia, new_public_stream.id, "final topic name", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+ self.assertFalse(
+ topic_has_visibility_policy(
+ aaron, new_public_stream.id, "final topic name", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
@mock.patch("zerver.actions.message_edit.send_event")
def test_wildcard_mention(self, mock_send_event: mock.MagicMock) -> None:
diff --git a/zerver/tests/test_message_fetch.py b/zerver/tests/test_message_fetch.py
index d6a69a63618..7e177b22f31 100644
--- a/zerver/tests/test_message_fetch.py
+++ b/zerver/tests/test_message_fetch.py
@@ -46,7 +46,7 @@ from zerver.lib.topic import MATCH_TOPIC, RESOLVED_TOPIC_PREFIX, TOPIC_NAME
from zerver.lib.types import DisplayRecipientT
from zerver.lib.upload.base import create_attachment
from zerver.lib.url_encoding import near_message_url
-from zerver.lib.user_topics import set_topic_mutes
+from zerver.lib.user_topics import set_topic_visibility_policy
from zerver.models import (
Attachment,
Message,
@@ -54,6 +54,7 @@ from zerver.models import (
Subscription,
UserMessage,
UserProfile,
+ UserTopic,
get_display_recipient,
get_realm,
get_stream,
@@ -3132,7 +3133,7 @@ class GetOldMessagesTest(ZulipTestCase):
muted_topics = [
["England", "muted"],
]
- set_topic_mutes(hamlet, muted_topics)
+ set_topic_visibility_policy(hamlet, muted_topics, UserTopic.VisibilityPolicy.MUTED)
# send a muted message
muted_message_id = self.send_stream_message(cordelia, "England", topic_name="muted")
@@ -3399,7 +3400,7 @@ class GetOldMessagesTest(ZulipTestCase):
["web stuff", "css"],
["bogus", "bogus"],
]
- set_topic_mutes(user_profile, muted_topics)
+ set_topic_visibility_policy(user_profile, muted_topics, UserTopic.VisibilityPolicy.MUTED)
query_params = dict(
anchor="first_unread",
@@ -3440,7 +3441,7 @@ class GetOldMessagesTest(ZulipTestCase):
muted_topics = [
["irrelevant_stream", "irrelevant_topic"],
]
- set_topic_mutes(user_profile, muted_topics)
+ set_topic_visibility_policy(user_profile, muted_topics, UserTopic.VisibilityPolicy.MUTED)
# If nothing relevant is muted, then exclude_muting_conditions()
# should return an empty list.
@@ -3462,7 +3463,7 @@ class GetOldMessagesTest(ZulipTestCase):
["Scotland", "golf"],
["web stuff", "css"],
]
- set_topic_mutes(user_profile, muted_topics)
+ set_topic_visibility_policy(user_profile, muted_topics, UserTopic.VisibilityPolicy.MUTED)
# And verify that our query will exclude them.
narrow = [
diff --git a/zerver/tests/test_user_topics.py b/zerver/tests/test_user_topics.py
index 096de6050fb..259ae7c29ad 100644
--- a/zerver/tests/test_user_topics.py
+++ b/zerver/tests/test_user_topics.py
@@ -9,7 +9,7 @@ from zerver.lib.stream_topic import StreamTopicTarget
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.user_topics import (
get_topic_mutes,
- topic_is_muted,
+ topic_has_visibility_policy,
)
from zerver.models import UserProfile, UserTopic, get_stream
@@ -107,7 +107,11 @@ class MutedTopicsTests(ZulipTestCase):
self.assert_json_success(result)
self.assertIn((stream.name, "Verona3", mock_date_muted), get_topic_mutes(user))
- self.assertTrue(topic_is_muted(user, stream.id, "verona3"))
+ self.assertTrue(
+ topic_has_visibility_policy(
+ user, stream.id, "verona3", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
do_set_user_topic_visibility_policy(
user,
@@ -162,7 +166,11 @@ class MutedTopicsTests(ZulipTestCase):
self.assert_json_success(result)
self.assertNotIn((stream.name, "Verona3", mock_date_muted), get_topic_mutes(user))
- self.assertFalse(topic_is_muted(user, stream.id, "verona3"))
+ self.assertFalse(
+ topic_has_visibility_policy(
+ user, stream.id, "verona3", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
def test_muted_topic_add_invalid(self) -> None:
user = self.example_user("hamlet")