Skip to content

Commit

Permalink
Switch channelevent.extra to always be strings
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Oct 4, 2023
1 parent 048fc88 commit ceab9b0
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 65 deletions.
12 changes: 6 additions & 6 deletions backends/rapidpro/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1265,7 +1265,7 @@ func (ts *BackendTestSuite) TestChannelEvent() {
clog := courier.NewChannelLog(courier.ChannelLogTypeUnknown, channel, nil)
urn, _ := urns.NewTelURNForCountry("12065551616", channel.Country())

event := ts.b.NewChannelEvent(channel, courier.EventTypeReferral, urn, clog).WithExtra(map[string]any{"ref_id": "12345"}).WithContactName("kermit frog")
event := ts.b.NewChannelEvent(channel, courier.EventTypeReferral, urn, clog).WithExtra(map[string]string{"ref_id": "12345"}).WithContactName("kermit frog")
err := ts.b.WriteChannelEvent(ctx, event, clog)
ts.NoError(err)

Expand All @@ -1276,18 +1276,18 @@ func (ts *BackendTestSuite) TestChannelEvent() {
dbE := event.(*ChannelEvent)
dbE = readChannelEventFromDB(ts.b, dbE.ID_)
ts.Equal(dbE.EventType_, courier.EventTypeReferral)
ts.Equal(map[string]any{"ref_id": "12345"}, dbE.Extra())
ts.Equal(map[string]string{"ref_id": "12345"}, dbE.Extra())
ts.Equal(contact.ID_, dbE.ContactID_)
ts.Equal(contact.URNID_, dbE.ContactURNID_)

event = ts.b.NewChannelEvent(channel, courier.EventTypeOptIn, urn, clog).WithExtra(map[string]any{"optin_id": "1", "optin_name": "Polls"})
event = ts.b.NewChannelEvent(channel, courier.EventTypeOptIn, urn, clog).WithExtra(map[string]string{"title": "Polls", "payload": "1"})
err = ts.b.WriteChannelEvent(ctx, event, clog)
ts.NoError(err)

dbE = event.(*ChannelEvent)
dbE = readChannelEventFromDB(ts.b, dbE.ID_)
ts.Equal(dbE.EventType_, courier.EventTypeOptIn)
ts.Equal(map[string]any{"optin_id": "1", "optin_name": "Polls"}, dbE.Extra())
ts.Equal(map[string]string{"title": "Polls", "payload": "1"}, dbE.Extra())
ts.Equal(null.Int(1), dbE.OptInID_)
}

Expand All @@ -1314,7 +1314,7 @@ func (ts *BackendTestSuite) TestMailroomEvents() {
clog := courier.NewChannelLog(courier.ChannelLogTypeUnknown, channel, nil)
urn, _ := urns.NewTelURNForCountry("12065551616", channel.Country())

event := ts.b.NewChannelEvent(channel, courier.EventTypeReferral, urn, clog).WithExtra(map[string]any{"ref_id": "12345"}).
event := ts.b.NewChannelEvent(channel, courier.EventTypeReferral, urn, clog).WithExtra(map[string]string{"ref_id": "12345"}).
WithContactName("kermit frog").
WithOccurredOn(time.Date(2020, 8, 5, 13, 30, 0, 123456789, time.UTC))
err := ts.b.WriteChannelEvent(ctx, event, clog)
Expand All @@ -1327,7 +1327,7 @@ func (ts *BackendTestSuite) TestMailroomEvents() {
dbE := event.(*ChannelEvent)
dbE = readChannelEventFromDB(ts.b, dbE.ID_)
ts.Equal(dbE.EventType_, courier.EventTypeReferral)
ts.Equal(map[string]any{"ref_id": "12345"}, dbE.Extra())
ts.Equal(map[string]string{"ref_id": "12345"}, dbE.Extra())
ts.Equal(contact.ID_, dbE.ContactID_)
ts.Equal(contact.URNID_, dbE.ContactURNID_)

Expand Down
19 changes: 10 additions & 9 deletions backends/rapidpro/channel_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type ChannelEvent struct {
URN_ urns.URN `json:"urn" db:"urn"`
EventType_ courier.ChannelEventType `json:"event_type" db:"event_type"`
OptInID_ null.Int `json:"optin_id" db:"optin_id"`
Extra_ null.Map[any] `json:"extra" db:"extra"`
Extra_ null.Map[string] `json:"extra" db:"extra"`
OccurredOn_ time.Time `json:"occurred_on" db:"occurred_on"`
CreatedOn_ time.Time `json:"created_on" db:"created_on"`
LogUUIDs pq.StringArray `json:"log_uuids" db:"log_uuids"`
Expand Down Expand Up @@ -83,7 +83,7 @@ func (e *ChannelEvent) ChannelID() courier.ChannelID { return e.ChannelID
func (e *ChannelEvent) ChannelUUID() courier.ChannelUUID { return e.ChannelUUID_ }
func (e *ChannelEvent) EventType() courier.ChannelEventType { return e.EventType_ }
func (e *ChannelEvent) URN() urns.URN { return e.URN_ }
func (e *ChannelEvent) Extra() map[string]any { return e.Extra_ }
func (e *ChannelEvent) Extra() map[string]string { return e.Extra_ }
func (e *ChannelEvent) OccurredOn() time.Time { return e.OccurredOn_ }
func (e *ChannelEvent) CreatedOn() time.Time { return e.CreatedOn_ }
func (e *ChannelEvent) Channel() *Channel { return e.channel }
Expand All @@ -98,15 +98,16 @@ func (e *ChannelEvent) WithURNAuthTokens(tokens map[string]string) courier.Chann
return e
}

func (e *ChannelEvent) WithExtra(extra map[string]any) courier.ChannelEvent {
optInID, ok := extra["optin_id"]
if ok {
asStr, _ := optInID.(string)
asInt, _ := strconv.Atoi(asStr)
e.OptInID_ = null.Int(asInt)
func (e *ChannelEvent) WithExtra(extra map[string]string) courier.ChannelEvent {
if e.EventType_ == courier.EventTypeOptIn || e.EventType_ == courier.EventTypeOptOut {
optInID := extra["payload"]
if optInID != "" {
asInt, _ := strconv.Atoi(optInID)
e.OptInID_ = null.Int(asInt)
}
}

e.Extra_ = null.Map[any](extra)
e.Extra_ = null.Map[string](extra)
return e
}

Expand Down
4 changes: 2 additions & 2 deletions channel_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ type ChannelEvent interface {
ChannelUUID() ChannelUUID
URN() urns.URN
EventType() ChannelEventType
Extra() map[string]any
Extra() map[string]string
CreatedOn() time.Time
OccurredOn() time.Time

WithContactName(name string) ChannelEvent
WithURNAuthTokens(tokens map[string]string) ChannelEvent
WithExtra(extra map[string]any) ChannelEvent
WithExtra(extra map[string]string) ChannelEvent
WithOccurredOn(time.Time) ChannelEvent
}
11 changes: 3 additions & 8 deletions handlers/facebook_legacy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,7 @@ func (h *handler) receiveEvents(ctx context.Context, channel courier.Channel, w
event := h.Backend().NewChannelEvent(channel, courier.EventTypeReferral, urn, clog).WithOccurredOn(date)

// build our extra
extra := map[string]any{
referrerIDKey: msg.OptIn.Ref,
}
extra := map[string]string{referrerIDKey: msg.OptIn.Ref}
event = event.WithExtra(extra)

err := h.Backend().WriteChannelEvent(ctx, event, clog)
Expand All @@ -295,7 +293,7 @@ func (h *handler) receiveEvents(ctx context.Context, channel courier.Channel, w
event := h.Backend().NewChannelEvent(channel, eventType, urn, clog).WithOccurredOn(date)

// build our extra
extra := map[string]any{
extra := map[string]string{
titleKey: msg.Postback.Title,
payloadKey: msg.Postback.Payload,
}
Expand Down Expand Up @@ -326,10 +324,7 @@ func (h *handler) receiveEvents(ctx context.Context, channel courier.Channel, w
event := h.Backend().NewChannelEvent(channel, courier.EventTypeReferral, urn, clog).WithOccurredOn(date)

// build our extra
extra := map[string]any{
sourceKey: msg.Referral.Source,
typeKey: msg.Referral.Type,
}
extra := map[string]string{sourceKey: msg.Referral.Source, typeKey: msg.Referral.Type}

// add referrer id if present
if msg.Referral.Ref != "" {
Expand Down
12 changes: 6 additions & 6 deletions handlers/facebook_legacy/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ var testCases = []IncomingTestCase{
ExpectedRespStatus: 200,
ExpectedBodyContains: "Handled",
ExpectedEvents: []ExpectedEvent{
{Type: courier.EventTypeReferral, URN: "facebook:ref:optin_user_ref", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]any{"referrer_id": "optin_ref"}},
{Type: courier.EventTypeReferral, URN: "facebook:ref:optin_user_ref", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]string{"referrer_id": "optin_ref"}},
},
},
{
Expand All @@ -492,7 +492,7 @@ var testCases = []IncomingTestCase{
ExpectedRespStatus: 200,
ExpectedBodyContains: "Handled",
ExpectedEvents: []ExpectedEvent{
{Type: courier.EventTypeReferral, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]any{"referrer_id": "optin_ref"}},
{Type: courier.EventTypeReferral, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]string{"referrer_id": "optin_ref"}},
},
},
{
Expand All @@ -502,7 +502,7 @@ var testCases = []IncomingTestCase{
ExpectedRespStatus: 200,
ExpectedBodyContains: "Handled",
ExpectedEvents: []ExpectedEvent{
{Type: courier.EventTypeNewConversation, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]any{"title": "postback title", "payload": "get_started"}},
{Type: courier.EventTypeNewConversation, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]string{"title": "postback title", "payload": "get_started"}},
},
},
{
Expand All @@ -512,7 +512,7 @@ var testCases = []IncomingTestCase{
ExpectedRespStatus: 200,
ExpectedBodyContains: "Handled",
ExpectedEvents: []ExpectedEvent{
{Type: courier.EventTypeReferral, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]any{"title": "postback title", "payload": "postback payload", "referrer_id": "postback ref", "source": "postback source", "type": "postback type"}},
{Type: courier.EventTypeReferral, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]string{"title": "postback title", "payload": "postback payload", "referrer_id": "postback ref", "source": "postback source", "type": "postback type"}},
},
},
{
Expand All @@ -522,7 +522,7 @@ var testCases = []IncomingTestCase{
ExpectedRespStatus: 200,
ExpectedBodyContains: "Handled",
ExpectedEvents: []ExpectedEvent{
{Type: courier.EventTypeReferral, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]any{"title": "postback title", "payload": "get_started", "referrer_id": "postback ref", "source": "postback source", "type": "postback type", "ad_id": "ad id"}},
{Type: courier.EventTypeReferral, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]string{"title": "postback title", "payload": "get_started", "referrer_id": "postback ref", "source": "postback source", "type": "postback type", "ad_id": "ad id"}},
},
},
{
Expand All @@ -532,7 +532,7 @@ var testCases = []IncomingTestCase{
ExpectedRespStatus: 200,
ExpectedBodyContains: `"referrer_id":"referral id"`,
ExpectedEvents: []ExpectedEvent{
{Type: courier.EventTypeReferral, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]any{"referrer_id": "referral id", "source": "referral source", "type": "referral type", "ad_id": "ad id"}},
{Type: courier.EventTypeReferral, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]string{"referrer_id": "referral id", "source": "referral source", "type": "referral type", "ad_id": "ad id"}},
},
},
{
Expand Down
16 changes: 8 additions & 8 deletions handlers/meta/facebook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ var facebookIncomingTests = []IncomingTestCase{
ExpectedRespStatus: 200,
ExpectedBodyContains: "Handled",
ExpectedEvents: []ExpectedEvent{
{Type: courier.EventTypeReferral, URN: "facebook:ref:optin_user_ref", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]any{"referrer_id": "optin_ref"}},
{Type: courier.EventTypeReferral, URN: "facebook:ref:optin_user_ref", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]string{"referrer_id": "optin_ref"}},
},
PrepRequest: addValidSignature,
},
Expand All @@ -111,7 +111,7 @@ var facebookIncomingTests = []IncomingTestCase{
ExpectedRespStatus: 200,
ExpectedBodyContains: "Handled",
ExpectedEvents: []ExpectedEvent{
{Type: courier.EventTypeReferral, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]any{"referrer_id": "optin_ref"}},
{Type: courier.EventTypeReferral, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]string{"referrer_id": "optin_ref"}},
},
PrepRequest: addValidSignature,
},
Expand All @@ -122,7 +122,7 @@ var facebookIncomingTests = []IncomingTestCase{
ExpectedRespStatus: 200,
ExpectedBodyContains: "Handled",
ExpectedEvents: []ExpectedEvent{
{Type: courier.EventTypeOptIn, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]any{"optin_id": "3456", "optin_name": "Bird Facts"}},
{Type: courier.EventTypeOptIn, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]string{"title": "Bird Facts", "payload": "3456"}},
},
ExpectedURNAuthTokens: map[urns.URN]map[string]string{"facebook:5678": {"optin:3456": "12345678901234567890"}},
PrepRequest: addValidSignature,
Expand All @@ -134,7 +134,7 @@ var facebookIncomingTests = []IncomingTestCase{
ExpectedRespStatus: 200,
ExpectedBodyContains: "Handled",
ExpectedEvents: []ExpectedEvent{
{Type: courier.EventTypeOptOut, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]any{"optin_id": "3456", "optin_name": "Bird Facts"}},
{Type: courier.EventTypeOptOut, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]string{"title": "Bird Facts", "payload": "3456"}},
},
ExpectedURNAuthTokens: map[urns.URN]map[string]string{"facebook:5678": {}},
PrepRequest: addValidSignature,
Expand All @@ -146,7 +146,7 @@ var facebookIncomingTests = []IncomingTestCase{
ExpectedRespStatus: 200,
ExpectedBodyContains: "Handled",
ExpectedEvents: []ExpectedEvent{
{Type: courier.EventTypeNewConversation, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]any{"title": "postback title", "payload": "get_started"}},
{Type: courier.EventTypeNewConversation, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]string{"title": "postback title", "payload": "get_started"}},
},
PrepRequest: addValidSignature,
},
Expand All @@ -157,7 +157,7 @@ var facebookIncomingTests = []IncomingTestCase{
ExpectedRespStatus: 200,
ExpectedBodyContains: "Handled",
ExpectedEvents: []ExpectedEvent{
{Type: courier.EventTypeReferral, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]any{"title": "postback title", "payload": "postback payload", "referrer_id": "postback ref", "source": "postback source", "type": "postback type"}},
{Type: courier.EventTypeReferral, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]string{"title": "postback title", "payload": "postback payload", "referrer_id": "postback ref", "source": "postback source", "type": "postback type"}},
},
PrepRequest: addValidSignature,
},
Expand All @@ -168,7 +168,7 @@ var facebookIncomingTests = []IncomingTestCase{
ExpectedRespStatus: 200,
ExpectedBodyContains: "Handled",
ExpectedEvents: []ExpectedEvent{
{Type: courier.EventTypeReferral, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]any{"title": "postback title", "payload": "get_started", "referrer_id": "postback ref", "source": "postback source", "type": "postback type", "ad_id": "ad id"}},
{Type: courier.EventTypeReferral, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]string{"title": "postback title", "payload": "get_started", "referrer_id": "postback ref", "source": "postback source", "type": "postback type", "ad_id": "ad id"}},
},
PrepRequest: addValidSignature,
},
Expand All @@ -179,7 +179,7 @@ var facebookIncomingTests = []IncomingTestCase{
ExpectedRespStatus: 200,
ExpectedBodyContains: `"referrer_id":"referral id"`,
ExpectedEvents: []ExpectedEvent{
{Type: courier.EventTypeReferral, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]any{"referrer_id": "referral id", "source": "referral source", "type": "referral type", "ad_id": "ad id"}},
{Type: courier.EventTypeReferral, URN: "facebook:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]string{"referrer_id": "referral id", "source": "referral source", "type": "referral type", "ad_id": "ad id"}},
},
PrepRequest: addValidSignature,
},
Expand Down
20 changes: 5 additions & 15 deletions handlers/meta/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,6 @@ func (h *handler) processFacebookInstagramPayload(ctx context.Context, channel c
if msg.OptIn.Type == "notification_messages" {
eventType := courier.EventTypeOptIn
authToken := msg.OptIn.NotificationMessagesToken
optInID := msg.OptIn.Payload
optInName := msg.OptIn.Title

if msg.OptIn.NotificationMessagesStatus == "STOP_NOTIFICATIONS" {
eventType = courier.EventTypeOptOut
Expand All @@ -448,8 +446,8 @@ func (h *handler) processFacebookInstagramPayload(ctx context.Context, channel c

event = h.Backend().NewChannelEvent(channel, eventType, urn, clog).
WithOccurredOn(date).
WithExtra(map[string]any{"optin_id": optInID, "optin_name": optInName}).
WithURNAuthTokens(map[string]string{fmt.Sprintf("optin:%s", optInID): authToken})
WithExtra(map[string]string{titleKey: msg.OptIn.Title, payloadKey: msg.OptIn.Payload}).
WithURNAuthTokens(map[string]string{fmt.Sprintf("optin:%s", msg.OptIn.Payload): authToken})
} else {

// this is an opt in, if we have a user_ref, use that as our URN (this is a checkbox plugin)
Expand All @@ -467,9 +465,7 @@ func (h *handler) processFacebookInstagramPayload(ctx context.Context, channel c

event = h.Backend().NewChannelEvent(channel, courier.EventTypeReferral, urn, clog).
WithOccurredOn(date).
WithExtra(map[string]any{
referrerIDKey: msg.OptIn.Ref,
})
WithExtra(map[string]string{referrerIDKey: msg.OptIn.Ref})
}

err := h.Backend().WriteChannelEvent(ctx, event, clog)
Expand All @@ -489,10 +485,7 @@ func (h *handler) processFacebookInstagramPayload(ctx context.Context, channel c
event := h.Backend().NewChannelEvent(channel, eventType, urn, clog).WithOccurredOn(date)

// build our extra
extra := map[string]any{
titleKey: msg.Postback.Title,
payloadKey: msg.Postback.Payload,
}
extra := map[string]string{titleKey: msg.Postback.Title, payloadKey: msg.Postback.Payload}

// add in referral information if we have it
if eventType == courier.EventTypeReferral {
Expand Down Expand Up @@ -520,10 +513,7 @@ func (h *handler) processFacebookInstagramPayload(ctx context.Context, channel c
event := h.Backend().NewChannelEvent(channel, courier.EventTypeReferral, urn, clog).WithOccurredOn(date)

// build our extra
extra := map[string]any{
sourceKey: msg.Referral.Source,
typeKey: msg.Referral.Type,
}
extra := map[string]string{sourceKey: msg.Referral.Source, typeKey: msg.Referral.Type}

// add referrer id if present
if msg.Referral.Ref != "" {
Expand Down
2 changes: 1 addition & 1 deletion handlers/meta/instagram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var instagramIncomingTests = []IncomingTestCase{
ExpectedRespStatus: 200,
ExpectedBodyContains: "Handled",
ExpectedEvents: []ExpectedEvent{
{Type: courier.EventTypeNewConversation, URN: "instagram:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]any{"title": "icebreaker question", "payload": "get_started"}},
{Type: courier.EventTypeNewConversation, URN: "instagram:5678", Time: time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC), Extra: map[string]string{"title": "icebreaker question", "payload": "get_started"}},
},
PrepRequest: addValidSignature,
},
Expand Down
2 changes: 1 addition & 1 deletion handlers/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type ExpectedEvent struct {
Type courier.ChannelEventType
URN urns.URN
Time time.Time
Extra map[string]any
Extra map[string]string
}

// IncomingTestCase defines the test values for a particular test case
Expand Down
Loading

0 comments on commit ceab9b0

Please sign in to comment.