Extract notification into separete package and interface

This commit is contained in:
Eugene 2017-10-09 19:21:56 +03:00 committed by Evgeny Danienko
parent 1c8d32c451
commit 4aaeeb6ebf
No known key found for this signature in database
GPG Key ID: BC8C34D8B45BECBF
8 changed files with 123 additions and 65 deletions

View File

@ -3,18 +3,12 @@ package api
import (
"context"
"github.com/NaySoftware/go-fcm"
"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/log"
"github.com/status-im/status-go/geth/params"
)
const (
serverKey = "AAAAxwa-r08:APA91bFtMIToDVKGAmVCm76iEXtA4dn9MPvLdYKIZqAlNpLJbd12EgdBI9DSDSXKdqvIAgLodepmRhGVaWvhxnXJzVpE6MoIRuKedDV3kfHSVBhWFqsyoLTwXY4xeufL9Sdzb581U-lx"
)
// StatusAPI provides API to access Status related functionality.
type StatusAPI struct {
b *StatusBackend
@ -199,33 +193,10 @@ func (api *StatusAPI) JailBaseJS(js string) {
api.b.jailManager.BaseJS(js)
}
// Notify sends a push notification to the device with the given token.
// TODO(oskarth): API package this stuff
// Notify and send message
func (api *StatusAPI) Notify(token string) string {
log.Debug("Notify", "token", token)
var NP fcm.NotificationPayload
NP.Title = "Status - new message"
NP.Body = "ping"
// TODO(oskarth): Experiment with this
data := map[string]string{
"msg": "Hello World1",
"sum": "Happy Day",
}
ids := []string{
token,
}
c := fcm.NewFcmClient(serverKey)
c.NewFcmRegIdsMsg(ids, data)
c.SetNotificationPayload(&NP)
_, err := c.Send()
if err != nil {
log.Error("Notify failed:", err)
}
api.b.notification.Notify(token)
api.b.notification.Send()
return token
}

View File

@ -10,6 +10,7 @@ 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/params"
"github.com/status-im/status-go/geth/signal"
"github.com/status-im/status-go/geth/txqueue"
@ -23,7 +24,7 @@ type StatusBackend struct {
accountManager common.AccountManager
txQueueManager common.TxQueueManager
jailManager common.JailManager
// TODO(oskarth): notifer here
notification common.Notification
}
// NewStatusBackend create a new NewStatusBackend instance
@ -34,12 +35,14 @@ func NewStatusBackend() *StatusBackend {
accountManager := account.NewManager(nodeManager)
txQueueManager := txqueue.NewManager(nodeManager, accountManager)
jailManager := jail.New(nodeManager)
notificationManager := notification.New(notification.NewFCMClient())
return &StatusBackend{
nodeManager: nodeManager,
accountManager: accountManager,
jailManager: jailManager,
txQueueManager: txQueueManager,
notification: notificationManager,
}
}

View File

@ -0,0 +1,16 @@
package common
import "github.com/NaySoftware/go-fcm"
// Notification manages Push Notifications and send messages
type Notification interface {
Notify(token string) string
Send() error
}
// Messaging manages send/notification messaging clients
type Messaging interface {
NewFcmRegIdsMsg(list []string, body interface{}) *fcm.FcmClient
Send() (*fcm.FcmResponseStatus, error)
SetNotificationPayload(payload *fcm.NotificationPayload) *fcm.FcmClient
}

View File

@ -0,0 +1,21 @@
package notification
import "github.com/NaySoftware/go-fcm"
const (
//todo(jeka): should be removed
fcmServerKey = "AAAAxwa-r08:APA91bFtMIToDVKGAmVCm76iEXtA4dn9MPvLdYKIZqAlNpLJbd12EgdBI9DSDSXKdqvIAgLodepmRhGVaWvhxnXJzVpE6MoIRuKedDV3kfHSVBhWFqsyoLTwXY4xeufL9Sdzb581U-lx"
)
// NewFCMClient Firebase Cloud Messaging client constructor
func NewFCMClient() *fcm.FcmClient {
return fcm.NewFcmClient(fcmServerKey).SetNotificationPayload(getNotificationPayload())
}
// only for feature testing
func getNotificationPayload() *fcm.NotificationPayload {
return &fcm.NotificationPayload{
Title: "Status - new message",
Body: "ping",
}
}

View File

@ -0,0 +1 @@
package notification

View File

@ -0,0 +1,45 @@
package notification
import (
"github.com/status-im/status-go/geth/common"
"github.com/status-im/status-go/geth/log"
)
// Manager of push notifications
type Manager struct {
messaging common.Messaging
}
// New notifications manager
func New(messaging common.Messaging) *Manager {
return &Manager{
messaging,
}
}
// Notify registers notification
func (n *Manager) Notify(token string) string {
log.Debug("Notify", "token", token)
n.messaging.NewFcmRegIdsMsg([]string{token}, n.getMessage)
return token
}
// Send prepared message
func (n *Manager) Send() error {
_, err := n.messaging.Send()
if err != nil {
log.Error("Notify failed:", err)
}
return err
}
func (n *Manager) getMessage() interface{} {
// TODO(oskarth): Experiment with this
return map[string]string{
"msg": "Hello World1",
"sum": "Happy Day",
}
}

View File

@ -0,0 +1 @@
package notification

View File

@ -518,25 +518,25 @@ func AssetNames() []string {
// _bindata is a table, holding each asset generator, mapped to its name.
var _bindata = map[string]func() (*asset, error){
"scripts/README.md": scriptsReadmeMd,
"scripts/web3.js": scriptsWeb3Js,
"config/cht.json": configChtJson,
"config/linter_exclude_list.txt": configLinter_exclude_listTxt,
"config/status-chain-genesis.json": configStatusChainGenesisJson,
"config/test-data.json": configTestDataJson,
"keys/firebaseauthkey": keysFirebaseauthkey,
"keys/test-account1-before-eip55.pk": keysTestAccount1BeforeEip55Pk,
"keys/test-account1.pk": keysTestAccount1Pk,
"keys/test-account2.pk": keysTestAccount2Pk,
"keys/wnodekey": keysWnodekey,
"keys/wnodepassword": keysWnodepassword,
"testdata/jail/commands.js": testdataJailCommandsJs,
"testdata/jail/status.js": testdataJailStatusJs,
"testdata/jail/tx-send/context-no-message-id.js": testdataJailTxSendContextNoMessageIdJs,
"testdata/jail/tx-send/message-id-no-context.js": testdataJailTxSendMessageIdNoContextJs,
"scripts/README.md": scriptsReadmeMd,
"scripts/web3.js": scriptsWeb3Js,
"config/cht.json": configChtJson,
"config/linter_exclude_list.txt": configLinter_exclude_listTxt,
"config/status-chain-genesis.json": configStatusChainGenesisJson,
"config/test-data.json": configTestDataJson,
"keys/firebaseauthkey": keysFirebaseauthkey,
"keys/test-account1-before-eip55.pk": keysTestAccount1BeforeEip55Pk,
"keys/test-account1.pk": keysTestAccount1Pk,
"keys/test-account2.pk": keysTestAccount2Pk,
"keys/wnodekey": keysWnodekey,
"keys/wnodepassword": keysWnodepassword,
"testdata/jail/commands.js": testdataJailCommandsJs,
"testdata/jail/status.js": testdataJailStatusJs,
"testdata/jail/tx-send/context-no-message-id.js": testdataJailTxSendContextNoMessageIdJs,
"testdata/jail/tx-send/message-id-no-context.js": testdataJailTxSendMessageIdNoContextJs,
"testdata/jail/tx-send/no-message-id-or-context.js": testdataJailTxSendNoMessageIdOrContextJs,
"testdata/jail/tx-send/tx-send.js": testdataJailTxSendTxSendJs,
"testdata/node/test.sol": testdataNodeTestSol,
"testdata/jail/tx-send/tx-send.js": testdataJailTxSendTxSendJs,
"testdata/node/test.sol": testdataNodeTestSol,
}
// AssetDir returns the file names below a certain
@ -578,34 +578,35 @@ type bintree struct {
Func func() (*asset, error)
Children map[string]*bintree
}
var _bintree = &bintree{nil, map[string]*bintree{
"config": &bintree{nil, map[string]*bintree{
"cht.json": &bintree{configChtJson, map[string]*bintree{}},
"linter_exclude_list.txt": &bintree{configLinter_exclude_listTxt, map[string]*bintree{}},
"cht.json": &bintree{configChtJson, map[string]*bintree{}},
"linter_exclude_list.txt": &bintree{configLinter_exclude_listTxt, map[string]*bintree{}},
"status-chain-genesis.json": &bintree{configStatusChainGenesisJson, map[string]*bintree{}},
"test-data.json": &bintree{configTestDataJson, map[string]*bintree{}},
"test-data.json": &bintree{configTestDataJson, map[string]*bintree{}},
}},
"keys": &bintree{nil, map[string]*bintree{
"firebaseauthkey": &bintree{keysFirebaseauthkey, map[string]*bintree{}},
"firebaseauthkey": &bintree{keysFirebaseauthkey, map[string]*bintree{}},
"test-account1-before-eip55.pk": &bintree{keysTestAccount1BeforeEip55Pk, map[string]*bintree{}},
"test-account1.pk": &bintree{keysTestAccount1Pk, map[string]*bintree{}},
"test-account2.pk": &bintree{keysTestAccount2Pk, map[string]*bintree{}},
"wnodekey": &bintree{keysWnodekey, map[string]*bintree{}},
"wnodepassword": &bintree{keysWnodepassword, map[string]*bintree{}},
"test-account1.pk": &bintree{keysTestAccount1Pk, map[string]*bintree{}},
"test-account2.pk": &bintree{keysTestAccount2Pk, map[string]*bintree{}},
"wnodekey": &bintree{keysWnodekey, map[string]*bintree{}},
"wnodepassword": &bintree{keysWnodepassword, map[string]*bintree{}},
}},
"scripts": &bintree{nil, map[string]*bintree{
"README.md": &bintree{scriptsReadmeMd, map[string]*bintree{}},
"web3.js": &bintree{scriptsWeb3Js, map[string]*bintree{}},
"web3.js": &bintree{scriptsWeb3Js, map[string]*bintree{}},
}},
"testdata": &bintree{nil, map[string]*bintree{
"jail": &bintree{nil, map[string]*bintree{
"commands.js": &bintree{testdataJailCommandsJs, map[string]*bintree{}},
"status.js": &bintree{testdataJailStatusJs, map[string]*bintree{}},
"status.js": &bintree{testdataJailStatusJs, map[string]*bintree{}},
"tx-send": &bintree{nil, map[string]*bintree{
"context-no-message-id.js": &bintree{testdataJailTxSendContextNoMessageIdJs, map[string]*bintree{}},
"message-id-no-context.js": &bintree{testdataJailTxSendMessageIdNoContextJs, map[string]*bintree{}},
"context-no-message-id.js": &bintree{testdataJailTxSendContextNoMessageIdJs, map[string]*bintree{}},
"message-id-no-context.js": &bintree{testdataJailTxSendMessageIdNoContextJs, map[string]*bintree{}},
"no-message-id-or-context.js": &bintree{testdataJailTxSendNoMessageIdOrContextJs, map[string]*bintree{}},
"tx-send.js": &bintree{testdataJailTxSendTxSendJs, map[string]*bintree{}},
"tx-send.js": &bintree{testdataJailTxSendTxSendJs, map[string]*bintree{}},
}},
}},
"node": &bintree{nil, map[string]*bintree{
@ -660,4 +661,3 @@ func _filePath(dir, name string) string {
cannonicalName := strings.Replace(name, "\\", "/", -1)
return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...)
}