fix: normalize jpush aliases
This commit is contained in:
@@ -360,7 +360,7 @@ func preparePayloads(rcpt *push.Receipt, config *configType) ([]*jpushPayload, [
|
||||
}
|
||||
var audience map[string][]string
|
||||
if useAlias {
|
||||
audience = map[string][]string{"alias": {uid.UserId()}}
|
||||
audience = map[string][]string{"alias": {aliasForUid(uid)}}
|
||||
} else {
|
||||
var registrationIDs []string
|
||||
for _, device := range devices[uid] {
|
||||
@@ -584,6 +584,12 @@ func containsString(values []string, target string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func aliasForUid(uid t.Uid) string {
|
||||
// JPush alias does not accept '-' or '_'. Tinode user IDs are base64url-like,
|
||||
// so map them to legal characters which never appear in the original alphabet.
|
||||
return strings.NewReplacer("-", ".", "_", "|").Replace(uid.UserId())
|
||||
}
|
||||
|
||||
func providerDevicesForUsers(tenantID t.TenantID, provider string, uids ...t.Uid) (map[t.Uid][]t.DeviceDef, int, error) {
|
||||
if tokens, ok := store.DevicePushTokens.(store.DevicePushTokenProviderQueryInterface); ok {
|
||||
return tokens.GetAllByProvider(tenantID, provider, uids...)
|
||||
|
||||
@@ -97,7 +97,7 @@ func TestPreparePayloadsUsesAliasAndNormalizesP2PTopic(t *testing.T) {
|
||||
t.Fatalf("unexpected payload count: %d %#v", len(payloads), uids)
|
||||
}
|
||||
payload := payloads[0]
|
||||
if got := payload.Audience["alias"][0]; got != callee.UserId() {
|
||||
if got := payload.Audience["alias"][0]; got != aliasForUid(callee) {
|
||||
t.Fatalf("unexpected alias: %s", got)
|
||||
}
|
||||
userTopic, _ := types.P2PNameForUser(callee, topic)
|
||||
@@ -109,6 +109,16 @@ func TestPreparePayloadsUsesAliasAndNormalizesP2PTopic(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAliasForUidUsesJPushSafeCharacters(t *testing.T) {
|
||||
uid := types.ParseUserId("usrd6Ag5opa-ww")
|
||||
if uid.IsZero() {
|
||||
t.Fatal("test user id did not parse")
|
||||
}
|
||||
if got, want := aliasForUid(uid), "usrd6Ag5opa.ww"; got != want {
|
||||
t.Fatalf("unexpected alias: got %s want %s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparePayloadsCanUseRegistrationID(t *testing.T) {
|
||||
oldTokens := store.DevicePushTokens
|
||||
uid := types.Uid(42)
|
||||
|
||||
Reference in New Issue
Block a user