2020-07-01 12:04:09 +00:00
package push_notification_server
2020-06-30 08:30:58 +00:00
import (
2020-07-01 10:09:40 +00:00
"crypto/ecdsa"
2020-06-30 08:30:58 +00:00
"database/sql"
2020-07-01 10:09:40 +00:00
2020-07-01 12:04:09 +00:00
"github.com/golang/protobuf/proto"
"github.com/status-im/status-go/eth-node/crypto"
2020-07-01 10:09:40 +00:00
"github.com/status-im/status-go/protocol/protobuf"
2020-06-30 08:30:58 +00:00
)
2020-07-01 10:09:40 +00:00
type Persistence interface {
2020-07-01 12:04:09 +00:00
// GetPushNotificationOptions retrieve a push notification options from storage given a public key and installation id
2020-07-01 10:09:40 +00:00
GetPushNotificationOptions ( publicKey * ecdsa . PublicKey , installationID string ) ( * protobuf . PushNotificationOptions , error )
2020-07-01 12:04:09 +00:00
// DeletePushNotificationOptions deletes a push notification options from storage given a public key and installation id
DeletePushNotificationOptions ( publicKey * ecdsa . PublicKey , installationID string ) error
// SavePushNotificationOptions saves a push notification option to the db
SavePushNotificationOptions ( publicKey * ecdsa . PublicKey , options * protobuf . PushNotificationOptions ) error
2020-07-01 10:09:40 +00:00
}
type SQLitePersistence struct {
2020-06-30 08:30:58 +00:00
db * sql . DB
}
2020-07-01 10:09:40 +00:00
func NewSQLitePersistence ( db * sql . DB ) Persistence {
return & SQLitePersistence { db : db }
}
func ( p * SQLitePersistence ) GetPushNotificationOptions ( publicKey * ecdsa . PublicKey , installationID string ) ( * protobuf . PushNotificationOptions , error ) {
2020-07-01 12:04:09 +00:00
var marshaledOptions [ ] byte
err := p . db . QueryRow ( ` SELECT registration FROM push_notification_server_registrations WHERE public_key = ? AND installation_id = ? ` , crypto . CompressPubkey ( publicKey ) , installationID ) . Scan ( & marshaledOptions )
if err == sql . ErrNoRows {
return nil , nil
} else if err != nil {
return nil , err
}
options := & protobuf . PushNotificationOptions { }
if err := proto . Unmarshal ( marshaledOptions , options ) ; err != nil {
return nil , err
}
return options , nil
}
func ( p * SQLitePersistence ) SavePushNotificationOptions ( publicKey * ecdsa . PublicKey , options * protobuf . PushNotificationOptions ) error {
compressedPublicKey := crypto . CompressPubkey ( publicKey )
marshaledOptions , err := proto . Marshal ( options )
if err != nil {
return err
}
_ , err = p . db . Exec ( ` INSERT INTO push_notification_server_registrations (public_key, installation_id, version, registration) VALUES (?, ?, ?, ?) ` , compressedPublicKey , options . InstallationId , options . Version , marshaledOptions )
return err
}
func ( p * SQLitePersistence ) DeletePushNotificationOptions ( publicKey * ecdsa . PublicKey , installationID string ) error {
_ , err := p . db . Exec ( ` DELETE FROM push_notification_server_registrations WHERE public_key = ? AND installation_id = ? ` , crypto . CompressPubkey ( publicKey ) , installationID )
return err
2020-06-30 08:30:58 +00:00
}