Skip to content

Commit 8039f50

Browse files
committed
Use unicode domain display name for third party chat pushes
1 parent d7efbab commit 8039f50

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

pkg/code/push/notifications.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
code_data "github.com/code-payments/code-server/pkg/code/data"
1818
"github.com/code-payments/code-server/pkg/code/data/chat"
1919
"github.com/code-payments/code-server/pkg/code/localization"
20+
"github.com/code-payments/code-server/pkg/code/thirdparty"
2021
"github.com/code-payments/code-server/pkg/kin"
2122
push_lib "github.com/code-payments/code-server/pkg/push"
2223
)
@@ -168,6 +169,13 @@ func SendChatMessagePushNotification(
168169
chatProperties, ok := chat_util.InternalChatProperties[chatTitle]
169170
if ok {
170171
chatTitle = chatProperties.TitleLocalizationKey
172+
} else {
173+
domainDisplayName, err := thirdparty.GetDomainDisplayName(chatTitle)
174+
if err == nil {
175+
chatTitle = domainDisplayName
176+
} else {
177+
log.WithError(err).Warn("failure getting domain display name")
178+
}
171179
}
172180

173181
// Best-effort try to update the badge count before pushing message content

pkg/code/thirdparty/domain.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010

1111
"github.com/pkg/errors"
1212
"golang.org/x/net/idna"
13+
"golang.org/x/text/cases"
14+
"golang.org/x/text/language"
1315

1416
"github.com/code-payments/code-server/pkg/code/common"
1517
"github.com/code-payments/code-server/pkg/netutil"
@@ -87,3 +89,21 @@ func GetAsciiBaseDomain(domain string) (string, error) {
8789
}
8890
return strings.ToLower(fmt.Sprintf("%s.%s", parts[len(parts)-2], parts[len(parts)-1])), nil
8991
}
92+
93+
// GetDomainDisplayName gets the display version of a domain within
94+
// UI elements of the Code app.
95+
func GetDomainDisplayName(domain string) (string, error) {
96+
parts := strings.Split(domain, ".")
97+
if len(parts) < 2 {
98+
return "", errors.New("value must have base domain and tld")
99+
}
100+
101+
displayName, err := idna.Display.ToUnicode(domain)
102+
if err != nil {
103+
return "", errors.Wrap(err, "error converting string to unicode")
104+
}
105+
106+
displayName = cases.Title(language.English, cases.NoLower).String(displayName)
107+
108+
return displayName, nil
109+
}

pkg/code/thirdparty/domain_test.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"strings"
77
"testing"
88

9-
"github.com/code-payments/code-server/pkg/testutil"
109
"github.com/code-payments/code-server/pkg/code/common"
10+
"github.com/code-payments/code-server/pkg/testutil"
1111
"github.com/stretchr/testify/assert"
1212
"github.com/stretchr/testify/require"
1313
)
@@ -57,6 +57,47 @@ func TestGetAsciiBaseDomain(t *testing.T) {
5757
}
5858
}
5959

60+
func TestGetDomainDisplayValue(t *testing.T) {
61+
for _, tc := range []struct {
62+
input string
63+
expected string
64+
isError bool
65+
}{
66+
{
67+
input: "app.getcode.com",
68+
expected: "App.getcode.com",
69+
},
70+
{
71+
input: "getcode.com",
72+
expected: "Getcode.com",
73+
},
74+
{
75+
input: "UPPERCASE.com",
76+
expected: "Uppercase.com",
77+
},
78+
{
79+
input: "xn--bcher-kva.com",
80+
expected: "Bücher.com",
81+
},
82+
{
83+
input: "xn--cher-zra.com",
84+
expected: "Ücher.com",
85+
},
86+
{
87+
input: "localhost",
88+
isError: true,
89+
},
90+
} {
91+
actual, err := GetDomainDisplayName(tc.input)
92+
if tc.isError {
93+
assert.Error(t, err)
94+
} else {
95+
require.NoError(t, err)
96+
assert.Equal(t, tc.expected, actual)
97+
}
98+
}
99+
}
100+
60101
func TestVerifyDomainNameOwnership(t *testing.T) {
61102
ctx := context.Background()
62103

0 commit comments

Comments
 (0)