GODT-1366: Simple lookup of index and select current user

This commit is contained in:
Jakub
2021-11-05 12:31:32 +01:00
parent 9984165798
commit 961742fa53
13 changed files with 106 additions and 17 deletions

View File

@ -22,7 +22,9 @@ package qt
import (
"context"
"encoding/base64"
"time"
"github.com/ProtonMail/proton-bridge/internal/events"
"github.com/ProtonMail/proton-bridge/internal/users"
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
)
@ -138,14 +140,40 @@ func (f *FrontendQt) finishLogin() {
return
}
_, err := f.bridge.FinishLogin(f.authClient, f.auth, f.password)
done := make(chan string)
f.eventListener.Add(events.UserChangeDone, done)
defer f.eventListener.Remove(events.UserChangeDone, done)
user, err := f.bridge.FinishLogin(f.authClient, f.auth, f.password)
if err != nil && err != users.ErrUserAlreadyConnected {
f.log.WithError(err).Errorf("Finish login failed")
f.qml.Login2PasswordErrorAbort(err.Error())
return
}
defer f.qml.LoginFinished()
// The user changed should be triggerd by FinishLogin but it is not
// guaranteed when this is going to happen. Therefore we should wait
// until we receive the signal from userChanged function.
f.waitForUserChangeDone(done, user.ID())
index := f.qml.Users().indexByID(user.ID())
f.log.WithField("index", index).Debug("Login finished")
defer f.qml.LoginFinished(index)
}
func (f *FrontendQt) waitForUserChangeDone(done <-chan string, userID string) {
for {
select {
case changedID := <-done:
if changedID == userID {
return
}
case <-time.After(2 * time.Second):
f.log.WithField("ID", userID).Warning("Login finished but user not added within 2 seconds")
return
}
}
}
func (f *FrontendQt) loginAbort(username string) {

View File

@ -64,7 +64,7 @@ type QMLBackend struct {
_ func() `signal:"login2PasswordRequested"`
_ func(errorMsg string) `signal:"login2PasswordError"`
_ func(errorMsg string) `signal:"login2PasswordErrorAbort"`
_ func() `signal:"loginFinished"`
_ func(index int) `signal:"loginFinished"`
_ func() `signal:"internetOff"`
_ func() `signal:"internetOn"`

View File

@ -23,6 +23,7 @@ package qt
import (
"sync"
"github.com/ProtonMail/proton-bridge/internal/events"
"github.com/ProtonMail/proton-bridge/internal/frontend/types"
"github.com/therecipe/qt/core"
)
@ -174,6 +175,8 @@ func (um *QMLUserModel) load() {
}
func (um *QMLUserModel) userChanged(userID string) {
defer um.f.eventListener.Emit(events.UserChangeDone, userID)
index := um.indexByIDNotSafe(userID)
user, err := um.f.bridge.GetUser(userID)