From 236a885c5d067ef83cc1ad1256943b2370d80276 Mon Sep 17 00:00:00 2001 From: Daniel Cadenas Date: Tue, 24 Sep 2024 19:09:40 -0300 Subject: [PATCH] Use localized follow alerts --- service/adapters/apns/apns.go | 13 ++++++++----- service/adapters/apns/apns_test.go | 23 +++++++++++++++++++---- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/service/adapters/apns/apns.go b/service/adapters/apns/apns.go index 06bb500..0783eb6 100644 --- a/service/adapters/apns/apns.go +++ b/service/adapters/apns/apns.go @@ -139,7 +139,8 @@ func FollowChangePayload(followChange domain.FollowChangeBatch) ([]byte, error) } func FollowChangePayloadWithValidation(followChange domain.FollowChangeBatch, validate bool) ([]byte, error) { - alertMessage := "" + alertObject := make(map[string]interface{}) + totalNpubs := len(followChange.Follows) if validate && totalNpubs > MAX_TOTAL_NPUBS { return nil, errors.New("FollowChangeBatch for followee " + followChange.Followee.Hex() + " has too many npubs (" + fmt.Sprint(totalNpubs) + "). MAX_TOTAL_NPUBS is " + fmt.Sprint(MAX_TOTAL_NPUBS)) @@ -149,12 +150,14 @@ func FollowChangePayloadWithValidation(followChange domain.FollowChangeBatch, va if singleChange { if strings.HasPrefix(followChange.FriendlyFollower, "npub") { - alertMessage = "You have a new follower!" + alertObject["loc-key"] = "newFollower" } else { - alertMessage = followChange.FriendlyFollower + " is a new follower!" + alertObject["loc-key"] = "namedNewFollower" + alertObject["loc-args"] = []interface{}{followChange.FriendlyFollower} } } else { - alertMessage = fmt.Sprintf("You have %d new followers!", len(followChange.Follows)) + alertObject["loc-key"] = "xNewFollowers" + alertObject["loc-args"] = []interface{}{fmt.Sprint(len(followChange.Follows))} } followeeNpub, error := nip19.EncodePublicKey(followChange.Followee.Hex()) @@ -184,7 +187,7 @@ func FollowChangePayloadWithValidation(followChange domain.FollowChangeBatch, va payload := map[string]interface{}{ "aps": map[string]interface{}{ - "alert": alertMessage, + "alert": alertObject, "sound": "default", "badge": 1, "thread-id": followeeNpub, diff --git a/service/adapters/apns/apns_test.go b/service/adapters/apns/apns_test.go index ef3e1c7..0b05ad3 100644 --- a/service/adapters/apns/apns_test.go +++ b/service/adapters/apns/apns_test.go @@ -24,7 +24,10 @@ func TestFollowChangePayload_SingleFollow(t *testing.T) { payload, err := apns.FollowChangePayload(batch) require.NoError(t, err) - expectedAlert := "You have a new follower!" + expectedAlert := map[string]interface{}{ + "loc-key": "newFollower", + } + expectedPayload := map[string]interface{}{ "aps": map[string]interface{}{ "alert": expectedAlert, @@ -60,7 +63,11 @@ func TestFollowChangePayload_MultipleFollowsUnfollows(t *testing.T) { payload, err := apns.FollowChangePayload(batch) require.NoError(t, err) - expectedAlert := "You have 2 new followers!" + expectedAlert := map[string]interface{}{ + "loc-key": "xNewFollowers", + "loc-args": []interface{}{"2"}, + } + expectedPayload := map[string]interface{}{ "aps": map[string]interface{}{ "alert": expectedAlert, @@ -94,7 +101,11 @@ func TestFollowChangePayload_SingleFollow_WithFriendlyFollower(t *testing.T) { payload, err := apns.FollowChangePayload(batch) require.NoError(t, err) - expectedAlert := "John Doe is a new follower!" + expectedAlert := map[string]interface{}{ + "loc-key": "namedNewFollower", + "loc-args": []interface{}{"John Doe"}, + } + expectedPayload := map[string]interface{}{ "aps": map[string]interface{}{ "alert": expectedAlert, @@ -136,7 +147,11 @@ func TestFollowChangePayload_BatchedFollow_WithNoFriendlyFollower(t *testing.T) payload, err := apns.FollowChangePayload(batch) require.NoError(t, err) - expectedAlert := "You have 2 new followers!" + expectedAlert := map[string]interface{}{ + "loc-key": "xNewFollowers", + "loc-args": []interface{}{"2"}, + } + expectedPayload := map[string]interface{}{ "aps": map[string]interface{}{ "alert": expectedAlert,