Skip to content

Commit

Permalink
Wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelreiswildlife committed Mar 22, 2024
1 parent 9569c57 commit de06d26
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 27 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

MOCKGENERATE := go run github.com/golang/mock/mockgen@v1.7.0-rc.1
MOCKGENERATE := go run go.uber.org/mock/mockgen@v0.4.0
GINKGO := go run github.com/onsi/ginkgo/[email protected]

build:
Expand Down Expand Up @@ -46,7 +46,7 @@ run:
@go run main.go

gcm:
@go run main.go gcm --senderId=test --apiKey=123
@go run main.go gcm

apns:
@go run main.go apns --certificate=./tls/_fixtures/certificate-valid.pem
Expand Down Expand Up @@ -171,6 +171,6 @@ integration-test-container-dev: build-image-dev start-deps-container-dev test-db
pusher:local make run-integration-test
@$(MAKE) stop-deps

# .PHONY: mocks
# mocks:
# $(MOCKGENERATE) -package=mocks -source=interfaces/apns.go -destination=mocks/apns.go
.PHONY: mocks
mocks:
$(MOCKGENERATE) -source=interfaces/client.go -destination=mocks/firebase/client.go
60 changes: 46 additions & 14 deletions extensions/client/firebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"encoding/json"
firebase "firebase.google.com/go/v4"
"firebase.google.com/go/v4/messaging"
"github.com/sirupsen/logrus"
Expand All @@ -19,7 +20,14 @@ var _ interfaces.PushClient = &firebaseClientImpl{}

func NewFirebaseClient(jsonCredentials string, logger *logrus.Logger) (interfaces.PushClient, error) {
ctx := context.Background()
app, err := firebase.NewApp(ctx, nil, option.WithCredentialsJSON([]byte(jsonCredentials)))
projectID, err := getProjectIDFromJson(jsonCredentials)
if err != nil {
return nil, err
}
cfg := &firebase.Config{
ProjectID: projectID,
}
app, err := firebase.NewApp(ctx, cfg, option.WithCredentialsJSON([]byte(jsonCredentials)))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -55,15 +63,31 @@ func (f *firebaseClientImpl) SendPush(ctx context.Context, msg interfaces.Messag
return nil
}

func getProjectIDFromJson(jsonStr string) (string, error) {
var data map[string]interface{}
err := json.Unmarshal([]byte(jsonStr), &data)
if err != nil {
return "", err
}

return data["project_id"].(string), nil
}

func toFirebaseMessage(message interfaces.Message) messaging.Message {
firebaseMessage := messaging.Message{
Data: nil,
Notification: &messaging.Notification{
Token: message.To,
}

if message.Data != nil {
firebaseMessage.Data = toMapString(message.Data)
}
if message.Notification != nil {
firebaseMessage.Notification = &messaging.Notification{
Title: message.Notification.Title,
Body: message.Notification.Body,
ImageURL: message.Notification.Icon,
},
Android: &messaging.AndroidConfig{
}
firebaseMessage.Android = &messaging.AndroidConfig{
CollapseKey: message.CollapseKey,
Priority: message.Priority,
Notification: &messaging.AndroidNotification{
Expand All @@ -77,8 +101,14 @@ func toFirebaseMessage(message interfaces.Message) messaging.Message {
BodyLocKey: message.Notification.BodyLocKey,
TitleLocKey: message.Notification.TitleLocKey,
},
},
Token: message.To,
}
if message.Notification.BodyLocArgs != "" {
firebaseMessage.Android.Notification.BodyLocArgs = []string{message.Notification.BodyLocArgs}
}

if message.Notification.TitleLocArgs != "" {
firebaseMessage.Android.Notification.TitleLocArgs = []string{message.Notification.TitleLocArgs}
}
}

if message.TimeToLive != nil {
Expand All @@ -87,13 +117,15 @@ func toFirebaseMessage(message interfaces.Message) messaging.Message {
firebaseMessage.Android.TTL = &ttl
}

if message.Notification.BodyLocArgs != "" {
firebaseMessage.Android.Notification.BodyLocArgs = []string{message.Notification.BodyLocArgs}
}
return firebaseMessage
}

if message.Notification.TitleLocArgs != "" {
firebaseMessage.Android.Notification.TitleLocArgs = []string{message.Notification.TitleLocArgs}
func toMapString(data interfaces.Data) map[string]string {
result := make(map[string]string)
for k, v := range data {
if str, ok := v.(string); ok {
result[k] = str
}
}

return firebaseMessage
return result
}
9 changes: 9 additions & 0 deletions extensions/handler/feedback_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package handler

// FeedbackResponse struct is sent to feedback reporters
// in order to keep the format expected by it
type FeedbackResponse struct {
From string `json:"from,omitempty"`
Error string `json:"error,omitempty"`
ErrorDescription string `json:"error_description,omitempty"`
}
23 changes: 18 additions & 5 deletions extensions/handler/message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ func (h *messageHandler) HandleMessages(ctx context.Context, msg interfaces.Kafk
h.stats.failures++
h.statsMutex.Unlock()

h.statsReporterHandleNotificationFailure(err)
h.handleNotificationFailure(err)
return
}

h.statsReporterHandleNotificationSent()
h.handleNotificationSent()

h.statsMutex.Lock()
h.stats.sent++
Expand Down Expand Up @@ -140,18 +139,32 @@ func (h *messageHandler) sendToFeedbackReporters(res interface{}) error {
return nil
}

func (h *messageHandler) statsReporterHandleNotificationSent() {
func (h *messageHandler) handleNotificationSent() {
for _, statsReporter := range h.statsReporters {
statsReporter.HandleNotificationSent(h.app, "gcm")
statsReporter.HandleNotificationSuccess(h.app, "gcm")
}

for _, feedbackReporter := range h.feedbackReporters {
r := &FeedbackResponse{}
b, _ := json.Marshal(r)
feedbackReporter.SendFeedback(h.app, "gcm", b)
}
}

func (h *messageHandler) statsReporterHandleNotificationFailure(err error) {
func (h *messageHandler) handleNotificationFailure(err error) {
pushError := translateToPushError(err)
for _, statsReporter := range h.statsReporters {
statsReporter.HandleNotificationFailure(h.app, "gcm", pushError)
}
for _, feedbackReporter := range h.feedbackReporters {
feedback := &FeedbackResponse{
Error: pushError.Key,
ErrorDescription: pushError.Description,
}
b, _ := json.Marshal(feedback)
feedbackReporter.SendFeedback(h.app, "gcm", b)
}
}

func translateToPushError(err error) *pushErrors.PushError {
Expand Down
Loading

0 comments on commit de06d26

Please sign in to comment.