Add endpoints to enable/disable pns

This commit is contained in:
Andrea Maria Piana 2020-07-15 14:43:15 +02:00
parent be1800f2ec
commit 7bad800cb8
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
5 changed files with 69 additions and 11 deletions

View File

@ -523,9 +523,6 @@ type ShhextConfig struct {
VerifyENSContractAddress string VerifyENSContractAddress string
VerifyTransactionChainID int64 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 // Validate validates the ShhextConfig struct and returns an error if inconsistent values are found

View File

@ -3043,6 +3043,26 @@ func (m *Messenger) AddPushNotificationServer(ctx context.Context, publicKey *ec
return m.pushNotificationClient.AddPushNotificationServer(publicKey) 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 // RegisterForPushNotification register deviceToken with any push notification server enabled
func (m *Messenger) RegisterForPushNotifications(ctx context.Context, deviceToken string) ([]*push_notification_client.PushNotificationServer, error) { func (m *Messenger) RegisterForPushNotifications(ctx context.Context, deviceToken string) ([]*push_notification_client.PushNotificationServer, error) {
if m.pushNotificationClient == nil { if m.pushNotificationClient == nil {

View File

@ -195,12 +195,6 @@ func (c *Client) Stop() error {
return nil 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 { func (c *Client) HandleMessageSent(sentMessage *common.SentMessage) error {
c.config.Logger.Info("sent message", zap.Any("sent message", sentMessage)) c.config.Logger.Info("sent message", zap.Any("sent message", sentMessage))
if !c.config.SendEnabled { 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, PublicKey: publicKey,
}) })
if err != nil {
return err
}
if c.config.RemoteNotificationsEnabled {
c.startRegistrationLoop()
}
return nil
} }
func (c *Client) QueryPushNotificationInfo(publicKey *ecdsa.PublicKey) error { 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 { func (c *Client) listenToPublicKeyQueryTopic(hashedPublicKey []byte) error {
encodedPublicKey := hex.EncodeToString(hashedPublicKey) encodedPublicKey := hex.EncodeToString(hashedPublicKey)
return c.messageProcessor.JoinPublic(encodedPublicKey) return c.messageProcessor.JoinPublic(encodedPublicKey)

View File

@ -406,19 +406,50 @@ func (api *PublicAPI) UpdateMailservers(enodes []string) error {
// PushNotifications server // PushNotifications server
func (api *PublicAPI) StartPushNotificationServer() error { func (api *PublicAPI) StartPushNotificationServer() error {
// update settings
return api.service.messenger.StartPushNotificationServer() return api.service.messenger.StartPushNotificationServer()
} }
func (api *PublicAPI) StopPushNotificationServer() error { func (api *PublicAPI) StopPushNotificationServer() error {
// update settings
return api.service.messenger.StopPushNotificationServer() return api.service.messenger.StopPushNotificationServer()
} }
// PushNotification client // PushNotification client
func (api *PublicAPI) RegisterForPushNotifications(ctx context.Context, deviceToken string) ([]*push_notification_client.PushNotificationServer, error) { 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) 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 { func (api *PublicAPI) AddPushNotificationServer(ctx context.Context, publicKeyBytes types.HexBytes) error {
publicKey, err := crypto.UnmarshalPubkey(publicKeyBytes) publicKey, err := crypto.UnmarshalPubkey(publicKeyBytes)
if err != nil { if err != nil {

View File

@ -469,7 +469,7 @@ func buildMessengerOptions(
return nil, err return nil, err
} }
if config.PushNotificationServerEnabled || settings.PushNotificationServerEnabled { if settings.PushNotificationServerEnabled {
config := &push_notification_server.Config{ config := &push_notification_server.Config{
Logger: logger, Logger: logger,
} }