Skip to content

Commit

Permalink
Merge pull request #68 from planetary-social/localized-follow-alerts
Browse files Browse the repository at this point in the history
Use localized follow alerts
  • Loading branch information
dcadenas authored Sep 24, 2024
2 parents 2a8545e + 236a885 commit 207c4b9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
13 changes: 8 additions & 5 deletions service/adapters/apns/apns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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())
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 19 additions & 4 deletions service/adapters/apns/apns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 207c4b9

Please sign in to comment.