Skip to content

Commit

Permalink
Merge pull request #665 from nyaruka/fix-FBA-referral-dates
Browse files Browse the repository at this point in the history
Fix FBA timestamps that sometimes are in seconds instead of milliseconds
  • Loading branch information
rowanseymour authored Dec 4, 2023
2 parents 80e3f68 + 2041140 commit 6689928
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
11 changes: 11 additions & 0 deletions handlers/meta/facebook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,17 @@ var facebookIncomingTests = []IncomingTestCase{
},
PrepRequest: addValidSignature,
},
{
Label: "Receive Referral timestamp seconds",
URL: "/c/fba/receive",
Data: string(test.ReadFile("./testdata/fba/referral_seconds.json")),
ExpectedRespStatus: 200,
ExpectedBodyContains: `"referrer_id":"referral id"`,
ExpectedEvents: []ExpectedEvent{
{Type: courier.EventTypeReferral, URN: "facebook:5678", Time: time.Date(2023, 12, 3, 10, 25, 11, 0, time.UTC), Extra: map[string]string{"referrer_id": "referral id", "source": "referral source", "type": "referral type", "ad_id": "ad id"}},
},
PrepRequest: addValidSignature,
},
{
Label: "Receive Fallback Attachment",
URL: "/c/fba/receive",
Expand Down
11 changes: 9 additions & 2 deletions handlers/meta/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,15 @@ func (h *handler) processFacebookInstagramPayload(ctx context.Context, channel c
continue
}

// create our date from the timestamp (they give us millis, arg is nanos)
date := time.Unix(0, msg.Timestamp*1000000).UTC()
var date time.Time

if msg.Timestamp >= 10_000_000_000 {
// create our date from the timestamp (they give us millis, arg is nanos)
date = time.Unix(0, msg.Timestamp*1000000).UTC()
} else {
// create our date from the timestamp (they give us seconds)
date = time.Unix(msg.Timestamp, 0).UTC()
}

sender := msg.Sender.UserRef
if sender == "" {
Expand Down
27 changes: 27 additions & 0 deletions handlers/meta/testdata/fba/referral_seconds.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"object": "page",
"entry": [
{
"id": "12345",
"messaging": [
{
"referral": {
"ref": "referral id",
"ad_id": "ad id",
"source": "referral source",
"type": "referral type"
},
"recipient": {
"id": "12345"
},
"sender": {
"id": "5678",
"user_ref": "5678"
},
"timestamp": 1701599111
}
],
"time": 1701599116216
}
]
}

0 comments on commit 6689928

Please sign in to comment.