Merge branch 'profilefix' of cwtch.im/cwtch into master
This commit is contained in:
commit
0218159114
15
app/app.go
15
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) {
|
func (app *application) CreatePeer(name string, password string) (peer.CwtchPeer, error) {
|
||||||
log.Debugf("CreatePeer(%v)\n", name)
|
log.Debugf("CreatePeer(%v)\n", name)
|
||||||
|
|
||||||
// TODO: eventBus per profile
|
profile := storage.NewProfile(name)
|
||||||
p := peer.NewCwtchPeer(name)
|
profileStore := storage.NewProfileStore(app.eventBus, path.Join(app.directory, "profiles", profile.LocalID), password, profile)
|
||||||
|
pc := profileStore.GetProfileCopy()
|
||||||
profileStore := storage.NewProfileStore(app.eventBus, path.Join(app.directory, "profiles", p.GetProfile().LocalID), password)
|
p := peer.FromProfile(pc)
|
||||||
err := profileStore.Init(name)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
p.Init(app.acn, app.eventBus)
|
p.Init(app.acn, app.eventBus)
|
||||||
_, exists := app.peers[p.GetProfile().Onion]
|
_, exists := app.peers[p.GetProfile().Onion]
|
||||||
if exists {
|
if exists {
|
||||||
|
@ -85,7 +80,7 @@ func (app *application) LoadProfiles(password string) error {
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
|
|
||||||
// TODO: Per profile eventBus
|
// 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()
|
err = profileStore.Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -25,7 +25,6 @@ type profileStore struct {
|
||||||
|
|
||||||
// ProfileStore is an interface to managing the storage of Cwtch Profiles
|
// ProfileStore is an interface to managing the storage of Cwtch Profiles
|
||||||
type ProfileStore interface {
|
type ProfileStore interface {
|
||||||
Init(name string) error
|
|
||||||
Load() error
|
Load() error
|
||||||
Shutdown()
|
Shutdown()
|
||||||
GetProfileCopy() *model.Profile
|
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
|
// NewProfileStore returns a profile store backed by a filestore listening for events and saving them
|
||||||
// directory should be $appDir/profiles/$rand
|
// 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)
|
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)
|
ps.queue = event.NewEventQueue(100)
|
||||||
go ps.eventHandler()
|
go ps.eventHandler()
|
||||||
|
|
||||||
|
@ -52,9 +51,10 @@ func NewProfileStore(eventManager *event.Manager, directory, password string) Pr
|
||||||
return ps
|
return ps
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ps *profileStore) Init(name string) error {
|
// NewProfile creates a new profile for use in the profile store.
|
||||||
ps.profile = model.GenerateNewProfile(name)
|
func NewProfile(name string) *model.Profile {
|
||||||
return ps.save()
|
profile := model.GenerateNewProfile(name)
|
||||||
|
return profile
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ps *profileStore) save() error {
|
func (ps *profileStore) save() error {
|
||||||
|
|
|
@ -17,17 +17,15 @@ func TestProfileStoreWriteRead(t *testing.T) {
|
||||||
os.RemoveAll(testingDir)
|
os.RemoveAll(testingDir)
|
||||||
eventBus := new(event.Manager)
|
eventBus := new(event.Manager)
|
||||||
eventBus.Initialize()
|
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}))
|
eventBus.Publish(event.NewEvent(event.SetAttribute, map[event.Field]string{event.Key: testKey, event.Data: testVal}))
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
groupid, invite, err := ps1.GetProfileCopy().StartGroup("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd")
|
groupid, invite, err := profile.StartGroup("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Creating group: %v\n", err)
|
t.Errorf("Creating group: %v\n", err)
|
||||||
}
|
}
|
||||||
|
@ -48,13 +46,13 @@ func TestProfileStoreWriteRead(t *testing.T) {
|
||||||
|
|
||||||
ps1.Shutdown()
|
ps1.Shutdown()
|
||||||
|
|
||||||
ps2 := NewProfileStore(eventBus, testingDir, password)
|
ps2 := NewProfileStore(eventBus, testingDir, password, nil)
|
||||||
err = ps2.Load()
|
err = ps2.Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Error createing profileStore: %v\n", err)
|
t.Errorf("Error createing profileStore: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
profile := ps2.GetProfileCopy()
|
profile = ps2.GetProfileCopy()
|
||||||
if profile.Name != testProfileName {
|
if profile.Name != testProfileName {
|
||||||
t.Errorf("Profile name from loaded profile incorrect. Expected: '%v' Actual: '%v'\n", testProfileName, profile.Name)
|
t.Errorf("Profile name from loaded profile incorrect. Expected: '%v' Actual: '%v'\n", testProfileName, profile.Name)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue