Skip to content

Commit

Permalink
Rework hooks so the hook instance is the exported thing
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Oct 6, 2020
1 parent 9d2d68c commit 3829e7a
Show file tree
Hide file tree
Showing 20 changed files with 91 additions and 91 deletions.
8 changes: 4 additions & 4 deletions hooks/airtime_transferred.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ func init() {
}

// InsertAirtimeTransfersHook is our hook for inserting airtime transfers
type InsertAirtimeTransfersHook struct{}
var InsertAirtimeTransfersHook models.EventCommitHook = &insertAirtimeTransfersHook{}

var insertAirtimeTransfersHook = &InsertAirtimeTransfersHook{}
type insertAirtimeTransfersHook struct{}

// Apply inserts all the airtime transfers that were created
func (h *InsertAirtimeTransfersHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
func (h *insertAirtimeTransfersHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
// gather all our transfers
transfers := make([]*models.AirtimeTransfer, 0, len(scenes))

Expand Down Expand Up @@ -105,7 +105,7 @@ func handleAirtimeTransferred(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool,
))
}

scene.AppendToEventPreCommitHook(insertAirtimeTransfersHook, transfer)
scene.AppendToEventPreCommitHook(InsertAirtimeTransfersHook, transfer)

return nil
}
10 changes: 5 additions & 5 deletions hooks/broadcast_created.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ func init() {
models.RegisterEventHandler(events.TypeBroadcastCreated, handleBroadcastCreated)
}

// StartBroadcastsHook is our hook for starting the broadcasts created in these scene
type StartBroadcastsHook struct{}
// StartBroadcastsHook is our hook for starting broadcasts
var StartBroadcastsHook models.EventCommitHook = &startBroadcastsHook{}

var startBroadcastsHook = &StartBroadcastsHook{}
type startBroadcastsHook struct{}

// Apply queues up our broadcasts for sending
func (h *StartBroadcastsHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
func (h *startBroadcastsHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
rc := rp.Get()
defer rc.Close()

Expand Down Expand Up @@ -66,7 +66,7 @@ func handleBroadcastCreated(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa
}).Debug("broadcast created")

// schedule this for being started after our scene are committed
scene.AppendToEventPostCommitHook(startBroadcastsHook, event)
scene.AppendToEventPostCommitHook(StartBroadcastsHook, event)

return nil
}
6 changes: 3 additions & 3 deletions hooks/campaigns.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (
)

// UpdateCampaignEventsHook is our hook to update any campaign events
type UpdateCampaignEventsHook struct{}
var UpdateCampaignEventsHook models.EventCommitHook = &updateCampaignEventsHook{}

var updateCampaignEventsHook = &UpdateCampaignEventsHook{}
type updateCampaignEventsHook struct{}

// Apply will update all the campaigns for the passed in scene, minimizing the number of queries to do so
func (h *UpdateCampaignEventsHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
func (h *updateCampaignEventsHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
// these are all the events we need to delete unfired fires for
deletes := make([]*models.FireDelete, 0, 5)

Expand Down
10 changes: 5 additions & 5 deletions hooks/contact_field_changed.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ func init() {
}

// CommitFieldChangesHook is our hook for contact field changes
type CommitFieldChangesHook struct{}
var CommitFieldChangesHook models.EventCommitHook = &commitFieldChangesHook{}

var commitFieldChangesHook = &CommitFieldChangesHook{}
type commitFieldChangesHook struct{}

// Apply squashes and writes all the field updates for the contacts
func (h *CommitFieldChangesHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
func (h *commitFieldChangesHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
// our list of updates
fieldUpdates := make([]interface{}, 0, len(scenes))
fieldDeletes := make(map[assets.FieldUUID][]interface{})
Expand Down Expand Up @@ -100,8 +100,8 @@ func handleContactFieldChanged(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool,
}).Debug("contact field changed")

// add our callback
scene.AppendToEventPreCommitHook(commitFieldChangesHook, event)
scene.AppendToEventPreCommitHook(updateCampaignEventsHook, event)
scene.AppendToEventPreCommitHook(CommitFieldChangesHook, event)
scene.AppendToEventPreCommitHook(UpdateCampaignEventsHook, event)

return nil
}
Expand Down
18 changes: 9 additions & 9 deletions hooks/contact_groups_changed.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func init() {
}

// CommitGroupChangesHook is our hook for all group changes
type CommitGroupChangesHook struct{}
var CommitGroupChangesHook models.EventCommitHook = &commitGroupChangesHook{}

var commitGroupChangesHook = &CommitGroupChangesHook{}
type commitGroupChangesHook struct{}

// Apply squashes and adds or removes all our contact groups
func (h *CommitGroupChangesHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
func (h *commitGroupChangesHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
// build up our list of all adds and removes
adds := make([]*models.GroupAdd, 0, len(scenes))
removes := make([]*models.GroupRemove, 0, len(scenes))
Expand Down Expand Up @@ -98,9 +98,9 @@ func handleContactGroupsChanged(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool
}

// add our add event
scene.AppendToEventPreCommitHook(commitGroupChangesHook, hookEvent)
scene.AppendToEventPreCommitHook(updateCampaignEventsHook, hookEvent)
scene.AppendToEventPreCommitHook(contactModifiedHook, scene.ContactID())
scene.AppendToEventPreCommitHook(CommitGroupChangesHook, hookEvent)
scene.AppendToEventPreCommitHook(UpdateCampaignEventsHook, hookEvent)
scene.AppendToEventPreCommitHook(ContactModifiedHook, scene.ContactID())
}

// add each of our groups
Expand All @@ -121,9 +121,9 @@ func handleContactGroupsChanged(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool
GroupID: group.ID(),
}

scene.AppendToEventPreCommitHook(commitGroupChangesHook, hookEvent)
scene.AppendToEventPreCommitHook(updateCampaignEventsHook, hookEvent)
scene.AppendToEventPreCommitHook(contactModifiedHook, scene.ContactID())
scene.AppendToEventPreCommitHook(CommitGroupChangesHook, hookEvent)
scene.AppendToEventPreCommitHook(UpdateCampaignEventsHook, hookEvent)
scene.AppendToEventPreCommitHook(ContactModifiedHook, scene.ContactID())
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions hooks/contact_language_changed.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func init() {
}

// CommitLanguageChangesHook is our hook for language changes
type CommitLanguageChangesHook struct{}
var CommitLanguageChangesHook models.EventCommitHook = &commitLanguageChangesHook{}

var commitLanguageChangesHook = &CommitLanguageChangesHook{}
type commitLanguageChangesHook struct{}

// Apply applies our contact language change before our commit
func (h *CommitLanguageChangesHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
func (h *commitLanguageChangesHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
// build up our list of pairs of contact id and language name
updates := make([]interface{}, 0, len(scenes))
for s, e := range scenes {
Expand All @@ -44,7 +44,7 @@ func handleContactLanguageChanged(ctx context.Context, tx *sqlx.Tx, rp *redis.Po
"language": event.Language,
}).Debug("changing contact language")

scene.AppendToEventPreCommitHook(commitLanguageChangesHook, event)
scene.AppendToEventPreCommitHook(CommitLanguageChangesHook, event)
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions hooks/contact_last_seen.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
)

// ContactLastSeenHook is our hook for contact changes that require an update to last_seen_on
type ContactLastSeenHook struct{}
var ContactLastSeenHook models.EventCommitHook = &contactLastSeenHook{}

var contactLastSeenHook = &ContactLastSeenHook{}
type contactLastSeenHook struct{}

// Apply squashes and updates modified_on on all the contacts passed in
func (h *ContactLastSeenHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
func (h *contactLastSeenHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {

for scene, evts := range scenes {
lastEvent := evts[len(evts)-1].(flows.Event)
Expand Down
6 changes: 3 additions & 3 deletions hooks/contact_modified.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
)

// ContactModifiedHook is our hook for contact changes that require an update to modified_on
type ContactModifiedHook struct{}
var ContactModifiedHook models.EventCommitHook = &contactModifiedHook{}

var contactModifiedHook = &ContactModifiedHook{}
type contactModifiedHook struct{}

// Apply squashes and updates modified_on on all the contacts passed in
func (h *ContactModifiedHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
func (h *contactModifiedHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
// our lists of contact ids
contactIDs := make([]models.ContactID, 0, len(scenes))

Expand Down
8 changes: 4 additions & 4 deletions hooks/contact_name_changed.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ func init() {
}

// CommitNameChangesHook is our hook for name changes
type CommitNameChangesHook struct{}
var CommitNameChangesHook models.EventCommitHook = &commitNameChangesHook{}

var commitNameChangesHook = &CommitNameChangesHook{}
type commitNameChangesHook struct{}

// Apply commits our contact name changes as a bulk update for the passed in map of scene
func (h *CommitNameChangesHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
func (h *commitNameChangesHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
// build up our list of pairs of contact id and contact name
updates := make([]interface{}, 0, len(scenes))
for s, e := range scenes {
Expand All @@ -45,7 +45,7 @@ func handleContactNameChanged(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool,
"name": event.Name,
}).Debug("changing contact name")

scene.AppendToEventPreCommitHook(commitNameChangesHook, event)
scene.AppendToEventPreCommitHook(CommitNameChangesHook, event)
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions hooks/contact_status_changed.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func init() {
}

// CommitStatusChangesHook is our hook for status changes
type CommitStatusChangesHook struct{}
var CommitStatusChangesHook models.EventCommitHook = &commitStatusChangesHook{}

var commitStatusChangesHook = &CommitStatusChangesHook{}
type commitStatusChangesHook struct{}

// Apply commits our contact status change
func (h *CommitStatusChangesHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
func (h *commitStatusChangesHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {

statusChanges := make([]*models.ContactStatusChange, 0, len(scenes))
for scene, es := range scenes {
Expand All @@ -47,6 +47,6 @@ func handleContactStatusChanged(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool
"status": event.Status,
}).Debug("updating contact status")

scene.AppendToEventPreCommitHook(commitStatusChangesHook, event)
scene.AppendToEventPreCommitHook(CommitStatusChangesHook, event)
return nil
}
10 changes: 5 additions & 5 deletions hooks/contact_urns_changed.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func init() {
}

// CommitURNChangesHook is our hook for when a URN is added to a contact
type CommitURNChangesHook struct{}
var CommitURNChangesHook models.EventCommitHook = &commitURNChangesHook{}

var commitURNChangesHook = &CommitURNChangesHook{}
type commitURNChangesHook struct{}

// Apply adds all our URNS in a batch
func (h *CommitURNChangesHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
func (h *commitURNChangesHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
// gather all our urn changes, we only care about the last change for each scene
changes := make([]*models.ContactURNsChanged, 0, len(scenes))
for _, sessionChanges := range scenes {
Expand Down Expand Up @@ -54,8 +54,8 @@ func handleContactURNsChanged(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool,
}

// add our callback
scene.AppendToEventPreCommitHook(commitURNChangesHook, change)
scene.AppendToEventPreCommitHook(contactModifiedHook, scene.ContactID())
scene.AppendToEventPreCommitHook(CommitURNChangesHook, change)
scene.AppendToEventPreCommitHook(ContactModifiedHook, scene.ContactID())

return nil
}
8 changes: 4 additions & 4 deletions hooks/input_labels_added.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ func init() {
}

// CommitAddedLabelsHook is our hook for input labels being added
type CommitAddedLabelsHook struct{}
var CommitAddedLabelsHook models.EventCommitHook = &commitAddedLabelsHook{}

var commitAddedLabelsHook = &CommitAddedLabelsHook{}
type commitAddedLabelsHook struct{}

// Apply applies our input labels added, committing them in a single batch
func (h *CommitAddedLabelsHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
func (h *commitAddedLabelsHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
// build our list of msg label adds, we dedupe these so we never double add in the same transaction
seen := make(map[string]bool)
adds := make([]*models.MsgLabelAdd, 0, len(scenes))
Expand Down Expand Up @@ -67,7 +67,7 @@ func handleInputLabelsAdded(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa
return errors.Errorf("cannot add label, no incoming message for scene: %d", scene.SessionID())
}

scene.AppendToEventPreCommitHook(commitAddedLabelsHook, &models.MsgLabelAdd{
scene.AppendToEventPreCommitHook(CommitAddedLabelsHook, &models.MsgLabelAdd{
MsgID: scene.Session().IncomingMsgID(),
LabelID: label.ID(),
})
Expand Down
8 changes: 4 additions & 4 deletions hooks/ivr_created.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func init() {
}

// CommitIVRHook is our hook for comitting scene messages / say commands
type CommitIVRHook struct{}
var CommitIVRHook models.EventCommitHook = &commitIVRHook{}

var commitIVRHook = &CommitIVRHook{}
type commitIVRHook struct{}

// Apply takes care of inserting all the messages in the passed in scene assigning topups to them as needed.
func (h *CommitIVRHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
func (h *commitIVRHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
msgs := make([]*models.Msg, 0, len(scenes))
for _, s := range scenes {
for _, m := range s {
Expand Down Expand Up @@ -78,7 +78,7 @@ func handleIVRCreated(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *mode
}

// register to have this message committed
scene.AppendToEventPreCommitHook(commitIVRHook, msg)
scene.AppendToEventPreCommitHook(CommitIVRHook, msg)

return nil
}
8 changes: 4 additions & 4 deletions hooks/msg_created.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func init() {
}

// SendMessagesHook is our hook for sending scene messages
type SendMessagesHook struct{}
var SendMessagesHook models.EventCommitHook = &sendMessagesHook{}

var sendMessagesHook = &SendMessagesHook{}
type sendMessagesHook struct{}

// Apply sends all non-android messages to courier
func (h *SendMessagesHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
func (h *sendMessagesHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
rc := rp.Get()
defer rc.Close()

Expand Down Expand Up @@ -231,7 +231,7 @@ func handleMsgCreated(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *mode

// don't send messages for surveyor flows
if scene.Session().SessionType() != models.SurveyorFlow {
scene.AppendToEventPostCommitHook(sendMessagesHook, msg)
scene.AppendToEventPostCommitHook(SendMessagesHook, msg)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions hooks/msg_received.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func handleMsgReceived(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *mod
}

// update the contact's last seen date
scene.AppendToEventPreCommitHook(contactLastSeenHook, event)
scene.AppendToEventPreCommitHook(updateCampaignEventsHook, event)
scene.AppendToEventPreCommitHook(ContactLastSeenHook, event)
scene.AppendToEventPreCommitHook(UpdateCampaignEventsHook, event)

return nil
}
8 changes: 4 additions & 4 deletions hooks/resthook_called.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func init() {
}

// InsertWebhookEventHook is our hook for when a resthook needs to be inserted
type InsertWebhookEventHook struct{}
var InsertWebhookEventHook models.EventCommitHook = &insertWebhookEventHook{}

var insertWebhookEventHook = &InsertWebhookEventHook{}
type insertWebhookEventHook struct{}

// Apply inserts all the webook events that were created
func (h *InsertWebhookEventHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
func (h *insertWebhookEventHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error {
events := make([]*models.WebhookEvent, 0, len(scenes))
for _, rs := range scenes {
for _, r := range rs {
Expand Down Expand Up @@ -61,7 +61,7 @@ func handleResthookCalled(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *
string(event.Payload),
event.CreatedOn(),
)
scene.AppendToEventPreCommitHook(insertWebhookEventHook, re)
scene.AppendToEventPreCommitHook(InsertWebhookEventHook, re)

return nil
}
Loading

0 comments on commit 3829e7a

Please sign in to comment.