From c9bc6f71eef0ecfd4d5f3a02928c0ab6be30df5b Mon Sep 17 00:00:00 2001 From: Xavier Michelon Date: Thu, 16 Nov 2023 08:29:43 +0100 Subject: [PATCH] feat(GODT-3122): server now support display name for addresses. --- address_test.go | 44 +++++++++++++++++++++++++++++++++++++++ server/backend/address.go | 17 ++++++++------- server/backend/backend.go | 33 ++++++++++++++++++++--------- server/server.go | 4 ++++ 4 files changed, 80 insertions(+), 18 deletions(-) diff --git a/address_test.go b/address_test.go index 58c24ef..1dab9f9 100644 --- a/address_test.go +++ b/address_test.go @@ -9,6 +9,50 @@ import ( "github.com/stretchr/testify/require" ) +func TestAddress_DisplayName(t *testing.T) { + s := server.New() + defer s.Close() + + // Create a user on the server. + userID, _, err := s.CreateUser("user", []byte("pass")) + require.NoError(t, err) + addr1, err := s.CreateAddress(userID, "user1@pm.me", []byte("pass")) + require.NoError(t, err) + require.NoError(t, s.ChangeAddressDisplayName(userID, addr1, "User 1")) + + addr2, err := s.CreateAddress(userID, "user2@pm.me", []byte("pass")) + require.NoError(t, err) + require.NoError(t, s.ChangeAddressDisplayName(userID, addr2, "User 2")) + + require.NoError(t, err) + + m := proton.New( + proton.WithHostURL(s.GetHostURL()), + proton.WithTransport(proton.InsecureTransport()), + ) + defer m.Close() + + // Create one session for the user. + c, auth, err := m.NewClientWithLogin(context.Background(), "user", []byte("pass")) + require.NoError(t, err) + require.Equal(t, userID, auth.UserID) + + // Get addresses for the user. + addrs, err := c.GetAddresses(context.Background()) + require.NoError(t, err) + + for _, addr := range addrs { + switch addr.ID { + case addr1: + require.Equal(t, addr.Email, "user1@pm.me") + require.Equal(t, addr.DisplayName, "User 1") + case addr2: + require.Equal(t, addr.Email, "user2@pm.me") + require.Equal(t, addr.DisplayName, "User 2") + } + } +} + func TestAddress_Types(t *testing.T) { s := server.New() defer s.Close() diff --git a/server/backend/address.go b/server/backend/address.go index f755d00..e3b1e51 100644 --- a/server/backend/address.go +++ b/server/backend/address.go @@ -7,13 +7,14 @@ import ( ) type address struct { - addrID string - email string - order int - status proton.AddressStatus - addrType proton.AddressType - keys []key - allowSend bool + addrID string + email string + displayName string + order int + status proton.AddressStatus + addrType proton.AddressType + keys []key + allowSend bool } func (add *address) toAddress() proton.Address { @@ -27,7 +28,7 @@ func (add *address) toAddress() proton.Address { Type: add.addrType, Order: add.order, - DisplayName: add.email, + DisplayName: add.displayName, Keys: xslices.Map(add.keys, func(key key) proton.Key { privKey, err := crypto.NewKeyFromArmored(key.key) diff --git a/server/backend/backend.go b/server/backend/backend.go index bd1891f..6ae81b4 100644 --- a/server/backend/backend.go +++ b/server/backend/backend.go @@ -275,13 +275,14 @@ func (b *Backend) createAddress( addressID := uuid.NewString() acc.addresses[addressID] = &address{ - addrID: addressID, - email: email, - order: len(acc.addresses) + 1, - status: status, - addrType: addrType, - keys: keys, - allowSend: allowSend, + addrID: addressID, + email: email, + displayName: email, + order: len(acc.addresses) + 1, + status: status, + addrType: addrType, + keys: keys, + allowSend: allowSend, } var update update @@ -302,15 +303,15 @@ func (b *Backend) createAddress( }) } -func (b *Backend) ChangeAddressType(userID, addrId string, addrType proton.AddressType) error { +func (b *Backend) ChangeAddressType(userID, addrID string, addrType proton.AddressType) error { return b.withAcc(userID, func(acc *account) error { for _, addr := range acc.addresses { - if addr.addrID == addrId { + if addr.addrID == addrID { addr.addrType = addrType return nil } } - return fmt.Errorf("no addrID matching %s for user %s", addrId, userID) + return fmt.Errorf("no addrID matching %s for user %s", addrID, userID) }) } @@ -326,6 +327,18 @@ func (b *Backend) ChangeAddressAllowSend(userID, addrId string, allowSend bool) }) } +func (b *Backend) ChangeAddressDisplayName(userID, addrID, displayName string) error { + return b.withAcc(userID, func(acc *account) error { + for _, addr := range acc.addresses { + if addr.addrID == addrID { + addr.displayName = displayName + return nil + } + } + return fmt.Errorf("no addrID matching %s for user %s", addrID, userID) + }) +} + func (b *Backend) CreateAddressKey(userID, addrID string, password []byte) error { return b.withAcc(userID, func(acc *account) error { token, err := crypto.RandomToken(32) diff --git a/server/server.go b/server/server.go index 2ba3a4a..2b1b170 100644 --- a/server/server.go +++ b/server/server.go @@ -163,6 +163,10 @@ func (s *Server) ChangeAddressType(userID, addrId string, addrType proton.Addres return s.b.ChangeAddressType(userID, addrId, addrType) } +func (s *Server) ChangeAddressDisplayName(userID, addrID, displayName string) error { + return s.b.ChangeAddressDisplayName(userID, addrID, displayName) +} + func (s *Server) RemoveAddress(userID, addrID string) error { return s.b.RemoveAddress(userID, addrID) }