Skip to content
Snippets Groups Projects
Commit a3042cfa authored by Josh Brooks's avatar Josh Brooks
Browse files

Remove BackupMissingFacts; NewManagerFromBackup has that responsibility

parent 0646f139
No related branches found
No related tags found
2 merge requests!233Modify restore to call user-defined bindings callback. Add Sent requests to...,!231Revert "Update store to print changes to the partners list"
...@@ -9,7 +9,6 @@ package bindings ...@@ -9,7 +9,6 @@ package bindings
import ( import (
"fmt" "fmt"
jww "github.com/spf13/jwalterweatherman"
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
...@@ -49,7 +48,16 @@ func NewUserDiscovery(client *Client) (*UserDiscovery, error) { ...@@ -49,7 +48,16 @@ func NewUserDiscovery(client *Client) (*UserDiscovery, error) {
// NewUserDiscoveryFromBackup returns a new user discovery object. It // NewUserDiscoveryFromBackup returns a new user discovery object. It
// wil set up the manager with the backup data. Pass into it the backed up // 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 // Only call this once. It must be called after StartNetworkFollower
// is called and will fail if the network has never been contacted. // 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 // This function technically has a memory leak because it causes both sides of
...@@ -58,43 +66,28 @@ func NewUserDiscovery(client *Client) (*UserDiscovery, error) { ...@@ -58,43 +66,28 @@ func NewUserDiscovery(client *Client) (*UserDiscovery, error) {
// for the life of the program. // for the life of the program.
// This must be called while start network follower is running. // This must be called while start network follower is running.
func NewUserDiscoveryFromBackup(client *Client, func NewUserDiscoveryFromBackup(client *Client,
email, phone, username string) (*UserDiscovery, error) { email, phone string) (*UserDiscovery, error) {
single, err := client.getSingle() single, err := client.getSingle()
if err != nil { if err != nil {
return nil, errors.WithMessage(err, "Failed to create User Discovery Manager") 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 var emailFact, phoneFact fact.Fact
// Parse email as a fact, if it exists if len(email) > 2 {
if email != "" { emailFact, err = fact.UnstringifyFact(email)
emailFact, err = fact.NewFact(fact.Email, email)
if err != nil { if err != nil {
return nil, errors.WithMessagef(err, "Failed to parse "+ return nil, errors.WithMessagef(err, "Failed to parse malformed email fact: %s", email)
"stringified email fact %q", email)
} }
} else {
jww.WARN.Printf("Loading manager without a registered email")
} }
// Parse phone number as a fact, if it exists if len(phone) > 2 {
if phone != "" { phoneFact, err = fact.UnstringifyFact(phone)
phoneFact, err = fact.NewFact(fact.Phone, phone)
if err != nil { if err != nil {
return nil, errors.WithMessagef(err, "Failed to parse "+ return nil, errors.WithMessagef(err, "Failed to parse malformed phone fact: %s", phone)
"stringified phone fact %q", 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 { if err != nil {
return nil, errors.WithMessage(err, "Failed to create User Discovery Manager") return nil, errors.WithMessage(err, "Failed to create User Discovery Manager")
} else { } else {
...@@ -159,37 +152,6 @@ func (ud *UserDiscovery) RemoveUser(fStr string) error { ...@@ -159,37 +152,6 @@ func (ud *UserDiscovery) RemoveUser(fStr string) error {
return ud.ud.RemoveUser(f) 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 // SearchCallback returns the result of a search
type SearchCallback interface { type SearchCallback interface {
Callback(contacts *ContactList, error string) Callback(contacts *ContactList, error string)
......
...@@ -116,10 +116,6 @@ func (s *Store) BackUpMissingFacts(email, phone fact.Fact) error { ...@@ -116,10 +116,6 @@ func (s *Store) BackUpMissingFacts(email, phone fact.Fact) error {
s.mux.Lock() s.mux.Lock()
defer s.mux.Unlock() defer s.mux.Unlock()
if isFactZero(email) && isFactZero(phone) {
return errors.New(backupMissingAllZeroesFactErr)
}
modifiedEmail, modifiedPhone := false, false modifiedEmail, modifiedPhone := false, false
// Handle email if it is not zero (empty string) // Handle email if it is not zero (empty string)
......
...@@ -110,7 +110,7 @@ func NewManager(client *api.Client, single *single.Manager) (*Manager, error) { ...@@ -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 // It will construct a manager that is already registered and restore
// already registered facts into store. // already registered facts into store.
func NewManagerFromBackup(client *api.Client, single *single.Manager, func NewManagerFromBackup(client *api.Client, single *single.Manager,
fl fact.FactList) (*Manager, error) { email, phone fact.Fact) (*Manager, error) {
jww.INFO.Println("ud.NewManager()") jww.INFO.Println("ud.NewManager()")
if client.NetworkFollowerStatus() != api.Running { if client.NetworkFollowerStatus() != api.Running {
return nil, errors.New( return nil, errors.New(
...@@ -128,7 +128,7 @@ func NewManagerFromBackup(client *api.Client, single *single.Manager, ...@@ -128,7 +128,7 @@ func NewManagerFromBackup(client *api.Client, single *single.Manager,
} }
err := m.client.GetStorage().GetUd(). err := m.client.GetStorage().GetUd().
RestoreFromBackUp(fl) BackUpMissingFacts(email, phone)
if err != nil { if err != nil {
return nil, errors.WithMessage(err, "Failed to restore UD store "+ return nil, errors.WithMessage(err, "Failed to restore UD store "+
"from backup") "from backup")
...@@ -213,17 +213,6 @@ func (m *Manager) UnsetAlternativeUserDiscovery() error { ...@@ -213,17 +213,6 @@ func (m *Manager) UnsetAlternativeUserDiscovery() error {
return nil 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 // GetFacts returns a list of fact.Fact objects that exist within the
// Store's registeredFacts map. // Store's registeredFacts map.
func (m *Manager) GetFacts() []fact.Fact { func (m *Manager) GetFacts() []fact.Fact {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment