handle unregistered clients

This commit is contained in:
Andrea Maria Piana 2020-07-30 15:37:32 +02:00
parent 4b9d3df5ad
commit aa78b89b40
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
3 changed files with 18 additions and 6 deletions

View File

@ -39,7 +39,8 @@ func NewSQLitePersistence(db *sql.DB) Persistence {
func (p *SQLitePersistence) GetPushNotificationRegistrationByPublicKeyAndInstallationID(publicKey []byte, installationID string) (*protobuf.PushNotificationRegistration, error) {
var marshaledRegistration []byte
err := p.db.QueryRow(`SELECT registration FROM push_notification_server_registrations WHERE public_key = ? AND installation_id = ?`, publicKey, installationID).Scan(&marshaledRegistration)
var version uint64
err := p.db.QueryRow(`SELECT version,registration FROM push_notification_server_registrations WHERE public_key = ? AND installation_id = ?`, publicKey, installationID).Scan(&version, &marshaledRegistration)
if err == sql.ErrNoRows {
return nil, nil
@ -47,7 +48,14 @@ func (p *SQLitePersistence) GetPushNotificationRegistrationByPublicKeyAndInstall
return nil, err
}
registration := &protobuf.PushNotificationRegistration{}
registration := &protobuf.PushNotificationRegistration{
InstallationId: installationID,
Version: version,
}
if marshaledRegistration == nil {
return registration, nil
}
if err := proto.Unmarshal(marshaledRegistration, registration); err != nil {
return nil, err
@ -86,6 +94,10 @@ func (p *SQLitePersistence) GetPushNotificationRegistrationByPublicKeys(publicKe
}
registration := &protobuf.PushNotificationRegistration{}
// Skip if there's no registration
if marshaledRegistration == nil {
continue
}
if err := proto.Unmarshal(marshaledRegistration, registration); err != nil {
return nil, err
@ -97,7 +109,7 @@ func (p *SQLitePersistence) GetPushNotificationRegistrationByPublicKeys(publicKe
}
func (p *SQLitePersistence) GetPushNotificationRegistrationPublicKeys() ([][]byte, error) {
rows, err := p.db.Query(`SELECT public_key FROM push_notification_server_registrations`)
rows, err := p.db.Query(`SELECT public_key FROM push_notification_server_registrations WHERE registration IS NOT NULL`)
if err != nil {
return nil, err
}

View File

@ -336,11 +336,10 @@ func (s *Server) buildPushNotificationRequestResponseAndSendNotification(request
if err != nil {
s.config.Logger.Error("failed to retrieve registration", zap.Error(err))
report.Error = protobuf.PushNotificationReport_UNKNOWN_ERROR_TYPE
} else if registration == nil {
} else if registration == nil || registration.AccessToken == "" {
s.config.Logger.Warn("empty registration")
report.Error = protobuf.PushNotificationReport_NOT_REGISTERED
} else if registration.AccessToken != pn.AccessToken {
s.config.Logger.Warn("invalid access token")
report.Error = protobuf.PushNotificationReport_WRONG_TOKEN
} else {
// For now we just assume that the notification will be successful
@ -403,6 +402,7 @@ func (s *Server) buildPushNotificationRegistrationResponse(publicKey *ecdsa.Publ
}
if registration.Unregister {
s.config.Logger.Info("unregistering client")
// We save an empty registration, only keeping version and installation-id
emptyRegistration := &protobuf.PushNotificationRegistration{
Version: registration.Version,

View File

@ -444,7 +444,7 @@ func (api *PublicAPI) RegisterForPushNotifications(ctx context.Context, deviceTo
return api.service.messenger.RegisterForPushNotifications(ctx, deviceToken, apnTopic, tokenType)
}
func (api *PublicAPI) UnregisterForPushNotifications(ctx context.Context) error {
func (api *PublicAPI) UnregisterFromPushNotifications(ctx context.Context) error {
err := api.service.accountsDB.SaveSetting("remote-push-notifications-enabled?", false)
if err != nil {
return err