Skip to content

Commit

Permalink
feat(push_notification): add pushprovider update and test (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdeme committed Apr 27, 2022
1 parent 6944432 commit ef11aa6
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/AlecAivazis/survey/v2 v2.2.15
github.com/GetStream/stream-chat-go/v5 v5.7.0
github.com/GetStream/stream-chat-go/v5 v5.8.0
github.com/MakeNowJust/heredoc v1.0.0
github.com/cheynewallace/tabby v1.1.1
github.com/gizak/termui/v3 v3.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/AlecAivazis/survey/v2 v2.2.15 h1:6UNMnk+YGegYFiPfdTOyZDIN+m08x2nGnqOn15BWcEQ=
github.com/AlecAivazis/survey/v2 v2.2.15/go.mod h1:TH2kPCDU3Kqq7pLbnCWwZXDBjnhZtmsCle5EiYDJ2fg=
github.com/GetStream/stream-chat-go/v5 v5.7.0 h1:KCsxAF8UqDDRDPQbIh6SUOpW20klERPEE4gKpPFkRME=
github.com/GetStream/stream-chat-go/v5 v5.7.0/go.mod h1:ET7NyUYplNy8+tyliin6Q3kKwbd/+FHQWMAW6zucisY=
github.com/GetStream/stream-chat-go/v5 v5.8.0 h1:HpkoZQeIdZ8qdHejKbVth6/fu5ZQXlc3P+5vxNAt+aw=
github.com/GetStream/stream-chat-go/v5 v5.8.0/go.mod h1:ET7NyUYplNy8+tyliin6Q3kKwbd/+FHQWMAW6zucisY=
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8 h1:xzYJEypr/85nBpB11F9br+3HUrpgb+fcm5iADzXXYEw=
Expand Down
215 changes: 215 additions & 0 deletions pkg/cmd/chat/push/push.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package push

import (
"encoding/json"

stream_chat "github.com/GetStream/stream-chat-go/v5"
"github.com/GetStream/stream-cli/pkg/config"
"github.com/GetStream/stream-cli/pkg/utils"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
)

func NewCmds() []*cobra.Command {
return []*cobra.Command{
upsertCmd(),
deleteCmd(),
listCmd(),
testCmd(),
}
}

func upsertCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "upsert-pushprovider --properties [raw-json]",
Short: "Create or updates a push provider",
Long: `
The "--properties" parameter expects a raw json string that can be
unmarshalled into a stream_chat.PushProvider object on the Go SDK side.
See the example section.
Available properties:
type
name
description
disabled_at
disabled_reason
apn_auth_key
apn_key_id
apn_team_id
apn_topic
firebase_notification_template
firebase_apn_template
firebase_credentials
huawei_app_id
huawei_app_secret
xiaomi_package_name
xiaomi_app_secret
`,
Example: heredoc.Doc(`
# Setting up an APN push provider
$ stream-cli chat upsert-pushprovider --properties "{'type': 'apn', 'name': 'staging', 'apn_auth_key': 'key', 'apn_key_id': 'id', 'apn_topic': 'topic', 'apn_team_id': 'id'}"
# Setting up a Firebase push provider
$ stream-cli chat upsert-pushprovider --properties "{'type': 'firebase', 'name': 'staging', 'firebase_credentials': 'credentials'}"
# Setting up a Huawei push provider
$ stream-cli chat upsert-pushprovider --properties "{'type': 'huawei', 'name': 'staging', 'huawei_app_id': 'id', 'huawei_app_secret': 'secret'}"
# Setting up a Xiaomi push provider
$ stream-cli chat upsert-pushprovider --properties "{'type': 'xiaomi', 'name': 'staging', 'xiaomi_package_name': 'name', 'xiaomi_app_secret': 'secret'}"
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}

prop, _ := cmd.Flags().GetString("properties")

var p stream_chat.PushProvider
err = json.Unmarshal([]byte(prop), &p)
if err != nil {
return err
}

_, err = c.UpsertPushProvider(cmd.Context(), &p)
if err != nil {
return err
}

cmd.Println("Successfully updated push provider.")
return nil
},
}

fl := cmd.Flags()
fl.StringP("properties", "p", "", "[required] Raw json properties to send to the backend")
_ = cmd.MarkFlagRequired("properties")

return cmd
}

func listCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list-pushproviders --output-format [json|tree]",
Short: "List all push providers",
Example: heredoc.Doc(`
# List all push providers
$ stream-cli chat list-pushproviders
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}

resp, err := c.ListPushProviders(cmd.Context())
if err != nil {
return err
}

return utils.PrintObject(cmd, resp.PushProviders)
},
}

fl := cmd.Flags()
fl.StringP("output-format", "o", "json", "Output format. One of: json|tree")

return cmd
}

func deleteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete-pushprovider --push-provider-type [type] --push-provider-name [name]",
Short: "Delete a push provider",
Example: heredoc.Doc(`
# Delete an APN push provider
$ stream-cli chat delete-pushprovider --push-provider-type apn --push-provider-name staging
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}

providerName, _ := cmd.Flags().GetString("push-provider-name")
providerType, _ := cmd.Flags().GetString("push-provider-type")

_, err = c.DeletePushProvider(cmd.Context(), providerType, providerName)
if err != nil {
return err
}

cmd.Println("Successfully deleted push provider.")
return nil
},
}

fl := cmd.Flags()
fl.StringP("push-provider-type", "t", "", "[required] Push provider type")
fl.StringP("push-provider-name", "n", "", "[required] Push provider name")
_ = cmd.MarkFlagRequired("push-provider-type")
_ = cmd.MarkFlagRequired("push-provider-name")

return cmd
}

func testCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "test-push --message-id [string]" +
" --apn-template [string]" +
" --firebase-template [string]" +
" --firebase-data-template [string]" +
" --skip-devices [true|false]" +
" --push-provider-name [string]" +
" --push-provider-type [string]" +
" --user-id [string]" +
" --output-format [json|tree]",
Short: "Test push notifications",
Example: heredoc.Doc(`
# A test push notification for a certain message id
$ stream-cli chat test-push --message-id msgid --user-id id --skip-devices true
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}

msgID, _ := cmd.Flags().GetString("message-id")
skipDevices, _ := cmd.Flags().GetBool("skip-devices")
pushProviderName, _ := cmd.Flags().GetString("push-provider-name")
pushProviderType, _ := cmd.Flags().GetString("push-provider-type")
userID, _ := cmd.Flags().GetString("user-id")

p := &stream_chat.CheckPushRequest{
MessageID: msgID,
SkipDevices: &skipDevices,
PushProviderName: pushProviderName,
PushProviderType: pushProviderType,
UserID: userID,
}

resp, err := c.CheckPush(cmd.Context(), p)
if err != nil {
return err
}

return utils.PrintObject(cmd, resp)
},
}

fl := cmd.Flags()
fl.String("message-id", "", "[optional] Message id to test")
fl.Bool("skip-devices", false, "[optional] Whether to notify devices")
fl.String("push-provider-name", "", "[optional] Push provider name to use")
fl.String("push-provider-type", "", "[optional] Push provider type to use")
fl.String("user-id", "", "[optional] User id to initiate the test")
fl.StringP("output-format", "o", "json", "[optional] Output format. Can be json or tree")

return cmd
}
26 changes: 26 additions & 0 deletions pkg/cmd/chat/push/push_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package push

import (
"bytes"
"testing"

"github.com/GetStream/stream-cli/test"
"github.com/stretchr/testify/require"
)

func TestPushTest(t *testing.T) {
cmd := test.GetRootCmdWithSubCommands(NewCmds()...)
ch := test.InitChannel(t)
u := test.CreateUser()
msgID := test.CreateMessage(ch, u)
t.Cleanup(func() {
test.DeleteMessage(msgID)
test.DeleteChannel(ch)
test.DeleteUser(u)
})

cmd.SetArgs([]string{"test-push", "--message-id", msgID, "--user-id", u, "--skip-devices", "true"})
_, err := cmd.ExecuteC()
require.NoError(t, err)
require.Contains(t, cmd.OutOrStdout().(*bytes.Buffer).String(), msgID)
}
5 changes: 3 additions & 2 deletions pkg/cmd/chat/reaction/reaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ func NewCmds() []*cobra.Command {
return []*cobra.Command{
getCmd(),
sendCmd(),
deleteCmd()}
deleteCmd(),
}
}

func getCmd() *cobra.Command {
Expand All @@ -32,7 +33,7 @@ func getCmd() *cobra.Command {

msgID := args[0]

resp, err := c.Channel("", "").GetReactions(cmd.Context(), msgID, nil)
resp, err := c.GetReactions(cmd.Context(), msgID, nil)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/chat/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/GetStream/stream-cli/pkg/cmd/chat/file"
"github.com/GetStream/stream-cli/pkg/cmd/chat/imports"
"github.com/GetStream/stream-cli/pkg/cmd/chat/message"
"github.com/GetStream/stream-cli/pkg/cmd/chat/push"
"github.com/GetStream/stream-cli/pkg/cmd/chat/reaction"
"github.com/GetStream/stream-cli/pkg/cmd/chat/user"
"github.com/GetStream/stream-cli/pkg/cmd/chat/watch"
Expand All @@ -28,6 +29,7 @@ func NewRootCmd() *cobra.Command {
cmd.AddCommand(imports.NewCmds()...)
cmd.AddCommand(message.NewCmds()...)
cmd.AddCommand(user.NewCmds()...)
cmd.AddCommand(push.NewCmds()...)
cmd.AddCommand(reaction.NewCmds()...)
cmd.AddCommand(watch.NewCmds()...)

Expand Down

0 comments on commit ef11aa6

Please sign in to comment.