diff --git a/bindings/ud.go b/bindings/ud.go index 5c20b78fd047aac309f29a080d3b1b243237ef8d..cc3c5164f761469acbc60c07d5e876950bd4f6d6 100644 --- a/bindings/ud.go +++ b/bindings/ud.go @@ -9,7 +9,6 @@ package bindings import ( "fmt" - jww "github.com/spf13/jwalterweatherman" "time" "github.com/pkg/errors" @@ -49,7 +48,16 @@ func NewUserDiscovery(client *Client) (*UserDiscovery, error) { // NewUserDiscoveryFromBackup returns a new user discovery object. It // wil set up the manager with the backup data. Pass into it the backed up -// facts, one email, phone and username each. +// facts, one email and phone number each. This will add the registered facts +// to the backed Store. Any one of these fields may be empty, +// however both fields being empty will cause an error. Any other fact that is not +// an email or phone number will return an error. You may only add a fact for the +// accepted types once each. If you attempt to back up a fact type that has already +// been backed up, an error will be returned. Anytime an error is returned, it means +// the backup was not successful. +// NOTE: Do not use this as a direct store operation. This feature is intended to add facts +// to a backend store that have ALREADY BEEN REGISTERED on the account. +// THIS IS NOT FOR ADDING NEWLY REGISTERED FACTS. That is handled on the backend. // Only call this once. It must be called after StartNetworkFollower // is called and will fail if the network has never been contacted. // This function technically has a memory leak because it causes both sides of @@ -58,43 +66,28 @@ func NewUserDiscovery(client *Client) (*UserDiscovery, error) { // for the life of the program. // This must be called while start network follower is running. func NewUserDiscoveryFromBackup(client *Client, - email, phone, username string) (*UserDiscovery, error) { + email, phone string) (*UserDiscovery, error) { single, err := client.getSingle() if err != nil { return nil, errors.WithMessage(err, "Failed to create User Discovery Manager") } - // Parse username as a fact, which should not be empty - userFact, err := fact.NewFact(fact.Username, username) - if err != nil { - return nil, errors.WithMessagef(err, "Failed to parse "+ - "stringified username fact %q", username) - } - var emailFact, phoneFact fact.Fact - // Parse email as a fact, if it exists - if email != "" { - emailFact, err = fact.NewFact(fact.Email, email) + if len(email) > 2 { + emailFact, err = fact.UnstringifyFact(email) if err != nil { - return nil, errors.WithMessagef(err, "Failed to parse "+ - "stringified email fact %q", email) + return nil, errors.WithMessagef(err, "Failed to parse malformed email fact: %s", email) } - } else { - jww.WARN.Printf("Loading manager without a registered email") } - // Parse phone number as a fact, if it exists - if phone != "" { - phoneFact, err = fact.NewFact(fact.Phone, phone) + if len(phone) > 2 { + phoneFact, err = fact.UnstringifyFact(phone) if err != nil { - return nil, errors.WithMessagef(err, "Failed to parse "+ - "stringified phone fact %q", phone) + return nil, errors.WithMessagef(err, "Failed to parse malformed phone fact: %s", phone) } - } else { - jww.WARN.Printf("Loading manager without a registered phone number") } - m, err := ud.NewManagerFromBackup(&client.api, single, fact.FactList{userFact, emailFact, phoneFact}) + m, err := ud.NewManagerFromBackup(&client.api, single, emailFact, phoneFact) if err != nil { return nil, errors.WithMessage(err, "Failed to create User Discovery Manager") } else { @@ -159,37 +152,6 @@ func (ud *UserDiscovery) RemoveUser(fStr string) error { return ud.ud.RemoveUser(f) } -//BackUpMissingFacts adds a registered fact to the Store object and saves -// it to storage. It can take in both an email or a phone number, passed into -// the function in that order. Any one of these fields may be empty, -// however both fields being empty will cause an error. Any other fact that is not -// an email or phone number will return an error. You may only add a fact for the -// accepted types once each. If you attempt to back up a fact type that has already -// been backed up, an error will be returned. Anytime an error is returned, it means -// the backup was not successful. -// NOTE: Do not use this as a direct store operation. This feature is intended to add facts -// to a backend store that have ALREADY BEEN REGISTERED on the account. -// THIS IS NOT FOR ADDING NEWLY REGISTERED FACTS. That is handled on the backend. -func (ud *UserDiscovery) BackUpMissingFacts(email, phone string) error { - var emailFact, phoneFact fact.Fact - var err error - if len(email) > 2 { - emailFact, err = fact.UnstringifyFact(email) - if err != nil { - return errors.WithMessagef(err, "Failed to parse malformed email fact: %s", email) - } - } - - if len(phone) > 2 { - phoneFact, err = fact.UnstringifyFact(phone) - if err != nil { - return errors.WithMessagef(err, "Failed to parse malformed phone fact: %s", phone) - } - } - - return ud.ud.BackUpMissingFacts(emailFact, phoneFact) -} - // SearchCallback returns the result of a search type SearchCallback interface { Callback(contacts *ContactList, error string) diff --git a/storage/ud/facts.go b/storage/ud/facts.go index 460d1dbb3a6e4120e8ca0ef3fb08f15a380af9dc..9ebfa59ba45ceaec15c31b909f0bd86ea2c009e6 100644 --- a/storage/ud/facts.go +++ b/storage/ud/facts.go @@ -116,10 +116,6 @@ func (s *Store) BackUpMissingFacts(email, phone fact.Fact) error { s.mux.Lock() defer s.mux.Unlock() - if isFactZero(email) && isFactZero(phone) { - return errors.New(backupMissingAllZeroesFactErr) - } - modifiedEmail, modifiedPhone := false, false // Handle email if it is not zero (empty string) diff --git a/ud/manager.go b/ud/manager.go index efe1c2f349163117c2bee789e14c2227031dd25c..9ef3f996dc0cf4765740ba30872ba5c5b33de357 100644 --- a/ud/manager.go +++ b/ud/manager.go @@ -110,7 +110,7 @@ func NewManager(client *api.Client, single *single.Manager) (*Manager, error) { // It will construct a manager that is already registered and restore // already registered facts into store. func NewManagerFromBackup(client *api.Client, single *single.Manager, - fl fact.FactList) (*Manager, error) { + email, phone fact.Fact) (*Manager, error) { jww.INFO.Println("ud.NewManager()") if client.NetworkFollowerStatus() != api.Running { return nil, errors.New( @@ -128,7 +128,7 @@ func NewManagerFromBackup(client *api.Client, single *single.Manager, } err := m.client.GetStorage().GetUd(). - RestoreFromBackUp(fl) + BackUpMissingFacts(email, phone) if err != nil { return nil, errors.WithMessage(err, "Failed to restore UD store "+ "from backup") @@ -213,17 +213,6 @@ func (m *Manager) UnsetAlternativeUserDiscovery() error { return nil } -// BackUpMissingFacts adds a registered fact to the Store object. It can take in both an -// email and a phone number. One or the other may be nil, however both is considered -// an error. It checks for the proper fact type for the associated fact. -// Any other fact.FactType is not accepted and returns an error and nothing is backed up. -// If you attempt to back up a fact type that has already been backed up, -// an error will be returned and nothing will be backed up. -// Otherwise, it adds the fact and returns whether the Store saved successfully. -func (m *Manager) BackUpMissingFacts(email, phone fact.Fact) error { - return m.storage.GetUd().BackUpMissingFacts(email, phone) -} - // GetFacts returns a list of fact.Fact objects that exist within the // Store's registeredFacts map. func (m *Manager) GetFacts() []fact.Fact {