2019-02-13 10:22:16 +01:00
|
|
|
from typing import Any, Dict, List, Tuple
|
2017-10-29 15:52:01 +01:00
|
|
|
|
2017-10-29 15:40:07 +01:00
|
|
|
from django.db.models.query import QuerySet
|
2020-06-11 00:54:34 +02:00
|
|
|
|
|
|
|
from zerver.models import Recipient, Stream, Subscription, UserProfile
|
|
|
|
|
2017-10-29 15:40:07 +01:00
|
|
|
|
2018-03-15 00:32:42 +01:00
|
|
|
def get_active_subscriptions_for_stream_id(stream_id: int) -> QuerySet:
|
|
|
|
# TODO: Change return type to QuerySet[Subscription]
|
2017-10-29 15:40:07 +01:00
|
|
|
return Subscription.objects.filter(
|
|
|
|
recipient__type=Recipient.STREAM,
|
|
|
|
recipient__type_id=stream_id,
|
|
|
|
active=True,
|
|
|
|
)
|
|
|
|
|
2018-03-15 00:32:42 +01:00
|
|
|
def get_active_subscriptions_for_stream_ids(stream_ids: List[int]) -> QuerySet:
|
|
|
|
# TODO: Change return type to QuerySet[Subscription]
|
2017-10-29 15:52:01 +01:00
|
|
|
return Subscription.objects.filter(
|
|
|
|
recipient__type=Recipient.STREAM,
|
|
|
|
recipient__type_id__in=stream_ids,
|
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
|
|
|
active=True,
|
2017-10-29 15:52:01 +01:00
|
|
|
)
|
|
|
|
|
perf: Extract get_subscribed_stream_ids_for_user.
This new method prevents us from getting fat
objects from the database.
Instead, now we just get ids from the database
to build our subqueries.
Note that we could also technically eliminate
the `set(...)` wrappers in this code to have
Django make a subquery and save a round trip.
I am postponing that for another commit (since
it's still somewhat coupled to some other
complexity in `do_get_streams` that I am trying
to cut through, plus it's not the main point
of this commit.)
BEFORE:
# old, still in use for other codepaths
def get_stream_subscriptions_for_user(user_profile: UserProfile) -> QuerySet:
# TODO: Change return type to QuerySet[Subscription]
return Subscription.objects.filter(
user_profile=user_profile,
recipient__type=Recipient.STREAM,
)
user_subs = get_stream_subscriptions_for_user(user_profile).filter(
active=True,
).select_related('recipient')
recipient_check = Q(id__in=[sub.recipient.type_id for sub in user_subs])
AFTER:
# newly added
def get_subscribed_stream_ids_for_user(user_profile: UserProfile) -> QuerySet:
return Subscription.objects.filter(
user_profile_id=user_profile,
recipient__type=Recipient.STREAM,
active=True,
).values_list('recipient__type_id', flat=True)
subscribed_stream_ids = get_subscribed_stream_ids_for_user(user_profile)
recipient_check = Q(id__in=set(subscribed_stream_ids))
2020-02-29 18:41:41 +01:00
|
|
|
def get_subscribed_stream_ids_for_user(user_profile: UserProfile) -> QuerySet:
|
|
|
|
return Subscription.objects.filter(
|
|
|
|
user_profile_id=user_profile,
|
|
|
|
recipient__type=Recipient.STREAM,
|
|
|
|
active=True,
|
|
|
|
).values_list('recipient__type_id', flat=True)
|
|
|
|
|
2018-03-15 00:32:42 +01:00
|
|
|
def get_stream_subscriptions_for_user(user_profile: UserProfile) -> QuerySet:
|
|
|
|
# TODO: Change return type to QuerySet[Subscription]
|
2017-10-29 17:11:11 +01:00
|
|
|
return Subscription.objects.filter(
|
|
|
|
user_profile=user_profile,
|
|
|
|
recipient__type=Recipient.STREAM,
|
|
|
|
)
|
|
|
|
|
2018-03-15 00:32:42 +01:00
|
|
|
def get_stream_subscriptions_for_users(user_profiles: List[UserProfile]) -> QuerySet:
|
|
|
|
# TODO: Change return type to QuerySet[Subscription]
|
2017-10-29 19:15:35 +01:00
|
|
|
return Subscription.objects.filter(
|
|
|
|
user_profile__in=user_profiles,
|
|
|
|
recipient__type=Recipient.STREAM,
|
|
|
|
)
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def get_bulk_stream_subscriber_info(
|
|
|
|
user_profiles: List[UserProfile],
|
|
|
|
stream_dict: Dict[int, Stream]) -> Dict[int, List[Tuple[Subscription, Stream]]]:
|
2017-10-29 20:19:57 +01:00
|
|
|
|
|
|
|
stream_ids = stream_dict.keys()
|
|
|
|
|
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
|
|
|
result: Dict[int, List[Tuple[Subscription, Stream]]] = {
|
2017-10-29 20:19:57 +01:00
|
|
|
user_profile.id: []
|
|
|
|
for user_profile in user_profiles
|
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
|
|
|
}
|
2017-10-29 20:19:57 +01:00
|
|
|
|
|
|
|
subs = Subscription.objects.filter(
|
|
|
|
user_profile__in=user_profiles,
|
|
|
|
recipient__type=Recipient.STREAM,
|
|
|
|
recipient__type_id__in=stream_ids,
|
|
|
|
active=True,
|
|
|
|
).select_related('user_profile', 'recipient')
|
|
|
|
|
|
|
|
for sub in subs:
|
|
|
|
user_profile_id = sub.user_profile_id
|
|
|
|
stream_id = sub.recipient.type_id
|
|
|
|
stream = stream_dict[stream_id]
|
|
|
|
result[user_profile_id].append((sub, stream))
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def num_subscribers_for_stream_id(stream_id: int) -> int:
|
2017-10-29 15:40:07 +01:00
|
|
|
return get_active_subscriptions_for_stream_id(stream_id).filter(
|
|
|
|
user_profile__is_active=True,
|
|
|
|
).count()
|
2019-02-13 10:22:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
def handle_stream_notifications_compatibility(user_profile: UserProfile,
|
|
|
|
stream_dict: Dict[str, Any],
|
|
|
|
notification_settings_null: bool) -> None:
|
|
|
|
# Old versions of the mobile apps don't support `None` as a
|
|
|
|
# value for the stream-level notifications properties, so we
|
|
|
|
# have to handle the normally frontend-side defaults for these
|
|
|
|
# settings here for those older clients.
|
|
|
|
#
|
|
|
|
# Note that this situation results in these older mobile apps
|
|
|
|
# having a subtle bug where changes to the user-level stream
|
|
|
|
# notification defaults will not properly propagate to the
|
|
|
|
# mobile app "stream notification settings" UI until the app
|
|
|
|
# re-registers. This is an acceptable level of
|
|
|
|
# backwards-compatibility problem in our view.
|
|
|
|
assert not notification_settings_null
|
|
|
|
|
|
|
|
for notification_type in ["desktop_notifications", "audible_notifications",
|
|
|
|
"push_notifications", "email_notifications"]:
|
|
|
|
# Values of true/false are supported by older clients.
|
|
|
|
if stream_dict[notification_type] is not None:
|
|
|
|
continue
|
|
|
|
target_attr = "enable_stream_" + notification_type
|
|
|
|
stream_dict[notification_type] = getattr(user_profile, target_attr)
|