Remove provider interface
This commit is contained in:
parent
c36a51d0cf
commit
804ed7c10c
|
@ -6,7 +6,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/accounts/keystore"
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
||||||
gethcommon "github.com/ethereum/go-ethereum/common"
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/status-im/status-go/geth/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"
|
"github.com/status-im/status-go/geth/params"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -196,19 +196,18 @@ func (api *StatusAPI) JailBaseJS(js string) {
|
||||||
|
|
||||||
// Notify and send message.
|
// Notify and send message.
|
||||||
func (api *StatusAPI) Notify(token string) string {
|
func (api *StatusAPI) Notify(token string) string {
|
||||||
|
log.Debug("Notify", "token", token)
|
||||||
|
|
||||||
// TODO(oskarth): Experiment with this
|
// TODO(oskarth): Experiment with this
|
||||||
msg := &message.Message{
|
msg := map[string]string{
|
||||||
Body: map[string]string{
|
"msg": "Hello World1",
|
||||||
"msg": "Hello World1",
|
"sum": "Happy Day",
|
||||||
"sum": "Happy Day",
|
|
||||||
},
|
|
||||||
Payload: &message.Payload{
|
|
||||||
Title: "Status - new message",
|
|
||||||
Body: "ping",
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
api.b.notification.Notify(token, msg)
|
err := api.b.notification.Notify(msg, token)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Notify failed:", err)
|
||||||
|
}
|
||||||
|
|
||||||
return token
|
return token
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,11 +10,10 @@ import (
|
||||||
"github.com/status-im/status-go/geth/jail"
|
"github.com/status-im/status-go/geth/jail"
|
||||||
"github.com/status-im/status-go/geth/log"
|
"github.com/status-im/status-go/geth/log"
|
||||||
"github.com/status-im/status-go/geth/node"
|
"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/params"
|
||||||
"github.com/status-im/status-go/geth/signal"
|
"github.com/status-im/status-go/geth/signal"
|
||||||
"github.com/status-im/status-go/geth/txqueue"
|
"github.com/status-im/status-go/geth/txqueue"
|
||||||
"github.com/NaySoftware/go-fcm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -30,7 +29,7 @@ type StatusBackend struct {
|
||||||
accountManager common.AccountManager
|
accountManager common.AccountManager
|
||||||
txQueueManager common.TxQueueManager
|
txQueueManager common.TxQueueManager
|
||||||
jailManager common.JailManager
|
jailManager common.JailManager
|
||||||
notification common.Notification
|
notification common.Notifier
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStatusBackend create a new NewStatusBackend instance
|
// NewStatusBackend create a new NewStatusBackend instance
|
||||||
|
@ -41,7 +40,7 @@ func NewStatusBackend() *StatusBackend {
|
||||||
accountManager := account.NewManager(nodeManager)
|
accountManager := account.NewManager(nodeManager)
|
||||||
txQueueManager := txqueue.NewManager(nodeManager, accountManager)
|
txQueueManager := txqueue.NewManager(nodeManager, accountManager)
|
||||||
jailManager := jail.New(nodeManager)
|
jailManager := jail.New(nodeManager)
|
||||||
notificationManager := notification.New(notification.NewFCMProvider(fcm.NewFcmClient(fcmServerKey)))
|
notificationManager := fcm.NewNotifier(fcmServerKey)
|
||||||
|
|
||||||
return &StatusBackend{
|
return &StatusBackend{
|
||||||
nodeManager: nodeManager,
|
nodeManager: nodeManager,
|
||||||
|
|
|
@ -1,25 +1,6 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
// Notifier manages Push Notifications.
|
||||||
"github.com/NaySoftware/go-fcm"
|
type Notifier interface {
|
||||||
"github.com/status-im/status-go/geth/notification/message"
|
Notify(body interface{}, tokens ...string) error
|
||||||
)
|
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -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,
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package notification
|
package fcm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
@ -6,8 +6,6 @@ import (
|
||||||
|
|
||||||
"github.com/NaySoftware/go-fcm"
|
"github.com/NaySoftware/go-fcm"
|
||||||
"github.com/golang/mock/gomock"
|
"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"
|
t "github.com/status-im/status-go/geth/testing"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
)
|
)
|
||||||
|
@ -19,13 +17,13 @@ func TestFCMClientTestSuite(t *testing.T) {
|
||||||
type FCMProviderTestSuite struct {
|
type FCMProviderTestSuite struct {
|
||||||
t.BaseTestSuite
|
t.BaseTestSuite
|
||||||
|
|
||||||
fcmClientMock *common.MockFirebaseClient
|
fcmClientMock *MockFirebaseClient
|
||||||
fcmClientMockCtrl *gomock.Controller
|
fcmClientMockCtrl *gomock.Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *FCMProviderTestSuite) SetupTest() {
|
func (s *FCMProviderTestSuite) SetupTest() {
|
||||||
s.fcmClientMockCtrl = gomock.NewController(s.T())
|
s.fcmClientMockCtrl = gomock.NewController(s.T())
|
||||||
s.fcmClientMock = common.NewMockFirebaseClient(s.fcmClientMockCtrl)
|
s.fcmClientMock = NewMockFirebaseClient(s.fcmClientMockCtrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *FCMProviderTestSuite) TearDownTest() {
|
func (s *FCMProviderTestSuite) TearDownTest() {
|
||||||
|
@ -33,49 +31,43 @@ func (s *FCMProviderTestSuite) TearDownTest() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *FCMProviderTestSuite) TestNewFCMClient() {
|
func (s *FCMProviderTestSuite) TestNewFCMClient() {
|
||||||
fcmClient := NewFCMProvider(s.fcmClientMock)
|
fcmClient := Notifier{s.fcmClientMock}
|
||||||
|
|
||||||
s.Require().NotNil(fcmClient)
|
s.Require().NotNil(fcmClient)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *FCMProviderTestSuite) TestSetMessage() {
|
func (s *FCMProviderTestSuite) TestNotifySuccess() {
|
||||||
|
fcmPayload := getPayload()
|
||||||
ids := []string{"1"}
|
ids := []string{"1"}
|
||||||
body := interface{}("body")
|
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)
|
s.fcmClientMock.EXPECT().SetNotificationPayload(fcmPayload).Times(1)
|
||||||
fcmClient := NewFCMProvider(s.fcmClientMock)
|
s.fcmClientMock.EXPECT().NewFcmRegIdsMsg(ids, body).Times(1)
|
||||||
|
|
||||||
fcmClient.SetPayload(payload)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *FCMProviderTestSuite) TestSendSuccess() {
|
|
||||||
s.fcmClientMock.EXPECT().Send().Return(nil, nil).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)
|
s.Require().NoError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *FCMProviderTestSuite) TestSendError() {
|
func (s *FCMProviderTestSuite) TestNotifyError() {
|
||||||
expectedError := errors.New("error")
|
expectedError := errors.New("error")
|
||||||
|
|
||||||
s.fcmClientMock.EXPECT().Send().Return(nil, expectedError).Times(1)
|
fcmPayload := getPayload()
|
||||||
fcmClient := NewFCMProvider(s.fcmClientMock)
|
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)
|
s.Require().Equal(expectedError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getPayload() *fcm.NotificationPayload {
|
||||||
|
return &fcm.NotificationPayload{Title: "Status - new message", Body: "ping"}
|
||||||
|
}
|
|
@ -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,
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,7 +0,0 @@
|
||||||
package message
|
|
||||||
|
|
||||||
// Message with data and payload
|
|
||||||
type Message struct {
|
|
||||||
Body interface{}
|
|
||||||
Payload *Payload
|
|
||||||
}
|
|
|
@ -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
|
|
||||||
}
|
|
|
@ -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",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,4 +1,4 @@
|
||||||
package message
|
package notification
|
||||||
|
|
||||||
// Payload data of message.
|
// Payload data of message.
|
||||||
type Payload struct {
|
type Payload struct {
|
Loading…
Reference in New Issue