Skip to content

Commit c9bc6f7

Browse files
committed
feat(GODT-3122): server now support display name for addresses.
1 parent 0ea2258 commit c9bc6f7

File tree

4 files changed

+80
-18
lines changed

4 files changed

+80
-18
lines changed

address_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,50 @@ import (
99
"github.com/stretchr/testify/require"
1010
)
1111

12+
func TestAddress_DisplayName(t *testing.T) {
13+
s := server.New()
14+
defer s.Close()
15+
16+
// Create a user on the server.
17+
userID, _, err := s.CreateUser("user", []byte("pass"))
18+
require.NoError(t, err)
19+
addr1, err := s.CreateAddress(userID, "[email protected]", []byte("pass"))
20+
require.NoError(t, err)
21+
require.NoError(t, s.ChangeAddressDisplayName(userID, addr1, "User 1"))
22+
23+
addr2, err := s.CreateAddress(userID, "[email protected]", []byte("pass"))
24+
require.NoError(t, err)
25+
require.NoError(t, s.ChangeAddressDisplayName(userID, addr2, "User 2"))
26+
27+
require.NoError(t, err)
28+
29+
m := proton.New(
30+
proton.WithHostURL(s.GetHostURL()),
31+
proton.WithTransport(proton.InsecureTransport()),
32+
)
33+
defer m.Close()
34+
35+
// Create one session for the user.
36+
c, auth, err := m.NewClientWithLogin(context.Background(), "user", []byte("pass"))
37+
require.NoError(t, err)
38+
require.Equal(t, userID, auth.UserID)
39+
40+
// Get addresses for the user.
41+
addrs, err := c.GetAddresses(context.Background())
42+
require.NoError(t, err)
43+
44+
for _, addr := range addrs {
45+
switch addr.ID {
46+
case addr1:
47+
require.Equal(t, addr.Email, "[email protected]")
48+
require.Equal(t, addr.DisplayName, "User 1")
49+
case addr2:
50+
require.Equal(t, addr.Email, "[email protected]")
51+
require.Equal(t, addr.DisplayName, "User 2")
52+
}
53+
}
54+
}
55+
1256
func TestAddress_Types(t *testing.T) {
1357
s := server.New()
1458
defer s.Close()

server/backend/address.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
)
88

99
type address struct {
10-
addrID string
11-
email string
12-
order int
13-
status proton.AddressStatus
14-
addrType proton.AddressType
15-
keys []key
16-
allowSend bool
10+
addrID string
11+
email string
12+
displayName string
13+
order int
14+
status proton.AddressStatus
15+
addrType proton.AddressType
16+
keys []key
17+
allowSend bool
1718
}
1819

1920
func (add *address) toAddress() proton.Address {
@@ -27,7 +28,7 @@ func (add *address) toAddress() proton.Address {
2728
Type: add.addrType,
2829

2930
Order: add.order,
30-
DisplayName: add.email,
31+
DisplayName: add.displayName,
3132

3233
Keys: xslices.Map(add.keys, func(key key) proton.Key {
3334
privKey, err := crypto.NewKeyFromArmored(key.key)

server/backend/backend.go

+23-10
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,14 @@ func (b *Backend) createAddress(
275275
addressID := uuid.NewString()
276276

277277
acc.addresses[addressID] = &address{
278-
addrID: addressID,
279-
email: email,
280-
order: len(acc.addresses) + 1,
281-
status: status,
282-
addrType: addrType,
283-
keys: keys,
284-
allowSend: allowSend,
278+
addrID: addressID,
279+
email: email,
280+
displayName: email,
281+
order: len(acc.addresses) + 1,
282+
status: status,
283+
addrType: addrType,
284+
keys: keys,
285+
allowSend: allowSend,
285286
}
286287

287288
var update update
@@ -302,15 +303,15 @@ func (b *Backend) createAddress(
302303
})
303304
}
304305

305-
func (b *Backend) ChangeAddressType(userID, addrId string, addrType proton.AddressType) error {
306+
func (b *Backend) ChangeAddressType(userID, addrID string, addrType proton.AddressType) error {
306307
return b.withAcc(userID, func(acc *account) error {
307308
for _, addr := range acc.addresses {
308-
if addr.addrID == addrId {
309+
if addr.addrID == addrID {
309310
addr.addrType = addrType
310311
return nil
311312
}
312313
}
313-
return fmt.Errorf("no addrID matching %s for user %s", addrId, userID)
314+
return fmt.Errorf("no addrID matching %s for user %s", addrID, userID)
314315
})
315316
}
316317

@@ -326,6 +327,18 @@ func (b *Backend) ChangeAddressAllowSend(userID, addrId string, allowSend bool)
326327
})
327328
}
328329

330+
func (b *Backend) ChangeAddressDisplayName(userID, addrID, displayName string) error {
331+
return b.withAcc(userID, func(acc *account) error {
332+
for _, addr := range acc.addresses {
333+
if addr.addrID == addrID {
334+
addr.displayName = displayName
335+
return nil
336+
}
337+
}
338+
return fmt.Errorf("no addrID matching %s for user %s", addrID, userID)
339+
})
340+
}
341+
329342
func (b *Backend) CreateAddressKey(userID, addrID string, password []byte) error {
330343
return b.withAcc(userID, func(acc *account) error {
331344
token, err := crypto.RandomToken(32)

server/server.go

+4
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ func (s *Server) ChangeAddressType(userID, addrId string, addrType proton.Addres
163163
return s.b.ChangeAddressType(userID, addrId, addrType)
164164
}
165165

166+
func (s *Server) ChangeAddressDisplayName(userID, addrID, displayName string) error {
167+
return s.b.ChangeAddressDisplayName(userID, addrID, displayName)
168+
}
169+
166170
func (s *Server) RemoveAddress(userID, addrID string) error {
167171
return s.b.RemoveAddress(userID, addrID)
168172
}

0 commit comments

Comments
 (0)