Example #1
0
def create_incident_activity(
    incident,
    activity_type,
    user=None,
    value=None,
    previous_value=None,
    comment=None,
    event_stats_snapshot=None,
):
    if activity_type == IncidentActivityType.COMMENT and user:
        subscribe_to_incident(incident, user)
    value = six.text_type(value) if value is not None else value
    previous_value = six.text_type(previous_value) if previous_value is not None else previous_value
    activity = IncidentActivity.objects.create(
        incident=incident,
        type=activity_type.value,
        user=user,
        value=value,
        previous_value=previous_value,
        comment=comment,
        event_stats_snapshot=event_stats_snapshot,
    )
    send_subscriber_notifications.apply_async(
        kwargs={'activity_id': activity.id},
        countdown=10,
    )
    return activity
Example #2
0
def create_incident_activity(
    incident,
    activity_type,
    user=None,
    value=None,
    previous_value=None,
    comment=None,
    event_stats_snapshot=None,
    mentioned_user_ids=None,
):
    if activity_type == IncidentActivityType.COMMENT and user:
        subscribe_to_incident(incident, user)
    value = six.text_type(value) if value is not None else value
    previous_value = six.text_type(
        previous_value) if previous_value is not None else previous_value
    activity = IncidentActivity.objects.create(
        incident=incident,
        type=activity_type.value,
        user=user,
        value=value,
        previous_value=previous_value,
        comment=comment,
        event_stats_snapshot=event_stats_snapshot,
    )

    if mentioned_user_ids:
        user_ids_to_subscribe = set(mentioned_user_ids) - set(
            IncidentSubscription.objects.filter(
                incident=incident,
                user_id__in=mentioned_user_ids,
            ).values_list('user_id', flat=True))
        if user_ids_to_subscribe:
            IncidentSubscription.objects.bulk_create([
                IncidentSubscription(incident=incident,
                                     user_id=mentioned_user_id)
                for mentioned_user_id in user_ids_to_subscribe
            ])
    send_subscriber_notifications.apply_async(
        kwargs={'activity_id': activity.id},
        countdown=10,
    )
    if activity_type == IncidentActivityType.COMMENT:
        analytics.record(
            'incident.comment',
            incident_id=incident.id,
            organization_id=incident.organization_id,
            incident_type=incident.type,
            user_id=user.id if user else None,
            activity_id=activity.id,
        )

    return activity