2023-12-15 03:04:08 +01:00
|
|
|
from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple, TypedDict
|
2022-09-19 21:48:53 +02:00
|
|
|
|
|
|
|
from django_stubs_ext import ValuesQuerySet
|
2019-08-15 00:44:33 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.cache import (
|
2020-07-01 03:29:31 +02:00
|
|
|
bulk_cached_fetch,
|
2020-06-11 00:54:34 +02:00
|
|
|
cache_with_key,
|
|
|
|
display_recipient_cache_key,
|
2023-07-19 19:14:43 +02:00
|
|
|
generic_bulk_cached_fetch,
|
2023-07-16 19:08:55 +02:00
|
|
|
single_user_display_recipient_cache_key,
|
2020-06-11 00:54:34 +02:00
|
|
|
)
|
2023-12-15 03:04:08 +01:00
|
|
|
from zerver.lib.per_request_cache import return_same_value_during_entire_request
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.types import DisplayRecipientT, UserDisplayRecipient
|
2023-12-15 03:04:08 +01:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from zerver.models import Recipient
|
2019-08-15 00:44:33 +02:00
|
|
|
|
2019-08-20 21:22:37 +02:00
|
|
|
display_recipient_fields = [
|
|
|
|
"id",
|
|
|
|
"email",
|
|
|
|
"full_name",
|
|
|
|
"is_mirror_dummy",
|
|
|
|
]
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-05-02 06:24:43 +02:00
|
|
|
class TinyStreamResult(TypedDict):
|
2023-07-16 17:40:48 +02:00
|
|
|
recipient_id: int
|
2020-05-02 06:24:43 +02:00
|
|
|
name: str
|
2020-03-27 16:17:38 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-13 05:42:12 +02:00
|
|
|
def get_display_recipient_cache_key(
|
|
|
|
recipient_id: int, recipient_type: int, recipient_type_id: Optional[int]
|
|
|
|
) -> str:
|
|
|
|
return display_recipient_cache_key(recipient_id)
|
|
|
|
|
|
|
|
|
|
|
|
@cache_with_key(get_display_recipient_cache_key, timeout=3600 * 24 * 7)
|
2021-02-12 08:19:30 +01:00
|
|
|
def get_display_recipient_remote_cache(
|
|
|
|
recipient_id: int, recipient_type: int, recipient_type_id: Optional[int]
|
2023-08-10 16:12:37 +02:00
|
|
|
) -> List[UserDisplayRecipient]:
|
2019-08-15 00:44:33 +02:00
|
|
|
"""
|
2023-08-10 16:12:37 +02:00
|
|
|
This returns an appropriate object describing the recipient of a
|
|
|
|
direct message (whether individual or group).
|
|
|
|
|
|
|
|
It will be an array of dicts for each recipient.
|
|
|
|
|
|
|
|
Do not use this for streams.
|
2019-08-15 00:44:33 +02:00
|
|
|
"""
|
2023-08-10 16:12:37 +02:00
|
|
|
|
2023-12-15 03:04:08 +01:00
|
|
|
from zerver.models import Recipient, UserProfile
|
|
|
|
|
2023-08-10 16:12:37 +02:00
|
|
|
assert recipient_type != Recipient.STREAM
|
2019-08-15 00:44:33 +02:00
|
|
|
|
|
|
|
# The main priority for ordering here is being deterministic.
|
|
|
|
# Right now, we order by ID, which matches the ordering of user
|
|
|
|
# names in the left sidebar.
|
2021-02-12 08:19:30 +01:00
|
|
|
user_profile_list = (
|
|
|
|
UserProfile.objects.filter(
|
|
|
|
subscription__recipient_id=recipient_id,
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
.order_by("id")
|
2021-02-12 08:19:30 +01:00
|
|
|
.values(*display_recipient_fields)
|
|
|
|
)
|
2019-08-18 01:00:04 +02:00
|
|
|
return list(user_profile_list)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-08-18 01:00:04 +02:00
|
|
|
def user_dict_id_fetcher(user_dict: UserDisplayRecipient) -> int:
|
2021-02-12 08:20:45 +01:00
|
|
|
return user_dict["id"]
|
2019-08-18 01:00:04 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-07-16 19:08:55 +02:00
|
|
|
def bulk_fetch_single_user_display_recipients(uids: List[int]) -> Dict[int, UserDisplayRecipient]:
|
2023-12-15 03:04:08 +01:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2020-07-01 03:29:31 +02:00
|
|
|
return bulk_cached_fetch(
|
2019-08-07 00:18:13 +02:00
|
|
|
# Use a separate cache key to protect us from conflicts with
|
|
|
|
# the get_user_profile_by_id cache.
|
2019-08-18 01:00:04 +02:00
|
|
|
# (Since we fetch only several fields here)
|
2023-07-16 19:08:55 +02:00
|
|
|
cache_key_function=single_user_display_recipient_cache_key,
|
2019-08-20 21:22:37 +02:00
|
|
|
query_function=lambda ids: list(
|
2021-02-12 08:19:30 +01:00
|
|
|
UserProfile.objects.filter(id__in=ids).values(*display_recipient_fields)
|
|
|
|
),
|
2019-08-18 01:00:04 +02:00
|
|
|
object_ids=uids,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
id_fetcher=user_dict_id_fetcher,
|
2019-08-07 00:18:13 +02:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-07-16 17:40:48 +02:00
|
|
|
def bulk_fetch_stream_names(
|
2021-02-12 08:19:30 +01:00
|
|
|
recipient_tuples: Set[Tuple[int, int, int]],
|
2023-07-16 17:40:48 +02:00
|
|
|
) -> Dict[int, str]:
|
2019-08-07 00:18:13 +02:00
|
|
|
"""
|
|
|
|
Takes set of tuples of the form (recipient_id, recipient_type, recipient_type_id)
|
|
|
|
Returns dict mapping recipient_id to corresponding display_recipient
|
|
|
|
"""
|
|
|
|
|
2023-12-15 03:04:08 +01:00
|
|
|
from zerver.models import Stream
|
|
|
|
|
2023-07-16 17:40:48 +02:00
|
|
|
if len(recipient_tuples) == 0:
|
|
|
|
return {}
|
2019-08-07 00:18:13 +02:00
|
|
|
|
2023-07-16 17:40:48 +02:00
|
|
|
recipient_id_to_stream_id = {tup[0]: tup[2] for tup in recipient_tuples}
|
|
|
|
recipient_ids = [tup[0] for tup in recipient_tuples]
|
2019-08-07 00:18:13 +02:00
|
|
|
|
2023-07-16 17:40:48 +02:00
|
|
|
def get_tiny_stream_rows(
|
2022-06-23 20:23:46 +02:00
|
|
|
recipient_ids: List[int],
|
2022-09-19 21:48:53 +02:00
|
|
|
) -> ValuesQuerySet[Stream, TinyStreamResult]:
|
2023-07-16 17:40:48 +02:00
|
|
|
stream_ids = [recipient_id_to_stream_id[recipient_id] for recipient_id in recipient_ids]
|
|
|
|
return Stream.objects.filter(id__in=stream_ids).values("recipient_id", "name")
|
2019-08-07 00:18:13 +02:00
|
|
|
|
2023-07-16 17:40:48 +02:00
|
|
|
def get_recipient_id(row: TinyStreamResult) -> int:
|
|
|
|
return row["recipient_id"]
|
2019-08-07 00:18:13 +02:00
|
|
|
|
2023-07-16 17:40:48 +02:00
|
|
|
def get_name(row: TinyStreamResult) -> str:
|
|
|
|
return row["name"]
|
2019-08-07 00:18:13 +02:00
|
|
|
|
2023-07-16 17:40:48 +02:00
|
|
|
# ItemT = TinyStreamResult, CacheItemT = str (name), ObjKT = int (recipient_id)
|
2023-07-19 19:14:43 +02:00
|
|
|
stream_display_recipients: Dict[int, str] = generic_bulk_cached_fetch(
|
2019-08-07 00:18:13 +02:00
|
|
|
cache_key_function=display_recipient_cache_key,
|
2023-07-16 17:40:48 +02:00
|
|
|
query_function=get_tiny_stream_rows,
|
|
|
|
object_ids=recipient_ids,
|
|
|
|
id_fetcher=get_recipient_id,
|
|
|
|
cache_transformer=get_name,
|
2023-07-19 19:14:43 +02:00
|
|
|
setter=lambda obj: obj,
|
|
|
|
extractor=lambda obj: obj,
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
)
|
2019-08-07 00:18:13 +02:00
|
|
|
|
2023-07-16 17:40:48 +02:00
|
|
|
return stream_display_recipients
|
|
|
|
|
|
|
|
|
|
|
|
def bulk_fetch_user_display_recipients(
|
|
|
|
recipient_tuples: Set[Tuple[int, int, int]],
|
|
|
|
) -> Dict[int, List[UserDisplayRecipient]]:
|
|
|
|
"""
|
|
|
|
Takes set of tuples of the form (recipient_id, recipient_type, recipient_type_id)
|
|
|
|
Returns dict mapping recipient_id to corresponding display_recipient
|
|
|
|
"""
|
|
|
|
|
2023-12-15 03:18:20 +01:00
|
|
|
from zerver.models import Recipient
|
2024-07-04 14:05:48 +02:00
|
|
|
from zerver.models.recipients import bulk_get_direct_message_group_user_ids
|
2023-12-15 03:04:08 +01:00
|
|
|
|
2023-07-16 17:40:48 +02:00
|
|
|
if len(recipient_tuples) == 0:
|
|
|
|
return {}
|
|
|
|
|
2023-07-16 19:08:55 +02:00
|
|
|
get_recipient_id = lambda tup: tup[0]
|
|
|
|
get_type = lambda tup: tup[1]
|
2023-07-16 17:40:48 +02:00
|
|
|
|
2023-07-16 19:08:55 +02:00
|
|
|
personal_tuples = [tup for tup in recipient_tuples if get_type(tup) == Recipient.PERSONAL]
|
2024-07-04 14:05:48 +02:00
|
|
|
direct_message_group_tuples = [
|
2024-03-22 00:39:33 +01:00
|
|
|
tup for tup in recipient_tuples if get_type(tup) == Recipient.DIRECT_MESSAGE_GROUP
|
|
|
|
]
|
2023-07-16 17:40:48 +02:00
|
|
|
|
2024-07-04 14:05:48 +02:00
|
|
|
direct_message_group_recipient_ids = [
|
|
|
|
get_recipient_id(tup) for tup in direct_message_group_tuples
|
|
|
|
]
|
|
|
|
huddle_recipient_id_to_user_ids = bulk_get_direct_message_group_user_ids(
|
|
|
|
direct_message_group_recipient_ids
|
|
|
|
)
|
2023-07-16 17:40:48 +02:00
|
|
|
|
2023-07-16 19:08:55 +02:00
|
|
|
# Find all user ids whose UserProfiles we will need to fetch:
|
|
|
|
user_ids_to_fetch: Set[int] = set()
|
2019-08-07 00:18:13 +02:00
|
|
|
|
2023-07-16 19:08:55 +02:00
|
|
|
for ignore_recipient_id, ignore_recipient_type, user_id in personal_tuples:
|
|
|
|
user_ids_to_fetch.add(user_id)
|
2019-08-07 00:18:13 +02:00
|
|
|
|
2024-07-04 14:05:48 +02:00
|
|
|
for recipient_id in direct_message_group_recipient_ids:
|
|
|
|
direct_message_group_user_ids = huddle_recipient_id_to_user_ids[recipient_id]
|
|
|
|
user_ids_to_fetch |= direct_message_group_user_ids
|
2023-07-16 19:08:55 +02:00
|
|
|
|
|
|
|
# Fetch the needed user dictionaries.
|
|
|
|
user_display_recipients = bulk_fetch_single_user_display_recipients(list(user_ids_to_fetch))
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
|
|
|
|
for recipient_id, ignore_recipient_type, user_id in personal_tuples:
|
|
|
|
display_recipients = [user_display_recipients[user_id]]
|
|
|
|
result[recipient_id] = display_recipients
|
|
|
|
|
2024-07-04 14:05:48 +02:00
|
|
|
for recipient_id in direct_message_group_recipient_ids:
|
2023-07-16 19:08:55 +02:00
|
|
|
user_ids = sorted(huddle_recipient_id_to_user_ids[recipient_id])
|
|
|
|
display_recipients = [user_display_recipients[user_id] for user_id in user_ids]
|
|
|
|
result[recipient_id] = display_recipients
|
2019-08-07 00:18:13 +02:00
|
|
|
|
2023-07-16 19:08:55 +02:00
|
|
|
return result
|
2023-07-16 17:40:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
def bulk_fetch_display_recipients(
|
|
|
|
recipient_tuples: Set[Tuple[int, int, int]],
|
|
|
|
) -> Dict[int, DisplayRecipientT]:
|
|
|
|
"""
|
|
|
|
Takes set of tuples of the form (recipient_id, recipient_type, recipient_type_id)
|
|
|
|
Returns dict mapping recipient_id to corresponding display_recipient
|
|
|
|
"""
|
|
|
|
|
2023-12-15 03:04:08 +01:00
|
|
|
from zerver.models import Recipient
|
|
|
|
|
2023-07-16 17:40:48 +02:00
|
|
|
stream_recipients = {
|
|
|
|
recipient for recipient in recipient_tuples if recipient[1] == Recipient.STREAM
|
|
|
|
}
|
2024-07-04 14:05:48 +02:00
|
|
|
direct_message_recipients = recipient_tuples - stream_recipients
|
2023-07-16 17:40:48 +02:00
|
|
|
|
|
|
|
stream_display_recipients = bulk_fetch_stream_names(stream_recipients)
|
2024-07-04 14:05:48 +02:00
|
|
|
direct_message_display_recipients = bulk_fetch_user_display_recipients(
|
|
|
|
direct_message_recipients
|
2023-07-16 17:40:48 +02:00
|
|
|
)
|
|
|
|
|
2019-08-07 00:18:13 +02:00
|
|
|
# Glue the dicts together and return:
|
2024-07-04 14:05:48 +02:00
|
|
|
return {**stream_display_recipients, **direct_message_display_recipients}
|
2023-12-15 03:04:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
@return_same_value_during_entire_request
|
|
|
|
def get_display_recipient_by_id(
|
|
|
|
recipient_id: int, recipient_type: int, recipient_type_id: Optional[int]
|
|
|
|
) -> List[UserDisplayRecipient]:
|
|
|
|
"""
|
|
|
|
returns: an object describing the recipient (using a cache).
|
|
|
|
If the type is a stream, the type_id must be an int; a string is returned.
|
|
|
|
Otherwise, type_id may be None; an array of recipient dicts is returned.
|
|
|
|
"""
|
|
|
|
# Have to import here, to avoid circular dependency.
|
|
|
|
from zerver.lib.display_recipient import get_display_recipient_remote_cache
|
|
|
|
|
|
|
|
return get_display_recipient_remote_cache(recipient_id, recipient_type, recipient_type_id)
|
|
|
|
|
|
|
|
|
|
|
|
def get_display_recipient(recipient: "Recipient") -> List[UserDisplayRecipient]:
|
|
|
|
return get_display_recipient_by_id(
|
|
|
|
recipient.id,
|
|
|
|
recipient.type,
|
|
|
|
recipient.type_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def get_recipient_ids(
|
|
|
|
recipient: Optional["Recipient"], user_profile_id: int
|
|
|
|
) -> Tuple[List[int], str]:
|
|
|
|
from zerver.models import Recipient
|
|
|
|
|
|
|
|
if recipient is None:
|
|
|
|
recipient_type_str = ""
|
|
|
|
to = []
|
|
|
|
elif recipient.type == Recipient.STREAM:
|
|
|
|
recipient_type_str = "stream"
|
|
|
|
to = [recipient.type_id]
|
|
|
|
else:
|
|
|
|
recipient_type_str = "private"
|
|
|
|
if recipient.type == Recipient.PERSONAL:
|
|
|
|
to = [recipient.type_id]
|
|
|
|
else:
|
|
|
|
to = []
|
|
|
|
for r in get_display_recipient(recipient):
|
|
|
|
assert not isinstance(r, str) # It will only be a string for streams
|
|
|
|
if r["id"] != user_profile_id:
|
|
|
|
to.append(r["id"])
|
|
|
|
return to, recipient_type_str
|