Remove provider interface

This commit is contained in:
Eugene 2017-10-11 16:51:43 +03:00 committed by Evgeny Danienko
parent c36a51d0cf
commit 804ed7c10c
No known key found for this signature in database
GPG Key ID: BC8C34D8B45BECBF
11 changed files with 102 additions and 234 deletions

View File

@ -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
}

View File

@ -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,

View File

@ -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
}

View File

@ -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
}

View File

@ -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(&notification.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,
}
}

View File

@ -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"}
}

View File

@ -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,
}
}

View File

@ -1,7 +0,0 @@
package message
// Message with data and payload
type Message struct {
Body interface{}
Payload *Payload
}

View File

@ -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
}

View File

@ -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",
},
}
}

View File

@ -1,4 +1,4 @@
package message
package notification
// Payload data of message.
type Payload struct {