From 5399a31a6f914d2cd4ed9bd1d3210809051b03b4 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Mon, 4 Feb 2019 10:25:58 -0800 Subject: [PATCH] Fixing Profile Creation Bug --- app/app.go | 15 +++++---------- storage/profile_store.go | 12 ++++++------ storage/profile_store_test.go | 14 ++++++-------- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/app/app.go b/app/app.go index f07cb6d..a477e6b 100644 --- a/app/app.go +++ b/app/app.go @@ -53,15 +53,10 @@ func NewApp(acn connectivity.ACN, appDirectory string) Application { func (app *application) CreatePeer(name string, password string) (peer.CwtchPeer, error) { log.Debugf("CreatePeer(%v)\n", name) - // TODO: eventBus per profile - p := peer.NewCwtchPeer(name) - - profileStore := storage.NewProfileStore(app.eventBus, path.Join(app.directory, "profiles", p.GetProfile().LocalID), password) - err := profileStore.Init(name) - - if err != nil { - return nil, err - } + profile := storage.NewProfile(name) + profileStore := storage.NewProfileStore(app.eventBus, path.Join(app.directory, "profiles", profile.LocalID), password, profile) + pc := profileStore.GetProfileCopy() + p := peer.FromProfile(pc) p.Init(app.acn, app.eventBus) _, exists := app.peers[p.GetProfile().Onion] if exists { @@ -85,7 +80,7 @@ func (app *application) LoadProfiles(password string) error { for _, file := range files { // TODO: Per profile eventBus - profileStore := storage.NewProfileStore(app.eventBus, path.Join(app.directory, "profiles", file.Name()), password) + profileStore := storage.NewProfileStore(app.eventBus, path.Join(app.directory, "profiles", file.Name()), password, nil) err = profileStore.Load() if err != nil { continue diff --git a/storage/profile_store.go b/storage/profile_store.go index a8e1c64..5eb7ead 100644 --- a/storage/profile_store.go +++ b/storage/profile_store.go @@ -25,7 +25,6 @@ type profileStore struct { // ProfileStore is an interface to managing the storage of Cwtch Profiles type ProfileStore interface { - Init(name string) error Load() error Shutdown() GetProfileCopy() *model.Profile @@ -33,9 +32,9 @@ type ProfileStore interface { // NewProfileStore returns a profile store backed by a filestore listening for events and saving them // directory should be $appDir/profiles/$rand -func NewProfileStore(eventManager *event.Manager, directory, password string) ProfileStore { +func NewProfileStore(eventManager *event.Manager, directory, password string, profile *model.Profile) ProfileStore { os.Mkdir(directory, 0700) - ps := &profileStore{fs: NewFileStore(directory, profileFilename, password), password: password, directory: directory, profile: nil, eventManager: eventManager, streamStores: map[string]StreamStore{}} + ps := &profileStore{fs: NewFileStore(directory, profileFilename, password), password: password, directory: directory, profile: profile, eventManager: eventManager, streamStores: map[string]StreamStore{}} ps.queue = event.NewEventQueue(100) go ps.eventHandler() @@ -52,9 +51,10 @@ func NewProfileStore(eventManager *event.Manager, directory, password string) Pr return ps } -func (ps *profileStore) Init(name string) error { - ps.profile = model.GenerateNewProfile(name) - return ps.save() +// NewProfile creates a new profile for use in the profile store. +func NewProfile(name string) *model.Profile { + profile := model.GenerateNewProfile(name) + return profile } func (ps *profileStore) save() error { diff --git a/storage/profile_store_test.go b/storage/profile_store_test.go index 9d304cb..5b3b131 100644 --- a/storage/profile_store_test.go +++ b/storage/profile_store_test.go @@ -17,17 +17,15 @@ func TestProfileStoreWriteRead(t *testing.T) { os.RemoveAll(testingDir) eventBus := new(event.Manager) eventBus.Initialize() - ps1 := NewProfileStore(eventBus, testingDir, password) + profile := NewProfile(testProfileName) + ps1 := NewProfileStore(eventBus, testingDir, password, profile) + - err := ps1.Init(testProfileName) - if err != nil { - t.Errorf("Error initializing profileStore: %v\n", err) - } eventBus.Publish(event.NewEvent(event.SetAttribute, map[event.Field]string{event.Key: testKey, event.Data: testVal})) time.Sleep(1 * time.Second) - groupid, invite, err := ps1.GetProfileCopy().StartGroup("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd") + groupid, invite, err := profile.StartGroup("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd") if err != nil { t.Errorf("Creating group: %v\n", err) } @@ -48,13 +46,13 @@ func TestProfileStoreWriteRead(t *testing.T) { ps1.Shutdown() - ps2 := NewProfileStore(eventBus, testingDir, password) + ps2 := NewProfileStore(eventBus, testingDir, password, nil) err = ps2.Load() if err != nil { t.Errorf("Error createing profileStore: %v\n", err) } - profile := ps2.GetProfileCopy() + profile = ps2.GetProfileCopy() if profile.Name != testProfileName { t.Errorf("Profile name from loaded profile incorrect. Expected: '%v' Actual: '%v'\n", testProfileName, profile.Name) }