From 804ed7c10c613baa637c6d6c21c60fa3c7f9283a Mon Sep 17 00:00:00 2001 From: Eugene <6655321@bk.ru> Date: Wed, 11 Oct 2017 16:51:43 +0300 Subject: [PATCH] Remove provider interface --- geth/api/api.go | 21 +++-- geth/api/backend.go | 7 +- geth/common/notification.go | 25 +----- geth/notification/fcm/client.go | 10 +++ geth/notification/fcm/notifier.go | 52 +++++++++++++ .../notifier_test.go} | 54 ++++++------- geth/notification/fcm_provider.go | 46 ----------- geth/notification/message/message.go | 7 -- geth/notification/notification.go | 34 -------- geth/notification/notification_test.go | 78 ------------------- geth/notification/{message => }/payload.go | 2 +- 11 files changed, 102 insertions(+), 234 deletions(-) create mode 100644 geth/notification/fcm/client.go create mode 100644 geth/notification/fcm/notifier.go rename geth/notification/{fcm_provider_test.go => fcm/notifier_test.go} (52%) delete mode 100644 geth/notification/fcm_provider.go delete mode 100644 geth/notification/message/message.go delete mode 100644 geth/notification/notification.go delete mode 100644 geth/notification/notification_test.go rename geth/notification/{message => }/payload.go (87%) diff --git a/geth/api/api.go b/geth/api/api.go index 13d4969d7..5ecfdad05 100644 --- a/geth/api/api.go +++ b/geth/api/api.go @@ -6,7 +6,7 @@ import ( "github.com/ethereum/go-ethereum/accounts/keystore" gethcommon "github.com/ethereum/go-ethereum/common" "github.com/status-im/status-go/geth/common" - "github.com/status-im/status-go/geth/notification/message" + "github.com/status-im/status-go/geth/log" "github.com/status-im/status-go/geth/params" ) @@ -196,19 +196,18 @@ func (api *StatusAPI) JailBaseJS(js string) { // Notify and send message. func (api *StatusAPI) Notify(token string) string { + log.Debug("Notify", "token", token) + // TODO(oskarth): Experiment with this - msg := &message.Message{ - Body: map[string]string{ - "msg": "Hello World1", - "sum": "Happy Day", - }, - Payload: &message.Payload{ - Title: "Status - new message", - Body: "ping", - }, + msg := map[string]string{ + "msg": "Hello World1", + "sum": "Happy Day", } - api.b.notification.Notify(token, msg) + err := api.b.notification.Notify(msg, token) + if err != nil { + log.Error("Notify failed:", err) + } return token } diff --git a/geth/api/backend.go b/geth/api/backend.go index 3dca45068..b47365f30 100644 --- a/geth/api/backend.go +++ b/geth/api/backend.go @@ -10,11 +10,10 @@ import ( "github.com/status-im/status-go/geth/jail" "github.com/status-im/status-go/geth/log" "github.com/status-im/status-go/geth/node" - "github.com/status-im/status-go/geth/notification" + "github.com/status-im/status-go/geth/notification/fcm" "github.com/status-im/status-go/geth/params" "github.com/status-im/status-go/geth/signal" "github.com/status-im/status-go/geth/txqueue" - "github.com/NaySoftware/go-fcm" ) const ( @@ -30,7 +29,7 @@ type StatusBackend struct { accountManager common.AccountManager txQueueManager common.TxQueueManager jailManager common.JailManager - notification common.Notification + notification common.Notifier } // NewStatusBackend create a new NewStatusBackend instance @@ -41,7 +40,7 @@ func NewStatusBackend() *StatusBackend { accountManager := account.NewManager(nodeManager) txQueueManager := txqueue.NewManager(nodeManager, accountManager) jailManager := jail.New(nodeManager) - notificationManager := notification.New(notification.NewFCMProvider(fcm.NewFcmClient(fcmServerKey))) + notificationManager := fcm.NewNotifier(fcmServerKey) return &StatusBackend{ nodeManager: nodeManager, diff --git a/geth/common/notification.go b/geth/common/notification.go index fec73f77d..ead1c5462 100644 --- a/geth/common/notification.go +++ b/geth/common/notification.go @@ -1,25 +1,6 @@ package common -import ( - "github.com/NaySoftware/go-fcm" - "github.com/status-im/status-go/geth/notification/message" -) - -// Notification manages Push Notifications and send messages. -type Notification interface { - Notify(token string, msg *message.Message) (string, error) -} - -// MessagingProvider manages send/notification messaging clients. -type MessagingProvider interface { - SetMessage(ids []string, body interface{}) - SetPayload(payload *message.Payload) - Send() error -} - -// FirebaseClient is a copy of "go-fcm" client methods. -type FirebaseClient interface { - NewFcmRegIdsMsg(list []string, body interface{}) *fcm.FcmClient - Send() (*fcm.FcmResponseStatus, error) - SetNotificationPayload(payload *fcm.NotificationPayload) *fcm.FcmClient +// Notifier manages Push Notifications. +type Notifier interface { + Notify(body interface{}, tokens ...string) error } diff --git a/geth/notification/fcm/client.go b/geth/notification/fcm/client.go new file mode 100644 index 000000000..73aa21c9a --- /dev/null +++ b/geth/notification/fcm/client.go @@ -0,0 +1,10 @@ +package fcm + +import "github.com/NaySoftware/go-fcm" + +// FirebaseClient is a copy of "go-fcm" client methods. +type FirebaseClient interface { + NewFcmRegIdsMsg(tokens []string, body interface{}) *fcm.FcmClient + Send() (*fcm.FcmResponseStatus, error) + SetNotificationPayload(payload *fcm.NotificationPayload) *fcm.FcmClient +} \ No newline at end of file diff --git a/geth/notification/fcm/notifier.go b/geth/notification/fcm/notifier.go new file mode 100644 index 000000000..7f91d65e3 --- /dev/null +++ b/geth/notification/fcm/notifier.go @@ -0,0 +1,52 @@ +package fcm + +import ( + "github.com/NaySoftware/go-fcm" + "github.com/status-im/status-go/geth/notification" +) + +// Notifier represents messaging provider for notifications. +type Notifier struct { + FirebaseClient +} + +// NewNotifier Firebase Cloud Messaging client constructor. +func NewNotifier(key string) *Notifier { + return &Notifier{fcm.NewFcmClient(key)} +} + +// Notify preparation and send to the tokens list. +func (p *Notifier) Notify(body interface{}, tokens ...string) error { + p.setPayload(¬ification.Payload{ + Title: "Status - new message", + Body: "ping", + }) + + p.setMessage(body, tokens...) + _, err := p.FirebaseClient.Send() + + return err +} + +// SetMessage to send for given the tokens list. +func (p *Notifier) setMessage(body interface{}, tokens ...string) { + p.NewFcmRegIdsMsg(tokens, body) +} + +// SetPayload sets payload message information. +func (p *Notifier) setPayload(payload *notification.Payload) { + fcmPayload := p.toFCMPayload(payload) + p.SetNotificationPayload(fcmPayload) +} + +func (p *Notifier) toFCMPayload(payload *notification.Payload) *fcm.NotificationPayload { + return &fcm.NotificationPayload{ + Title: payload.Title, + Body: payload.Body, + Icon: payload.Icon, + Sound: payload.Sound, + Badge: payload.Badge, + Tag: payload.Tag, + Color: payload.Color, + } +} diff --git a/geth/notification/fcm_provider_test.go b/geth/notification/fcm/notifier_test.go similarity index 52% rename from geth/notification/fcm_provider_test.go rename to geth/notification/fcm/notifier_test.go index d59a5beff..61950a57c 100644 --- a/geth/notification/fcm_provider_test.go +++ b/geth/notification/fcm/notifier_test.go @@ -1,4 +1,4 @@ -package notification +package fcm import ( "errors" @@ -6,8 +6,6 @@ import ( "github.com/NaySoftware/go-fcm" "github.com/golang/mock/gomock" - "github.com/status-im/status-go/geth/common" - "github.com/status-im/status-go/geth/notification/message" t "github.com/status-im/status-go/geth/testing" "github.com/stretchr/testify/suite" ) @@ -19,13 +17,13 @@ func TestFCMClientTestSuite(t *testing.T) { type FCMProviderTestSuite struct { t.BaseTestSuite - fcmClientMock *common.MockFirebaseClient + fcmClientMock *MockFirebaseClient fcmClientMockCtrl *gomock.Controller } func (s *FCMProviderTestSuite) SetupTest() { s.fcmClientMockCtrl = gomock.NewController(s.T()) - s.fcmClientMock = common.NewMockFirebaseClient(s.fcmClientMockCtrl) + s.fcmClientMock = NewMockFirebaseClient(s.fcmClientMockCtrl) } func (s *FCMProviderTestSuite) TearDownTest() { @@ -33,49 +31,43 @@ func (s *FCMProviderTestSuite) TearDownTest() { } func (s *FCMProviderTestSuite) TestNewFCMClient() { - fcmClient := NewFCMProvider(s.fcmClientMock) + fcmClient := Notifier{s.fcmClientMock} s.Require().NotNil(fcmClient) } -func (s *FCMProviderTestSuite) TestSetMessage() { +func (s *FCMProviderTestSuite) TestNotifySuccess() { + fcmPayload := getPayload() ids := []string{"1"} body := interface{}("body") - s.fcmClientMock.EXPECT().NewFcmRegIdsMsg(ids, body).Times(1) - fcmClient := NewFCMProvider(s.fcmClientMock) - - fcmClient.SetMessage(ids, body) -} - -func (s *FCMProviderTestSuite) TestSetPayload() { - title := "title" - body := "body" - payload := &message.Payload{Title: title, Body: body} - fcmPayload := &fcm.NotificationPayload{Title: title, Body: body} - s.fcmClientMock.EXPECT().SetNotificationPayload(fcmPayload).Times(1) - fcmClient := NewFCMProvider(s.fcmClientMock) - - fcmClient.SetPayload(payload) -} - -func (s *FCMProviderTestSuite) TestSendSuccess() { + s.fcmClientMock.EXPECT().NewFcmRegIdsMsg(ids, body).Times(1) s.fcmClientMock.EXPECT().Send().Return(nil, nil).Times(1) - fcmClient := NewFCMProvider(s.fcmClientMock) + fcmClient := Notifier{s.fcmClientMock} - err := fcmClient.Send() + err := fcmClient.Notify(body, ids...) s.Require().NoError(err) } -func (s *FCMProviderTestSuite) TestSendError() { +func (s *FCMProviderTestSuite) TestNotifyError() { expectedError := errors.New("error") - s.fcmClientMock.EXPECT().Send().Return(nil, expectedError).Times(1) - fcmClient := NewFCMProvider(s.fcmClientMock) + fcmPayload := getPayload() + ids := []string{"1"} + body := interface{}("body") - err := fcmClient.Send() + s.fcmClientMock.EXPECT().SetNotificationPayload(fcmPayload).Times(1) + s.fcmClientMock.EXPECT().NewFcmRegIdsMsg(ids, body).Times(1) + s.fcmClientMock.EXPECT().Send().Return(nil, expectedError).Times(1) + fcmClient := Notifier{s.fcmClientMock} + + err := fcmClient.Notify(body, ids...) s.Require().Equal(expectedError, err) } + +func getPayload() *fcm.NotificationPayload { + return &fcm.NotificationPayload{Title: "Status - new message", Body: "ping"} +} diff --git a/geth/notification/fcm_provider.go b/geth/notification/fcm_provider.go deleted file mode 100644 index bd9f63b51..000000000 --- a/geth/notification/fcm_provider.go +++ /dev/null @@ -1,46 +0,0 @@ -package notification - -import ( - "github.com/NaySoftware/go-fcm" - "github.com/status-im/status-go/geth/notification/message" - "github.com/status-im/status-go/geth/common" -) - -// FCMProvider represents messaging provider for notifications. -type FCMProvider struct { - common.FirebaseClient -} - -// NewFCMProvider Firebase Cloud Messaging client constructor. -func NewFCMProvider(fcmClient common.FirebaseClient) *FCMProvider { - return &FCMProvider{fcmClient} -} - -// SetMessage to send for given ids. -func (p *FCMProvider) SetMessage(ids []string, body interface{}) { - p.NewFcmRegIdsMsg(ids, body) -} - -// SetPayload sets payload message information. -func (p *FCMProvider) SetPayload(payload *message.Payload) { - fcmPayload := p.toFCMPayload(payload) - p.SetNotificationPayload(fcmPayload) -} - -// Send message. -func (p *FCMProvider) Send() error { - _, err := p.FirebaseClient.Send() - return err -} - -func (p *FCMProvider) toFCMPayload(payload *message.Payload) *fcm.NotificationPayload { - return &fcm.NotificationPayload{ - Title: payload.Title, - Body: payload.Body, - Icon: payload.Icon, - Sound: payload.Sound, - Badge: payload.Badge, - Tag: payload.Tag, - Color: payload.Color, - } -} diff --git a/geth/notification/message/message.go b/geth/notification/message/message.go deleted file mode 100644 index 9e5c841e9..000000000 --- a/geth/notification/message/message.go +++ /dev/null @@ -1,7 +0,0 @@ -package message - -// Message with data and payload -type Message struct { - Body interface{} - Payload *Payload -} diff --git a/geth/notification/notification.go b/geth/notification/notification.go deleted file mode 100644 index 6b77220be..000000000 --- a/geth/notification/notification.go +++ /dev/null @@ -1,34 +0,0 @@ -package notification - -import ( - "github.com/status-im/status-go/geth/common" - "github.com/status-im/status-go/geth/log" - "github.com/status-im/status-go/geth/notification/message" -) - -// Manager of push notifications. -type Manager struct { - messaging common.MessagingProvider -} - -// New notifications manager. -func New(messaging common.MessagingProvider) *Manager { - return &Manager{ - messaging, - } -} - -// Notify makes send message and notification. -func (n *Manager) Notify(token string, msg *message.Message) (string, error) { - log.Debug("Notify", "token", token) - - n.messaging.SetMessage([]string{token}, msg.Body) - n.messaging.SetPayload(msg.Payload) - - err := n.messaging.Send() - if err != nil { - log.Error("Notify failed:", err) - } - - return token, err -} diff --git a/geth/notification/notification_test.go b/geth/notification/notification_test.go deleted file mode 100644 index 099d8d1cb..000000000 --- a/geth/notification/notification_test.go +++ /dev/null @@ -1,78 +0,0 @@ -package notification - -import ( - "testing" - - "errors" - "github.com/golang/mock/gomock" - "github.com/status-im/status-go/geth/common" - "github.com/status-im/status-go/geth/notification/message" - t "github.com/status-im/status-go/geth/testing" - "github.com/stretchr/testify/suite" -) - -func TestNotificationTestSuite(t *testing.T) { - suite.Run(t, new(NotificationTestSuite)) -} - -type NotificationTestSuite struct { - t.BaseTestSuite - messagingMock *common.MockMessagingProvider - messagingMockCtrl *gomock.Controller -} - -func (s *NotificationTestSuite) SetupTest() { - s.messagingMockCtrl = gomock.NewController(s.T()) - s.messagingMock = common.NewMockMessagingProvider(s.messagingMockCtrl) -} - -func (s *NotificationTestSuite) TearDownTest() { - s.messagingMockCtrl.Finish() -} - -func (s *NotificationTestSuite) TestNewNotification() { - manager := New(nil) - s.Require().NotNil(manager) -} - -func (s *NotificationTestSuite) TestNotifySuccess() { - token := "test" - msg := getMessage() - - s.messagingMock.EXPECT().SetMessage([]string{token}, msg.Body).Times(1) - s.messagingMock.EXPECT().SetPayload(msg.Payload).Times(1) - s.messagingMock.EXPECT().Send().Return(nil).Times(1) - - manager := New(s.messagingMock) - res, err := manager.Notify(token, msg) - - s.Require().Equal(token, res) - s.Require().NoError(err) -} - -func (s *NotificationTestSuite) TestNotifyError() { - token := "test" - msg := getMessage() - expectedError := errors.New("error") - - s.messagingMock.EXPECT().SetMessage([]string{token}, msg.Body).Times(1) - s.messagingMock.EXPECT().SetPayload(msg.Payload).Times(1) - s.messagingMock.EXPECT().Send().Return(expectedError).Times(1) - - manager := New(s.messagingMock) - _, err := manager.Notify(token, msg) - - s.Require().Equal(expectedError, err) -} - -func getMessage() *message.Message { - return &message.Message{ - Body: map[string]string{ - "msg": "Hello World1", - "sum": "Happy Day", - }, - Payload: &message.Payload{ - Title: "test notification", - }, - } -} diff --git a/geth/notification/message/payload.go b/geth/notification/payload.go similarity index 87% rename from geth/notification/message/payload.go rename to geth/notification/payload.go index c856e5c5e..0dc787a4a 100644 --- a/geth/notification/message/payload.go +++ b/geth/notification/payload.go @@ -1,4 +1,4 @@ -package message +package notification // Payload data of message. type Payload struct {