Browse Source

Fixing Profile Creation Bug

Sarah Jamie Lewis 1 month ago
parent
commit
5399a31a6f
3 changed files with 17 additions and 24 deletions
  1. 5 10
      app/app.go
  2. 6 6
      storage/profile_store.go
  3. 6 8
      storage/profile_store_test.go

+ 5 - 10
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

+ 6 - 6
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 {

+ 6 - 8
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)
 	}