2018-10-14 23:23:34 +02:00
|
|
|
from typing import Any, Dict, List
|
|
|
|
|
2018-10-15 13:03:44 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.data_import.import_util import build_user_profile
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2018-10-15 13:03:44 +02:00
|
|
|
|
2018-10-14 23:23:34 +02:00
|
|
|
class UserHandler:
|
|
|
|
'''
|
|
|
|
Our UserHandler class is a glorified wrapper
|
|
|
|
around the data that eventually goes into
|
|
|
|
zerver_userprofile.
|
|
|
|
|
|
|
|
The class helps us do things like map ids
|
|
|
|
to names for mentions.
|
|
|
|
|
2018-10-15 13:03:44 +02:00
|
|
|
We also sometimes need to build mirror
|
|
|
|
users on the fly.
|
2018-10-14 23:23:34 +02:00
|
|
|
'''
|
2020-04-22 01:45:30 +02:00
|
|
|
|
2018-10-14 23:23:34 +02:00
|
|
|
def __init__(self) -> None:
|
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
|
|
|
self.id_to_user_map: Dict[int, Dict[str, Any]] = dict()
|
|
|
|
self.name_to_mirror_user_map: Dict[str, Dict[str, Any]] = dict()
|
2018-10-15 13:03:44 +02:00
|
|
|
self.mirror_user_id = 1
|
2018-10-14 23:23:34 +02:00
|
|
|
|
|
|
|
def add_user(self, user: Dict[str, Any]) -> None:
|
|
|
|
user_id = user['id']
|
|
|
|
self.id_to_user_map[user_id] = user
|
|
|
|
|
|
|
|
def get_user(self, user_id: int) -> Dict[str, Any]:
|
|
|
|
user = self.id_to_user_map[user_id]
|
|
|
|
return user
|
|
|
|
|
2018-10-15 13:03:44 +02:00
|
|
|
def get_mirror_user(self,
|
|
|
|
realm_id: int,
|
|
|
|
name: str) -> Dict[str, Any]:
|
|
|
|
if name in self.name_to_mirror_user_map:
|
|
|
|
user = self.name_to_mirror_user_map[name]
|
|
|
|
return user
|
|
|
|
|
|
|
|
user_id = self._new_mirror_user_id()
|
|
|
|
short_name = name
|
|
|
|
full_name = name
|
2020-06-09 00:25:09 +02:00
|
|
|
email = f'mirror-{user_id}@example.com'
|
2018-10-15 13:03:44 +02:00
|
|
|
delivery_email = email
|
|
|
|
avatar_source = 'G'
|
|
|
|
date_joined = int(timezone_now().timestamp())
|
|
|
|
timezone = 'UTC'
|
|
|
|
|
2018-10-17 20:35:22 +02:00
|
|
|
user = build_user_profile(
|
2018-10-15 13:03:44 +02:00
|
|
|
avatar_source=avatar_source,
|
|
|
|
date_joined=date_joined,
|
|
|
|
delivery_email=delivery_email,
|
|
|
|
email=email,
|
|
|
|
full_name=full_name,
|
|
|
|
id=user_id,
|
|
|
|
is_active=False,
|
2019-10-05 02:35:07 +02:00
|
|
|
role=UserProfile.ROLE_MEMBER,
|
2018-10-15 13:03:44 +02:00
|
|
|
is_mirror_dummy=True,
|
|
|
|
realm_id=realm_id,
|
|
|
|
short_name=short_name,
|
|
|
|
timezone=timezone,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.name_to_mirror_user_map[name] = user
|
|
|
|
return user
|
|
|
|
|
|
|
|
def _new_mirror_user_id(self) -> int:
|
|
|
|
next_id = self.mirror_user_id
|
|
|
|
while next_id in self.id_to_user_map:
|
|
|
|
next_id += 1
|
|
|
|
self.mirror_user_id = next_id + 1
|
|
|
|
return next_id
|
|
|
|
|
2018-10-14 23:23:34 +02:00
|
|
|
def get_normal_users(self) -> List[Dict[str, Any]]:
|
|
|
|
users = list(self.id_to_user_map.values())
|
|
|
|
return users
|
|
|
|
|
|
|
|
def get_all_users(self) -> List[Dict[str, Any]]:
|
2018-10-15 13:03:44 +02:00
|
|
|
normal_users = self.get_normal_users()
|
|
|
|
mirror_users = list(self.name_to_mirror_user_map.values())
|
|
|
|
all_users = normal_users + mirror_users
|
|
|
|
return all_users
|