From 7bad800cb87438ac2b0127aedadf63d0e2846587 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Wed, 15 Jul 2020 14:43:15 +0200 Subject: [PATCH] Add endpoints to enable/disable pns --- params/config.go | 3 -- protocol/messenger.go | 20 +++++++++++++ protocol/push_notification_client/client.go | 24 +++++++++++----- services/ext/api.go | 31 +++++++++++++++++++++ services/ext/service.go | 2 +- 5 files changed, 69 insertions(+), 11 deletions(-) diff --git a/params/config.go b/params/config.go index 3ad56d79d..54b6ba923 100644 --- a/params/config.go +++ b/params/config.go @@ -523,9 +523,6 @@ type ShhextConfig struct { VerifyENSContractAddress string VerifyTransactionChainID int64 - - // PushNotificationServerEnabled indicates whether a push notification server should be started - PushNotificationServerEnabled bool } // Validate validates the ShhextConfig struct and returns an error if inconsistent values are found diff --git a/protocol/messenger.go b/protocol/messenger.go index 8e7d76e90..39f6b2a73 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -3043,6 +3043,26 @@ func (m *Messenger) AddPushNotificationServer(ctx context.Context, publicKey *ec return m.pushNotificationClient.AddPushNotificationServer(publicKey) } +func (m *Messenger) UnregisterFromPushNotifications(ctx context.Context) error { + return nil +} + +func (m *Messenger) DisableSendingPushNotifications() error { + if m.pushNotificationClient == nil { + return errors.New("push notification client not enabled") + } + m.pushNotificationClient.DisableSending() + return nil +} + +func (m *Messenger) EnableSendingPushNotifications() error { + if m.pushNotificationClient == nil { + return errors.New("push notification client not enabled") + } + m.pushNotificationClient.EnableSending() + return nil +} + // RegisterForPushNotification register deviceToken with any push notification server enabled func (m *Messenger) RegisterForPushNotifications(ctx context.Context, deviceToken string) ([]*push_notification_client.PushNotificationServer, error) { if m.pushNotificationClient == nil { diff --git a/protocol/push_notification_client/client.go b/protocol/push_notification_client/client.go index abef9e148..5f13c30b6 100644 --- a/protocol/push_notification_client/client.go +++ b/protocol/push_notification_client/client.go @@ -195,12 +195,6 @@ func (c *Client) Stop() error { return nil } -// The message has been sent -// We should: -// 1) Check whether we should notify on anything -// 2) Refresh info if necessaary -// 3) Sent push notifications -// TODO: handle DH messages func (c *Client) HandleMessageSent(sentMessage *common.SentMessage) error { c.config.Logger.Info("sent message", zap.Any("sent message", sentMessage)) if !c.config.SendEnabled { @@ -775,9 +769,17 @@ func (c *Client) AddPushNotificationServer(publicKey *ecdsa.PublicKey) error { } } - return c.persistence.UpsertServer(&PushNotificationServer{ + err = c.persistence.UpsertServer(&PushNotificationServer{ PublicKey: publicKey, }) + if err != nil { + return err + } + + if c.config.RemoteNotificationsEnabled { + c.startRegistrationLoop() + } + return nil } func (c *Client) QueryPushNotificationInfo(publicKey *ecdsa.PublicKey) error { @@ -814,6 +816,14 @@ func (c *Client) GetPushNotificationInfo(publicKey *ecdsa.PublicKey, installatio } } +func (c *Client) EnableSending() { + c.config.SendEnabled = true +} + +func (c *Client) DisableSending() { + c.config.SendEnabled = false +} + func (c *Client) listenToPublicKeyQueryTopic(hashedPublicKey []byte) error { encodedPublicKey := hex.EncodeToString(hashedPublicKey) return c.messageProcessor.JoinPublic(encodedPublicKey) diff --git a/services/ext/api.go b/services/ext/api.go index 11968a44d..468f01dab 100644 --- a/services/ext/api.go +++ b/services/ext/api.go @@ -406,19 +406,50 @@ func (api *PublicAPI) UpdateMailservers(enodes []string) error { // PushNotifications server func (api *PublicAPI) StartPushNotificationServer() error { + // update settings return api.service.messenger.StartPushNotificationServer() } func (api *PublicAPI) StopPushNotificationServer() error { + // update settings return api.service.messenger.StopPushNotificationServer() } // PushNotification client func (api *PublicAPI) RegisterForPushNotifications(ctx context.Context, deviceToken string) ([]*push_notification_client.PushNotificationServer, error) { + err := api.service.accountsDB.SaveSetting("remote-push-notifications-enabled", true) + if err != nil { + return nil, err + } return api.service.messenger.RegisterForPushNotifications(ctx, deviceToken) } +func (api *PublicAPI) UnregisterForPushNotifications(ctx context.Context) error { + err := api.service.accountsDB.SaveSetting("remote-push-notifications-enabled", false) + if err != nil { + return err + } + return api.service.messenger.UnregisterFromPushNotifications(ctx) +} + +func (api *PublicAPI) DisableSendingNotifications(ctx context.Context) error { + err := api.service.accountsDB.SaveSetting("send-push-notifications", false) + if err != nil { + return err + } + + return api.service.messenger.DisableSendingPushNotifications() +} + +func (api *PublicAPI) EnableSendingNotifications(ctx context.Context) error { + err := api.service.accountsDB.SaveSetting("send-push-notifications", true) + if err != nil { + return err + } + return api.service.messenger.EnableSendingPushNotifications() +} + func (api *PublicAPI) AddPushNotificationServer(ctx context.Context, publicKeyBytes types.HexBytes) error { publicKey, err := crypto.UnmarshalPubkey(publicKeyBytes) if err != nil { diff --git a/services/ext/service.go b/services/ext/service.go index 54b0fd80d..d08f0e8ee 100644 --- a/services/ext/service.go +++ b/services/ext/service.go @@ -469,7 +469,7 @@ func buildMessengerOptions( return nil, err } - if config.PushNotificationServerEnabled || settings.PushNotificationServerEnabled { + if settings.PushNotificationServerEnabled { config := &push_notification_server.Config{ Logger: logger, }