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

View File

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

View File

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

View File

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

View File

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