Skip to content

Commit

Permalink
Properly handle clear ack events
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Apr 22, 2024
1 parent 2d780b2 commit a9070b6
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions internal/incident/incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (i *Incident) ProcessEvent(ctx context.Context, ev *event.Event) error {
return errors.New("can't insert incident event to the database")
}

if ev.Type == event.TypeAcknowledgementSet {
if ev.Type == event.TypeAcknowledgementSet || ev.Type == event.TypeAcknowledgementCleared {
if err = i.processAcknowledgementEvent(ctx, tx, ev); err != nil {
return err
}
Expand Down Expand Up @@ -592,32 +592,49 @@ func (i *Incident) notifyContact(contact *recipient.Contact, ev *event.Event, ch
}

// processAcknowledgementEvent processes the given ack event.
// Promotes the ack author to incident.RoleManager if it's not already the case and generates a history entry.
// This function promotes/demotes the ack author to incident.RoleManager/incident.RoleSubscriber based the provided
// event type, if it's not already the case and generates a history entry.
// Returns error on database failure.
func (i *Incident) processAcknowledgementEvent(ctx context.Context, tx *sqlx.Tx, ev *event.Event) error {
contact := i.runtimeConfig.GetContact(ev.Username)
if contact == nil {
i.logger.Warnw("Ignoring acknowledgement event from an unknown author", zap.String("author", ev.Username))
i.logger.Warnw("Ignoring (clear) acknowledgement event from an unknown author", zap.String("author", ev.Username))

return fmt.Errorf("unknown acknowledgment author %q", ev.Username)
return fmt.Errorf("unknown (clear) acknowledgment author %q", ev.Username)
}

recipientKey := recipient.ToKey(contact)
state := i.Recipients[recipientKey]
oldRole := RoleNone
newRole := RoleManager
if state != nil {
oldRole = state.Role

if oldRole == RoleManager {
// The user is already a manager
return nil
var newRole, oldRole ContactRole
switch ev.Type {
case event.TypeAcknowledgementSet:
oldRole = RoleNone
newRole = RoleManager
if state != nil {
oldRole = state.Role

if oldRole == RoleManager {
// The user is already a manager
i.logger.Debugw("Ignoring acknowledgement set event, object already acknowledged", zap.String("author", contact.String()))

return errors.New("duplicate acknowledgement")
}
} else {
i.Recipients[recipientKey] = &RecipientState{Role: newRole}
}
case event.TypeAcknowledgementCleared:
if state == nil || state.Role < RoleManager {
i.logger.Warnw("Ignoring clear acknowledgement event, no acknowledgement found", zap.String("author", contact.String()))

return errors.New("cannot clear non-existent acknowledgement")
}
} else {
i.Recipients[recipientKey] = &RecipientState{Role: newRole}

oldRole = state.Role
newRole = RoleSubscriber
}

i.logger.Infof("Contact %q role changed from %s to %s", contact.String(), oldRole.String(), newRole.String())
i.logger.Infof("Contact %q role changed from %q to %q", contact.String(), oldRole.String(), newRole.String())

hr := &HistoryRow{
Key: recipientKey,
Expand Down

0 comments on commit a9070b6

Please sign in to comment.