Skip to content

Commit

Permalink
Fix FCM message struct and token source initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Aug 28, 2024
1 parent e7d0e9d commit 96f56f6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
53 changes: 27 additions & 26 deletions handlers/firebase/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import (
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/gocommon/urns"
"google.golang.org/api/idtoken"
"google.golang.org/api/option"
"golang.org/x/oauth2/google"
)

const (
Expand Down Expand Up @@ -132,19 +131,21 @@ func (h *handler) registerContact(ctx context.Context, channel courier.Channel,
}

type mtPayload struct {
Data struct {
Type string `json:"type"`
Title string `json:"title"`
Message string `json:"message"`
MessageID int64 `json:"message_id"`
SessionStatus string `json:"session_status"`
QuickReplies []string `json:"quick_replies,omitempty"`
} `json:"data"`
Notification *mtNotification `json:"notification,omitempty"`
Token string `json:"token"`
Android struct {
Priority string `json:"priority"`
} `json:"android,omitempty"`
Message struct {
Data struct {
Type string `json:"type"`
Title string `json:"title"`
Message string `json:"message"`
MessageID string `json:"message_id"`
SessionStatus string `json:"session_status"`
QuickReplies []string `json:"quick_replies,omitempty"`
} `json:"data"`
Notification *mtNotification `json:"notification,omitempty"`
Token string `json:"token"`
Android struct {
Priority string `json:"priority"`
} `json:"android,omitempty"`
} `json:"message"`
}

type mtAPIKeyPayload struct {
Expand Down Expand Up @@ -278,21 +279,21 @@ func (h *handler) sendWithCredsJSON(ctx context.Context, msg courier.MsgOut, res
for i, part := range msgParts {
payload := mtPayload{}

payload.Data.Type = "rapidpro"
payload.Data.Title = title
payload.Data.Message = part
payload.Data.MessageID = int64(msg.ID())
payload.Data.SessionStatus = msg.SessionStatus()
payload.Message.Data.Type = "rapidpro"
payload.Message.Data.Title = title
payload.Message.Data.Message = part
payload.Message.Data.MessageID = msg.ID().String()
payload.Message.Data.SessionStatus = msg.SessionStatus()

if i == len(msgParts)-1 {
payload.Data.QuickReplies = msg.QuickReplies()
payload.Message.Data.QuickReplies = msg.QuickReplies()
}

payload.Token = msg.URNAuth()
payload.Android.Priority = "high"
payload.Message.Token = msg.URNAuth()
payload.Message.Android.Priority = "high"

if notification {
payload.Notification = &mtNotification{
payload.Message.Notification = &mtNotification{
Title: title,
Body: part,
}
Expand Down Expand Up @@ -378,9 +379,9 @@ func (h *handler) fetchAccessToken(ctx context.Context, channel courier.Channel,
var credentialsFileJSON map[string]string
jsonx.MustUnmarshal(jsonx.MustMarshal(credentialsFile), &credentialsFileJSON)

sendURL := fmt.Sprintf("https://fcm.googleapis.com/v1/projects/%s/messages:send", credentialsFileJSON["project_id"])
scopes := []string{"https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/firebase.messaging "}

Check warning on line 382 in handlers/firebase/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/firebase/handler.go#L382

Added line #L382 was not covered by tests

ts, err := idtoken.NewTokenSource(ctx, sendURL, option.WithCredentialsJSON(jsonx.MustMarshal(credentialsFileJSON)))
ts, err := google.JWTAccessTokenSourceWithScope(jsonx.MustMarshal(credentialsFileJSON), scopes...)

Check warning on line 384 in handlers/firebase/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/firebase/handler.go#L384

Added line #L384 was not covered by tests
if err != nil {
return "", 0, fmt.Errorf("failed to create NewTokenSource: %w", err)
}
Expand Down
16 changes: 8 additions & 8 deletions handlers/firebase/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ var notificationSendTestCases = []OutgoingTestCase{
},
ExpectedRequests: []ExpectedRequest{{
Headers: map[string]string{"Authorization": "Bearer FCMToken"},
Body: `{"data":{"type":"rapidpro","title":"FCMTitle","message":"Simple Message","message_id":10,"session_status":""},"notification":{"title":"FCMTitle","body":"Simple Message"},"token":"auth1","android":{"priority":"high"}}`,
Body: `{"message":{"data":{"type":"rapidpro","title":"FCMTitle","message":"Simple Message","message_id":"10","session_status":""},"notification":{"title":"FCMTitle","body":"Simple Message"},"token":"auth1","android":{"priority":"high"}}}`,
}},
ExpectedExtIDs: []string{"123456-a"},
},
Expand All @@ -294,7 +294,7 @@ var sendTestCases = []OutgoingTestCase{
},
ExpectedRequests: []ExpectedRequest{{
Headers: map[string]string{"Authorization": "Bearer FCMToken"},
Body: `{"data":{"type":"rapidpro","title":"FCMTitle","message":"Simple Message","message_id":10,"session_status":""},"token":"auth1","android":{"priority":"high"}}`,
Body: `{"message":{"data":{"type":"rapidpro","title":"FCMTitle","message":"Simple Message","message_id":"10","session_status":""},"token":"auth1","android":{"priority":"high"}}}`,
}},
ExpectedExtIDs: []string{"123456-a"},
},
Expand All @@ -312,11 +312,11 @@ var sendTestCases = []OutgoingTestCase{
ExpectedRequests: []ExpectedRequest{
{
Headers: map[string]string{"Authorization": "Bearer FCMToken"},
Body: `{"data":{"type":"rapidpro","title":"FCMTitle","message":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas convallis augue vel placerat congue.\nEtiam nec tempus enim. Cras placerat at est vel suscipit. Duis quis faucibus metus, non elementum tortor.\nPellentesque posuere ullamcorper metus auctor venenatis. Proin eget hendrerit dui. Sed eget massa nec mauris consequat pretium.\nPraesent mattis arcu tortor, ac aliquet turpis tincidunt eu.\n\nFusce ut lacinia augue. Vestibulum felis nisi, porta ut est condimentum, condimentum volutpat libero.\nSuspendisse a elit venenatis, condimentum sem at, ultricies mauris. Morbi interdum sem id tempor tristique.\nUt tincidunt massa eu purus lacinia sodales a volutpat neque. Cras dolor quam, eleifend a rhoncus quis, sodales nec purus.\nVivamus justo dolor, gravida at quam eu, hendrerit rutrum justo. Sed hendrerit nisi vitae nisl ornare tristique.\nProin vulputate id justo non aliquet.\n\nDuis eu arcu pharetra, laoreet nunc at, pharetra sapien. Nulla eu libero diam.\nDonec euismod dapibus ligula, sit amet hendrerit neque vulput","message_id":10,"session_status":""},"token":"auth1","android":{"priority":"high"}}`,
Body: `{"message":{"data":{"type":"rapidpro","title":"FCMTitle","message":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas convallis augue vel placerat congue.\nEtiam nec tempus enim. Cras placerat at est vel suscipit. Duis quis faucibus metus, non elementum tortor.\nPellentesque posuere ullamcorper metus auctor venenatis. Proin eget hendrerit dui. Sed eget massa nec mauris consequat pretium.\nPraesent mattis arcu tortor, ac aliquet turpis tincidunt eu.\n\nFusce ut lacinia augue. Vestibulum felis nisi, porta ut est condimentum, condimentum volutpat libero.\nSuspendisse a elit venenatis, condimentum sem at, ultricies mauris. Morbi interdum sem id tempor tristique.\nUt tincidunt massa eu purus lacinia sodales a volutpat neque. Cras dolor quam, eleifend a rhoncus quis, sodales nec purus.\nVivamus justo dolor, gravida at quam eu, hendrerit rutrum justo. Sed hendrerit nisi vitae nisl ornare tristique.\nProin vulputate id justo non aliquet.\n\nDuis eu arcu pharetra, laoreet nunc at, pharetra sapien. Nulla eu libero diam.\nDonec euismod dapibus ligula, sit amet hendrerit neque vulput","message_id":"10","session_status":""},"token":"auth1","android":{"priority":"high"}}}`,
},
{
Headers: map[string]string{"Authorization": "Bearer FCMToken"},
Body: `{"data":{"type":"rapidpro","title":"FCMTitle","message":"ate ac.","message_id":10,"session_status":""},"token":"auth1","android":{"priority":"high"}}`,
Body: `{"message":{"data":{"type":"rapidpro","title":"FCMTitle","message":"ate ac.","message_id":"10","session_status":""},"token":"auth1","android":{"priority":"high"}}}`,
},
},
ExpectedExtIDs: []string{"123456-a", "123456-a"},
Expand All @@ -335,7 +335,7 @@ var sendTestCases = []OutgoingTestCase{
},
ExpectedRequests: []ExpectedRequest{{
Headers: map[string]string{"Authorization": "Bearer FCMToken"},
Body: `{"data":{"type":"rapidpro","title":"FCMTitle","message":"Simple Message\nhttps://foo.bar","message_id":10,"session_status":"","quick_replies":["yes","no"]},"token":"auth1","android":{"priority":"high"}}`,
Body: `{"message":{"data":{"type":"rapidpro","title":"FCMTitle","message":"Simple Message\nhttps://foo.bar","message_id":"10","session_status":"","quick_replies":["yes","no"]},"token":"auth1","android":{"priority":"high"}}}`,
}},
ExpectedExtIDs: []string{"123456-a"},
},
Expand All @@ -351,7 +351,7 @@ var sendTestCases = []OutgoingTestCase{
},
ExpectedRequests: []ExpectedRequest{{
Headers: map[string]string{"Authorization": "Bearer FCMToken"},
Body: `{"data":{"type":"rapidpro","title":"FCMTitle","message":"Error","message_id":10,"session_status":""},"token":"auth1","android":{"priority":"high"}}`,
Body: `{"message":{"data":{"type":"rapidpro","title":"FCMTitle","message":"Error","message_id":"10","session_status":""},"token":"auth1","android":{"priority":"high"}}}`,
}},
ExpectedError: courier.ErrResponseUnexpected,
},
Expand All @@ -367,7 +367,7 @@ var sendTestCases = []OutgoingTestCase{
},
ExpectedRequests: []ExpectedRequest{{
Headers: map[string]string{"Authorization": "Bearer FCMToken"},
Body: `{"data":{"type":"rapidpro","title":"FCMTitle","message":"Error","message_id":10,"session_status":""},"token":"auth1","android":{"priority":"high"}}`,
Body: `{"message":{"data":{"type":"rapidpro","title":"FCMTitle","message":"Error","message_id":"10","session_status":""},"token":"auth1","android":{"priority":"high"}}}`,
}},
ExpectedError: courier.ErrResponseUnexpected,
},
Expand All @@ -383,7 +383,7 @@ var sendTestCases = []OutgoingTestCase{
},
ExpectedRequests: []ExpectedRequest{{
Headers: map[string]string{"Authorization": "Bearer FCMToken"},
Body: `{"data":{"type":"rapidpro","title":"FCMTitle","message":"Error","message_id":10,"session_status":""},"token":"auth1","android":{"priority":"high"}}`,
Body: `{"message":{"data":{"type":"rapidpro","title":"FCMTitle","message":"Error","message_id":"10","session_status":""},"token":"auth1","android":{"priority":"high"}}}`,
}},
ExpectedError: courier.ErrConnectionFailed,
},
Expand Down

0 comments on commit 96f56f6

Please sign in to comment.