Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(push_notification): add pushprovider update and test #92

Merged
merged 4 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
148 changes: 148 additions & 0 deletions pkg/cmd/chat/push/push.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
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{
updateCmd(),
testCmd(),
}
}

func updateCmd() *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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For v2 we have:

  • firebase_notification_template
  • firebase_apn_template

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll need this PR to be merged first: GetStream/stream-chat-go#218

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added these 3

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can still provide firebase_apn_template and firebase_notification_template. why did you drop them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from where? it's there

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

himm, it wasn't there but it was a commit diff I guess. Looks fine


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 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