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_topic: Use get_user_topics to build muted topics tuples.

Kartik Srivastava8b674ee3d7c5
diff --git a/zerver/lib/types.py b/zerver/lib/types.py
index 4cef9ddd5c6..23e0d254fc8 100644
--- a/zerver/lib/types.py
+++ b/zerver/lib/types.py
@@ -147,6 +147,18 @@ class FormattedEditHistoryEvent(TypedDict, total=False):
content_html_diff: str
+class UserTopicDict(TypedDict, total=False):
+ """Dictionary containing fields fetched from the UserTopic model that
+ are needed to encode the UserTopic object for the API.
+ """
+
+ stream_id: int
+ stream__name: str
+ topic_name: str
+ last_updated: int
+ visibility_policy: int
+
+
# This next batch of types is for Stream/Subscription objects.
class RawStreamDict(TypedDict):
"""Dictionary containing fields fetched from the Stream model that
diff --git a/zerver/lib/user_topics.py b/zerver/lib/user_topics.py
index f67d897f07a..a38a77793d0 100644
--- a/zerver/lib/user_topics.py
+++ b/zerver/lib/user_topics.py
@@ -8,25 +8,57 @@ from sqlalchemy.types import Integer
from zerver.lib.timestamp import datetime_to_timestamp
from zerver.lib.topic import topic_match_sa
+from zerver.lib.types import UserTopicDict
from zerver.models import UserProfile, UserTopic, get_stream
-def get_topic_mutes(
- user_profile: UserProfile, include_deactivated: bool = False
-) -> List[Tuple[str, str, float]]:
- query = UserTopic.objects.filter(user_profile=user_profile, visibility_policy=UserTopic.MUTED)
- # Exclude muted topics that are part of deactivated streams unless
+def get_user_topics(
+ user_profile: UserProfile,
+ include_deactivated: bool = False,
+ visibility_policy: Optional[int] = None,
+) -> List[UserTopicDict]:
+ """
+ Fetches UserTopic objects associated with the target user.
+ * include_deactivated: Whether to include those associated with
+ deactivated streams.
+ * visibility_policy: If specified, returns only UserTopic objects
+ with the specified visibility_policy value.
+ """
+ query = UserTopic.objects.filter(user_profile=user_profile)
+
+ if visibility_policy is not None:
+ query = query.filter(visibility_policy=visibility_policy)
+
+ # Exclude user topics that are part of deactivated streams unless
# explicitly requested.
if not include_deactivated:
query = query.filter(stream__deactivated=False)
+
rows = query.values(
- "stream__name",
- "topic_name",
- "last_updated",
+ "stream_id", "stream__name", "topic_name", "last_updated", "visibility_policy"
)
+
+ result = []
+ for row in rows:
+ row["last_updated"] = datetime_to_timestamp(row["last_updated"])
+ result.append(row)
+
+ return result
+
+
+def get_topic_mutes(
+ user_profile: UserProfile, include_deactivated: bool = False
+) -> List[Tuple[str, str, int]]:
+
+ user_topics = get_user_topics(
+ user_profile=user_profile,
+ include_deactivated=include_deactivated,
+ visibility_policy=UserTopic.MUTED,
+ )
+
return [
- (row["stream__name"], row["topic_name"], datetime_to_timestamp(row["last_updated"]))
- for row in rows
+ (user_topic["stream__name"], user_topic["topic_name"], user_topic["last_updated"])
+ for user_topic in user_topics
]