def list_participants(incident_id: int, command: dict = None, db_session=None): """Returns the list of incident participants to the user as an ephemeral message.""" blocks = [] blocks.append( {"type": "section", "text": {"type": "mrkdwn", "text": "*Incident Participants*"}} ) participants = participant_service.get_all_by_incident_id( db_session=db_session, incident_id=incident_id ) contact_plugin = plugins.get(INCIDENT_PLUGIN_CONTACT_SLUG) for participant in participants: if participant.is_active: participant_email = participant.individual.email participant_info = contact_plugin.get(participant_email) participant_name = participant_info["fullname"] participant_team = participant_info["team"] participant_department = participant_info["department"] participant_location = participant_info["location"] participant_weblink = participant_info["weblink"] participant_avatar_url = dispatch_slack_service.get_user_avatar_url( slack_client, participant_email ) participant_active_roles = participant_role_service.get_all_active_roles( db_session=db_session, participant_id=participant.id ) participant_roles = [] for role in participant_active_roles: participant_roles.append(role.role) blocks.append( { "type": "section", "text": { "type": "mrkdwn", "text": ( f"*Name:* <{participant_weblink}|{participant_name}>\n" f"*Team*: {participant_team}, {participant_department}\n" f"*Location*: {participant_location}\n" f"*Incident Role(s)*: {(', ').join(participant_roles)}\n" ), }, "accessory": { "type": "image", "image_url": participant_avatar_url, "alt_text": participant_name, }, } ) blocks.append({"type": "divider"}) dispatch_slack_service.send_ephemeral_message( slack_client, command["channel_id"], command["user_id"], "Incident List Participants", blocks=blocks, )
def inactivate_participant(user_email: str, incident: Incident, db_session: SessionLocal): """Inactivates a participant.""" participant = get_by_incident_id_and_email(db_session=db_session, incident_id=incident.id, email=user_email) if not participant: log.debug( f"Can't inactivate participant with {user_email} email. They're not a participant of {incident.name} incident." ) return False log.debug( f"Inactivating {participant.individual.name} from {incident.name} incident..." ) participant_active_roles = participant_role_service.get_all_active_roles( db_session=db_session, participant_id=participant.id) for participant_active_role in participant_active_roles: participant_role_service.renounce_role( db_session=db_session, participant_role=participant_active_role) event_service.log( db_session=db_session, source="Dispatch Core App", description=f"{participant.individual.name} has been inactivated", incident_id=incident.id, ) return True
def test_get_all_active_roles(session, participant, participant_roles): from dispatch.participant_role.service import get_all_active_roles for participant_role in participant_roles: participant.participant_role.append(participant_role) t_participant_roles = get_all_active_roles( db_session=session, participant_id=participant.id).all() assert len(t_participant_roles) > 1
def remove_participant(user_email: str, incident_id: int, db_session: SessionLocal): """Removes a participant.""" # We load the incident incident = incident_service.get(db_session=db_session, incident_id=incident_id) # We get information about the individual contact_plugin = plugin_service.get_active(db_session=db_session, plugin_type="contact") individual_info = contact_plugin.instance.get(user_email) individual_fullname = individual_info["fullname"] log.debug( f"Removing {individual_fullname} from incident {incident.name}...") participant = get_by_incident_id_and_email(db_session=db_session, incident_id=incident_id, email=user_email) if not participant: log.debug( f"Can't remove {individual_fullname}. They're not an active participant of incident {incident.name}." ) return False # We mark the participant as inactive participant.is_active = False # We make the participant renounce to their active roles participant_active_roles = participant_role_service.get_all_active_roles( db_session=db_session, participant_id=participant.id) for participant_active_role in participant_active_roles: participant_role_service.renounce_role( db_session=db_session, participant_role=participant_active_role) # We add and commit the changes db_session.add(participant) db_session.commit() event_service.log( db_session=db_session, source="Dispatch Core App", description=f"{participant.individual.name} removed from incident", incident_id=incident_id, ) return True
def remove_participant(user_email: str, incident_id: int, db_session: SessionLocal): """Removes a participant.""" # We load the incident incident = incident_service.get(db_session=db_session, incident_id=incident_id) # We get information about the individual contact_plugin = plugins.get(INCIDENT_PLUGIN_CONTACT_SLUG) individual_info = contact_plugin.get(user_email) individual_fullname = individual_info["fullname"] log.debug( f"Removing {individual_fullname} from incident {incident.name}...") participant = get_by_incident_id_and_email(db_session=db_session, incident_id=incident_id, email=user_email) if not participant: log.debug( f"Can't remove {individual_fullname}. They're not an active participant of incident {incident.name}." ) return False # We mark the participant as inactive participant.is_active = False # We make the participant renounce to their active roles participant_active_roles = participant_role_service.get_all_active_roles( db_session=db_session, participant_id=participant.id) for participant_active_role in participant_active_roles: participant_role_service.renounce_role( db_session=db_session, participant_role=participant_active_role) # We add and commit the changes db_session.add(participant) db_session.commit() log.debug( f"Participant {participant.individual.name} has been removed from the incident." ) return True
def send_incident_participant_announcement_message(participant_email: str, incident: Incident, db_session: SessionLocal): """Announces a participant in the conversation.""" convo_plugin = plugin_service.get_active_instance( db_session=db_session, project_id=incident.project.id, plugin_type="conversation") if not convo_plugin: log.warning( "Incident participant announcement message not sent. No conversation plugin enabled." ) return notification_text = "New Incident Participant" notification_type = MessageType.incident_notification notification_template = [] participant = participant_service.get_by_incident_id_and_email( db_session=db_session, incident_id=incident.id, email=participant_email) participant_info = {} contact_plugin = plugin_service.get_active_instance( db_session=db_session, project_id=incident.project.id, plugin_type="contact") if contact_plugin: participant_info = contact_plugin.instance.get(participant_email, db_session=db_session) participant_name = participant_info.get("fullname", "Unknown") participant_team = participant_info.get("team", "Unknown") participant_department = participant_info.get("department", "Unknown") participant_location = participant_info.get("location", "Unknown") participant_weblink = participant_info.get("weblink", DISPATCH_UI_URL) participant_active_roles = participant_role_service.get_all_active_roles( db_session=db_session, participant_id=participant.id) participant_roles = [] for role in participant_active_roles: participant_roles.append(role.role) participant_avatar_url = convo_plugin.instance.get_participant_avatar_url( participant_email) # TODO these shouldn't be raw blocks (kglisson) blocks = [ { "type": "section", "text": { "type": "mrkdwn", "text": f"*{notification_text}*" } }, { "type": "section", "text": { "type": "mrkdwn", "text": (f"*Name:* <{participant_weblink}|{participant_name}>\n" f"*Team*: {participant_team}, {participant_department}\n" f"*Location*: {participant_location}\n" f"*Incident Role(s)*: {(', ').join(participant_roles)}\n"), }, "accessory": { "type": "image", "image_url": participant_avatar_url, "alt_text": participant_name, }, }, ] convo_plugin.instance.send( incident.conversation.channel_id, notification_text, notification_template, notification_type, blocks=blocks, ) log.debug("Incident participant announcement message sent.")
def list_participants(incident_id: int, command: dict = None, db_session=None): """Returns the list of incident participants to the user as an ephemeral message.""" blocks = [] blocks.append( {"type": "section", "text": {"type": "mrkdwn", "text": "*Incident Participants*"}} ) participants = participant_service.get_all_by_incident_id( db_session=db_session, incident_id=incident_id ).all() contact_plugin = plugin_service.get_active(db_session=db_session, plugin_type="contact") for participant in participants: if participant.is_active: participant_email = participant.individual.email participant_info = contact_plugin.instance.get(participant_email) participant_name = participant_info["fullname"] participant_team = participant_info["team"] participant_department = participant_info["department"] participant_location = participant_info["location"] participant_weblink = participant_info["weblink"] participant_avatar_url = dispatch_slack_service.get_user_avatar_url( slack_client, participant_email ) participant_reason_added = participant.added_reason or "Unknown" if participant.added_by: participant_added_by = participant.added_by.individual.name else: participant_added_by = "Unknown" participant_active_roles = participant_role_service.get_all_active_roles( db_session=db_session, participant_id=participant.id ) participant_roles = [] for role in participant_active_roles: participant_roles.append(role.role) block = { "type": "section", "text": { "type": "mrkdwn", "text": ( f"*Name:* <{participant_weblink}|{participant_name}>\n" f"*Team*: {participant_team}, {participant_department}\n" f"*Location*: {participant_location}\n" f"*Incident Role(s)*: {(', ').join(participant_roles)}\n" f"*Reason Added*: {participant_reason_added}\n" f"*Added By*: {participant_added_by}\n" ), }, } if len(participants) < 20: block.update( { "accessory": { "type": "image", "alt_text": participant_name, "image_url": participant_avatar_url, }, } ) blocks.append(block) blocks.append({"type": "divider"}) dispatch_slack_service.send_ephemeral_message( slack_client, command["channel_id"], command["user_id"], "Incident Participant List", blocks=blocks, )
def send_incident_participant_announcement_message( participant_email: str, incident_id: int, db_session=SessionLocal ): """Announces a participant in the conversation.""" notification_text = "New Incident Participant" notification_type = MessageType.incident_notification notification_template = [] # we load the incident instance incident = incident_service.get(db_session=db_session, incident_id=incident_id) participant = participant_service.get_by_incident_id_and_email( db_session=db_session, incident_id=incident_id, email=participant_email ) contact_plugin = plugins.get(INCIDENT_PLUGIN_CONTACT_SLUG) participant_info = contact_plugin.get(participant_email) participant_name = participant_info["fullname"] participant_team = participant_info["team"] participant_department = participant_info["department"] participant_location = participant_info["location"] participant_weblink = participant_info["weblink"] convo_plugin = plugins.get(INCIDENT_PLUGIN_CONVERSATION_SLUG) participant_avatar_url = convo_plugin.get_participant_avatar_url(participant_email) participant_active_roles = participant_role_service.get_all_active_roles( db_session=db_session, participant_id=participant.id ) participant_roles = [] for role in participant_active_roles: participant_roles.append(role.role) blocks = [ {"type": "section", "text": {"type": "mrkdwn", "text": f"*{notification_text}*"}}, { "type": "section", "text": { "type": "mrkdwn", "text": ( f"*Name:* <{participant_weblink}|{participant_name}>\n" f"*Team*: {participant_team}, {participant_department}\n" f"*Location*: {participant_location}\n" f"*Incident Role(s)*: {(', ').join(participant_roles)}\n" ), }, "accessory": { "type": "image", "image_url": participant_avatar_url, "alt_text": participant_name, }, }, ] convo_plugin.send( incident.conversation.channel_id, notification_text, notification_template, notification_type, blocks=blocks, ) log.debug("Incident participant announcement message sent.")
def list_participants( user_id: str, user_email: str, channel_id: str, incident_id: int, config: SlackConversationConfiguration = None, command: dict = None, db_session=None, slack_client=None, ): """Returns the list of incident participants to the user as an ephemeral message.""" blocks = [] blocks.append( {"type": "section", "text": {"type": "mrkdwn", "text": "*Incident Participants*"}} ) participants = participant_service.get_all_by_incident_id( db_session=db_session, incident_id=incident_id ).all() incident = incident_service.get(db_session=db_session, incident_id=incident_id) contact_plugin = plugin_service.get_active_instance( db_session=db_session, project_id=incident.project.id, plugin_type="contact" ) for participant in participants: if participant.active_roles: participant_email = participant.individual.email participant_info = contact_plugin.instance.get(participant_email, db_session=db_session) participant_name = participant_info.get("fullname", participant.individual.email) participant_team = participant_info.get("team", "Unknown") participant_department = participant_info.get("department", "Unknown") participant_location = participant_info.get("location", "Unknown") participant_weblink = participant_info.get("weblink") participant_avatar_url = dispatch_slack_service.get_user_avatar_url( slack_client, participant_email ) participant_reason_added = participant.added_reason or "Unknown" if participant.added_by: participant_added_by = participant.added_by.individual.name else: participant_added_by = "Unknown" participant_active_roles = participant_role_service.get_all_active_roles( db_session=db_session, participant_id=participant.id ) participant_roles = [] for role in participant_active_roles: participant_roles.append(role.role) # TODO we should make this more resilient to missing data (kglisson) block = { "type": "section", "text": { "type": "mrkdwn", "text": ( f"*Name:* <{participant_weblink}|{participant_name} ({participant_email})>\n" f"*Team*: {participant_team}, {participant_department}\n" f"*Location*: {participant_location}\n" f"*Incident Role(s)*: {(', ').join(participant_roles)}\n" f"*Reason Added*: {participant_reason_added}\n" f"*Added By*: {participant_added_by}\n" ), }, } if len(participants) < 20: block.update( { "accessory": { "type": "image", "alt_text": participant_name, "image_url": participant_avatar_url, }, } ) blocks.append(block) blocks.append({"type": "divider"}) dispatch_slack_service.send_ephemeral_message( slack_client, channel_id, user_id, "Incident Participant List", blocks=blocks, )