2022-09-19 21:48:53 +02:00
|
|
|
from typing import Dict, List, Optional, Set, Tuple, TypedDict
|
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
from zerver.lib.types import DisplayRecipientT, UserDisplayRecipient
|
2019-08-07 00:18:13 +02:00
|
|
|
from zerver.models import Recipient, Stream, UserProfile, bulk_get_huddle_user_ids
|
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]
|
|
|
|
) -> DisplayRecipientT:
|
2019-08-15 00:44:33 +02:00
|
|
|
"""
|
|
|
|
returns: an appropriate object describing the recipient. For a
|
|
|
|
stream this will be the stream name as a string. For a huddle or
|
|
|
|
personal, it will be an array of dicts about each recipient.
|
|
|
|
"""
|
|
|
|
if recipient_type == Recipient.STREAM:
|
|
|
|
assert recipient_type_id is not None
|
2021-02-12 08:20:45 +01:00
|
|
|
stream = Stream.objects.values("name").get(id=recipient_type_id)
|
|
|
|
return stream["name"]
|
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]:
|
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-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
|
|
|
|
"""
|
|
|
|
|
|
|
|
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]
|
|
|
|
huddle_tuples = [tup for tup in recipient_tuples if get_type(tup) == Recipient.HUDDLE]
|
2023-07-16 17:40:48 +02:00
|
|
|
|
2023-07-16 19:08:55 +02:00
|
|
|
huddle_recipient_ids = [get_recipient_id(tup) for tup in huddle_tuples]
|
|
|
|
huddle_recipient_id_to_user_ids = bulk_get_huddle_user_ids(huddle_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
|
|
|
|
2023-07-16 19:08:55 +02:00
|
|
|
for recipient_id in huddle_recipient_ids:
|
|
|
|
huddle_user_ids = huddle_recipient_id_to_user_ids[recipient_id]
|
|
|
|
user_ids_to_fetch |= huddle_user_ids
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
for recipient_id in huddle_recipient_ids:
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
|
|
|
stream_recipients = {
|
|
|
|
recipient for recipient in recipient_tuples if recipient[1] == Recipient.STREAM
|
|
|
|
}
|
|
|
|
personal_and_huddle_recipients = recipient_tuples - stream_recipients
|
|
|
|
|
|
|
|
stream_display_recipients = bulk_fetch_stream_names(stream_recipients)
|
|
|
|
personal_and_huddle_display_recipients = bulk_fetch_user_display_recipients(
|
|
|
|
personal_and_huddle_recipients
|
|
|
|
)
|
|
|
|
|
2019-08-07 00:18:13 +02:00
|
|
|
# Glue the dicts together and return:
|
|
|
|
return {**stream_display_recipients, **personal_and_huddle_display_recipients}
|