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.

message_send: Extract ActiveUserDict for accurate typing.

Zixuan James Lib210c223298e
diff --git a/zerver/actions/message_send.py b/zerver/actions/message_send.py
index b517dba28ab..586d4e7b395 100644
--- a/zerver/actions/message_send.py
+++ b/zerver/actions/message_send.py
@@ -2,12 +2,14 @@ import datetime
import logging
from collections import defaultdict
from typing import (
+ TYPE_CHECKING,
AbstractSet,
Any,
Callable,
Collection,
Dict,
List,
+ Literal,
Optional,
Sequence,
Set,
@@ -25,6 +27,7 @@ from django.utils.html import escape
from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _
from django.utils.translation import override as override_language
+from typing_extensions import TypeGuard
from zerver.actions.uploads import do_claim_attachments
from zerver.lib.addressee import Addressee
@@ -86,6 +89,9 @@ from zerver.models import (
)
from zerver.tornado.django_api import send_event
+if TYPE_CHECKING:
+ from django.db.models.query import _QuerySet as ValuesQuerySet
+
def compute_irc_user_fullname(email: str) -> str:
return email.split("@")[0] + " (IRC)"
@@ -165,6 +171,24 @@ class RecipientInfoResult(TypedDict):
all_bot_user_ids: Set[int]
+class BaseActiveUserDict(TypedDict):
+ id: int
+ enable_online_push_notifications: bool
+ enable_offline_email_notifications: bool
+ enable_offline_push_notifications: bool
+ long_term_idle: bool
+
+
+class ActiveUserDict(BaseActiveUserDict):
+ is_bot: bool
+ bot_type: Optional[int]
+
+
+class ActiveBotUserDict(BaseActiveUserDict):
+ is_bot: Literal[True]
+ bot_type: int
+
+
def get_recipient_info(
*,
realm_id: int,
@@ -278,7 +302,9 @@ def get_recipient_info(
user_ids |= possibly_mentioned_user_ids
if user_ids:
- query = UserProfile.objects.filter(is_active=True).values(
+ query: ValuesQuerySet[UserProfile, ActiveUserDict] = UserProfile.objects.filter(
+ is_active=True
+ ).values(
"id",
"enable_online_push_notifications",
"enable_offline_email_notifications",
@@ -313,11 +339,11 @@ def get_recipient_info(
# to-do.
rows = []
- def get_ids_for(f: Callable[[Dict[str, Any]], bool]) -> Set[int]:
+ def get_ids_for(f: Callable[[ActiveUserDict], bool]) -> Set[int]:
"""Only includes users on the explicit message to line"""
return {row["id"] for row in rows if f(row)} & message_to_user_id_set
- def is_service_bot(row: Dict[str, Any]) -> bool:
+ def is_service_bot(row: ActiveUserDict) -> TypeGuard[ActiveBotUserDict]:
return row["is_bot"] and (row["bot_type"] in UserProfile.SERVICE_BOT_TYPES)
active_user_ids = get_ids_for(lambda r: True)