fix(GODT-28022): Missing user identity service methods

Add methods to access user identity state from the outside.
This commit is contained in:
Leander Beernaert
2023-07-28 14:54:13 +02:00
parent c4b75c6f34
commit 0048767022
5 changed files with 138 additions and 10 deletions

View File

@ -21,14 +21,13 @@ import (
"context"
"crypto/subtle"
"fmt"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"strings"
"github.com/ProtonMail/go-proton-api"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/ProtonMail/proton-bridge/v3/internal/usertypes"
"github.com/ProtonMail/proton-bridge/v3/pkg/algo"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)
// State holds all the required user identity state. The idea of this type is that
@ -82,6 +81,13 @@ func (s *State) GetAddr(email string) (proton.Address, error) {
return proton.Address{}, fmt.Errorf("address %s not found", email)
}
// GetAddrByID returns the address for the given addressID.
func (s *State) GetAddrByID(id string) (proton.Address, bool) {
v, ok := s.Addresses[id]
return v, ok
}
// GetPrimaryAddr returns the primary address for this user.
func (s *State) GetPrimaryAddr() (proton.Address, error) {
if len(s.AddressesSorted) == 0 {
@ -204,9 +210,15 @@ func (s *State) OnAddressEvents(events []proton.AddressEvent) {
}
func (s *State) Clone() *State {
mapCopy := make(map[string]proton.Address, len(s.Addresses))
sliceCopy := make([]proton.Address, len(s.AddressesSorted))
copy(sliceCopy, s.AddressesSorted)
maps.Copy(mapCopy, s.Addresses)
return &State{
AddressesSorted: slices.Clone(s.AddressesSorted),
Addresses: maps.Clone(s.Addresses),
AddressesSorted: sliceCopy,
Addresses: mapCopy,
User: s.User,
provider: s.provider,
}