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 email = 'mirror-{user_id}@example.com'.format(user_id=user_id) delivery_email = email avatar_source = 'G' date_joined = int(timezone_now().timestamp()) timezone = 'UTC' user = build_user_profile( avatar_source=avatar_source, date_joined=date_joined, delivery_email=delivery_email, email=email, full_name=full_name, id=user_id, is_active=False, role=UserProfile.ROLE_MEMBER, 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 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 email = 'mirror-{user_id}@example.com'.format(user_id=user_id) delivery_email = email avatar_source = 'G' date_joined = int(timezone_now().timestamp()) timezone = 'UTC' user = build_user_profile( avatar_source=avatar_source, date_joined=date_joined, delivery_email=delivery_email, email=email, full_name=full_name, id=user_id, is_active=False, is_realm_admin=False, is_guest=False, 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 process(in_dict: ZerverFieldsT) -> ZerverFieldsT: delivery_email = in_dict['email'] email = in_dict['email'] full_name = in_dict['name'] id = user_id_mapper.get(in_dict['id']) is_mirror_dummy = False short_name = in_dict['mention_name'] timezone = in_dict['timezone'] role = UserProfile.ROLE_MEMBER if in_dict['account_type'] == 'admin': role = UserProfile.ROLE_REALM_ADMINISTRATOR if in_dict['account_type'] == 'guest': role = UserProfile.ROLE_GUEST date_joined = int(timezone_now().timestamp()) is_active = not in_dict['is_deleted'] if not email: if role == UserProfile.ROLE_GUEST: # Hipchat guest users don't have emails, so # we just fake them. email = 'guest-{id}@example.com'.format(id=id) delivery_email = email else: # Hipchat sometimes doesn't export an email for deactivated users. assert not is_active email = delivery_email = "deactivated-{id}@example.com".format( id=id) # unmapped fields: # title - Developer, Project Manager, etc. # rooms - no good sample data # created - we just use "now" # roles - we just use account_type if in_dict.get('avatar'): avatar_source = 'U' else: avatar_source = 'G' return build_user_profile( avatar_source=avatar_source, date_joined=date_joined, delivery_email=delivery_email, email=email, full_name=full_name, id=id, is_active=is_active, role=role, is_mirror_dummy=is_mirror_dummy, realm_id=realm_id, short_name=short_name, timezone=timezone, )
def process_user( user_dict: Dict[str, Any], realm_id: int, team_name: str, user_id_mapper: IdMapper ) -> ZerverFieldsT: def is_team_admin(user_dict: Dict[str, Any]) -> bool: if user_dict["teams"] is None: return False for team in user_dict["teams"]: if team["name"] == team_name and "team_admin" in team["roles"]: return True return False def get_full_name(user_dict: Dict[str, Any]) -> str: full_name = "{} {}".format(user_dict["first_name"], user_dict["last_name"]) if full_name.strip(): return full_name return user_dict["username"] avatar_source = "G" full_name = get_full_name(user_dict) id = user_id_mapper.get(user_dict["username"]) delivery_email = user_dict["email"] email = user_dict["email"] short_name = user_dict["username"] date_joined = int(timezone_now().timestamp()) timezone = "UTC" role = UserProfile.ROLE_MEMBER if is_team_admin(user_dict): role = UserProfile.ROLE_REALM_OWNER if user_dict["is_mirror_dummy"]: is_active = False is_mirror_dummy = True else: is_active = True is_mirror_dummy = False return build_user_profile( avatar_source=avatar_source, date_joined=date_joined, delivery_email=delivery_email, email=email, full_name=full_name, id=id, is_active=is_active, role=role, is_mirror_dummy=is_mirror_dummy, realm_id=realm_id, short_name=short_name, timezone=timezone, )
def process(in_dict: ZerverFieldsT) -> ZerverFieldsT: delivery_email = in_dict['email'] email = in_dict['email'] full_name = in_dict['name'] id = in_dict['id'] is_realm_admin = in_dict['account_type'] == 'admin' is_guest = in_dict['account_type'] == 'guest' is_mirror_dummy = False short_name = in_dict['mention_name'] timezone = in_dict['timezone'] if not email: # Hipchat guest users don't have emails, so # we just fake them. assert (is_guest) email = 'guest-{id}@example.com'.format(id=id) delivery_email = email date_joined = int(timezone_now().timestamp()) is_active = not in_dict['is_deleted'] # unmapped fields: # title - Developer, Project Manager, etc. # rooms - no good sample data # created - we just use "now" # roles - we just use account_type if in_dict.get('avatar'): avatar_source = 'U' else: avatar_source = 'G' return build_user_profile( avatar_source=avatar_source, date_joined=date_joined, delivery_email=delivery_email, email=email, full_name=full_name, id=id, is_active=is_active, is_realm_admin=is_realm_admin, is_guest=is_guest, is_mirror_dummy=is_mirror_dummy, realm_id=realm_id, short_name=short_name, timezone=timezone, )
def process_user(user_dict: Dict[str, Any], realm_id: int, team_name: str, user_id_mapper: IdMapper) -> ZerverFieldsT: def is_team_admin(user_dict: Dict[str, Any]) -> bool: for team in user_dict["teams"]: if team["name"] == team_name and "team_admin" in team["roles"]: return True return False def get_full_name(user_dict: Dict[str, Any]) -> str: full_name = "{} {}".format(user_dict["first_name"], user_dict["last_name"]) if full_name.strip(): return full_name return user_dict['username'] avatar_source = 'G' full_name = get_full_name(user_dict) id = user_id_mapper.get(user_dict['username']) delivery_email = user_dict['email'] email = user_dict['email'] is_realm_admin = is_team_admin(user_dict) is_guest = False short_name = user_dict['username'] date_joined = int(timezone_now().timestamp()) timezone = 'UTC' if user_dict["is_mirror_dummy"]: is_active = False is_mirror_dummy = True else: is_active = True is_mirror_dummy = False return build_user_profile( avatar_source=avatar_source, date_joined=date_joined, delivery_email=delivery_email, email=email, full_name=full_name, id=id, is_active=is_active, is_realm_admin=is_realm_admin, is_guest=is_guest, is_mirror_dummy=is_mirror_dummy, realm_id=realm_id, short_name=short_name, timezone=timezone, )
def process_users( user_id_to_user_map: Dict[str, Dict[str, Any]], realm_id: int, domain_name: str, user_handler: UserHandler, user_id_mapper: IdMapper, ) -> None: realm_owners: List[int] = [] bots: List[int] = [] for rc_user_id in user_id_to_user_map: user_dict = user_id_to_user_map[rc_user_id] is_mirror_dummy = False is_bot = False is_active = True # Rocket.Chat has three user types: # "user": This is a regular user of the system. # "bot": A special user types for bots. # "unknown": This usually represents a livechat guest. if user_dict["type"] != "user": is_active = False if user_dict["type"] == "bot": is_bot = True else: is_mirror_dummy = True if not user_dict.get("emails"): user_dict["emails"] = [{ "address": "{}-{}@{}".format(user_dict["username"], user_dict["type"], domain_name) }] # TODO: Change this to use actual exported avatar avatar_source = "G" full_name = user_dict["name"] id = user_id_mapper.get(rc_user_id) delivery_email = user_dict["emails"][0]["address"] email = user_dict["emails"][0]["address"] short_name = user_dict["username"] date_joined = float(user_dict["createdAt"].timestamp()) timezone = "UTC" role = UserProfile.ROLE_MEMBER if "admin" in user_dict["roles"]: role = UserProfile.ROLE_REALM_OWNER realm_owners.append(id) elif "guest" in user_dict["roles"]: role = UserProfile.ROLE_GUEST if is_bot: bots.append(id) user = build_user_profile( avatar_source=avatar_source, date_joined=date_joined, delivery_email=delivery_email, email=email, full_name=full_name, id=id, is_active=is_active, role=role, is_mirror_dummy=is_mirror_dummy, realm_id=realm_id, short_name=short_name, timezone=timezone, is_bot=is_bot, bot_type=1 if is_bot else None, ) user_handler.add_user(user) # Set the first realm_owner as the owner of # all the bots. if realm_owners: for bot_id in bots: bot_user = user_handler.get_user(user_id=bot_id) bot_user["bot_owner"] = realm_owners[0]