def get_hipchat_sender_id(realm_id: int, message_dict: Dict[str, Any], user_handler: UserHandler) -> int: ''' The HipChat export is inconsistent in how it renders senders, and sometimes we don't even get an id. ''' if isinstance(message_dict['sender'], str): # Some Hipchat instances just give us a person's # name in the sender field for NotificationMessage. # We turn them into a mirror user. mirror_user = user_handler.get_mirror_user( realm_id=realm_id, name=message_dict['sender'], ) sender_id = mirror_user['id'] return sender_id sender_id = message_dict['sender']['id'] if sender_id == 0: mirror_user = user_handler.get_mirror_user( realm_id=realm_id, name=message_dict['sender']['name'], ) sender_id = mirror_user['id'] return sender_id # HAPPY PATH: Hipchat just gave us an ordinary # sender_id. return sender_id
def get_hipchat_sender_id(realm_id: int, slim_mode: bool, message_dict: Dict[str, Any], user_id_mapper: IdMapper, user_handler: UserHandler) -> Optional[int]: ''' The HipChat export is inconsistent in how it renders senders, and sometimes we don't even get an id. ''' if isinstance(message_dict['sender'], str): if slim_mode: return None # Some Hipchat instances just give us a person's # name in the sender field for NotificationMessage. # We turn them into a mirror user. mirror_user = user_handler.get_mirror_user( realm_id=realm_id, name=message_dict['sender'], ) sender_id = mirror_user['id'] return sender_id raw_sender_id = message_dict['sender']['id'] if raw_sender_id == 0: if slim_mode: return None mirror_user = user_handler.get_mirror_user( realm_id=realm_id, name=message_dict['sender']['name'] ) sender_id = mirror_user['id'] return sender_id if not user_id_mapper.has(raw_sender_id): if slim_mode: return None mirror_user = user_handler.get_mirror_user( realm_id=realm_id, name=message_dict['sender']['id'] ) sender_id = mirror_user['id'] return sender_id # HAPPY PATH: Hipchat just gave us an ordinary # sender_id. sender_id = user_id_mapper.get(raw_sender_id) return sender_id