diff --git a/models/device.go b/models/device.go index 49d1dfb..1777d61 100644 --- a/models/device.go +++ b/models/device.go @@ -7,8 +7,8 @@ import ( ) type Device struct { - VaultId string `gorm:"type:varchar(255);not null" json:"vault_id" binding:"required"` - PartyName string `gorm:"type:varchar(255);not null" json:"party_name" binding:"required"` + VaultId string `gorm:"type:varchar(255);not null;uniqueIndex:idx_vault_party" json:"vault_id" binding:"required"` + PartyName string `gorm:"type:varchar(255);not null;uniqueIndex:idx_vault_party" json:"party_name" binding:"required"` Token string `gorm:"type:text;not null" json:"token" binding:"required"` DeviceType string `gorm:"type:varchar(255);not null" json:"device_type" binding:"required"` // apple, android, or web } diff --git a/service/notification.go b/service/notification.go index d4455a0..e29b726 100644 --- a/service/notification.go +++ b/service/notification.go @@ -160,8 +160,8 @@ func (s *NotificationService) processAppleNotification(ctx context.Context, devi notification.Topic = appID p := payload.NewPayload().Alert(nil). AlertTitle("Vultisig Keysign request"). - AlertSubtitle("Vault: " + request.VaultName). - AlertBody(request.QRCodeData). + AlertSubtitle("Vault: "+request.VaultName). + Custom("deeplink", request.QRCodeData). Sound("default") notification.Payload = p // See Payload section below diff --git a/storage/database.go b/storage/database.go index e03e25a..74669d1 100644 --- a/storage/database.go +++ b/storage/database.go @@ -11,6 +11,7 @@ import ( "github.com/vultisig/notification/models" "gorm.io/driver/mysql" "gorm.io/gorm" + "gorm.io/gorm/clause" "gorm.io/gorm/logger" ) @@ -50,7 +51,12 @@ func (d *Database) RegisterDevice(ctx context.Context, device models.Device) err cancel() }() deviceModel := device.GetDeviceDBModel() - result := d.db.WithContext(newContext).Create(&deviceModel) + result := d.db.WithContext(newContext). + Clauses(clause.OnConflict{ + Columns: []clause.Column{{Name: "vault_id"}, {Name: "party_name"}}, + DoUpdates: clause.AssignmentColumns([]string{"token", "device_type", "updated_at"}), + }). + Create(&deviceModel) if result.Error != nil { return fmt.Errorf("failed to register device: %w", result.Error) } @@ -70,7 +76,7 @@ func (d *Database) UnregisterDevice(ctx context.Context, vaultId, tokenId string defer func() { cancel() }() - result := d.db.WithContext(newContext).Where("vault_id = ? and token = ?", vaultId, tokenId).Delete(&models.DeviceDBModel{}) + result := d.db.WithContext(newContext).Unscoped().Where("vault_id = ? and token = ?", vaultId, tokenId).Delete(&models.DeviceDBModel{}) if result.Error != nil { return fmt.Errorf("failed to unregister device: %w", result.Error) }