diff --git a/docs/stream-cli_chat.md b/docs/stream-cli_chat.md index 37b6e7e..7157f57 100644 --- a/docs/stream-cli_chat.md +++ b/docs/stream-cli_chat.md @@ -70,6 +70,7 @@ Allows you to interact with your Chat applications * [stream-cli chat update-channel](stream-cli_chat_update-channel.md) - Update a channel * [stream-cli chat update-channel-partial](stream-cli_chat_update-channel-partial.md) - Update a channel partially * [stream-cli chat update-channel-type](stream-cli_chat_update-channel-type.md) - Update channel type +* [stream-cli chat update-message](stream-cli_chat_update-message.md) - Update an existing message * [stream-cli chat update-message-partial](stream-cli_chat_update-message-partial.md) - Partially update a message * [stream-cli chat update-user-partial](stream-cli_chat_update-user-partial.md) - Partially update a user * [stream-cli chat upload-file](stream-cli_chat_upload-file.md) - Upload a file diff --git a/docs/stream-cli_chat_update-message.md b/docs/stream-cli_chat_update-message.md new file mode 100644 index 0000000..17a4d38 --- /dev/null +++ b/docs/stream-cli_chat_update-message.md @@ -0,0 +1,42 @@ +## stream-cli chat update-message + +Update an existing message + +### Synopsis + +Update a message by providing the message ID, user ID, and new message text. +This fully overwrites the message content while preserving metadata. + + +``` +stream-cli chat update-message --message-id [id] --user [user-id] --text [text] [flags] +``` + +### Examples + +``` +# Update a message by ID +$ stream-cli chat update-message --message-id msgid-123 --user user123 --text "Updated message text" + +``` + +### Options + +``` + -h, --help help for update-message + --message-id string [required] ID of the message to update + --text string [required] New message text + --user string [required] User ID performing the update +``` + +### Options inherited from parent commands + +``` + --app string [optional] Application name to use as it's defined in the configuration file + --config string [optional] Explicit config file path +``` + +### SEE ALSO + +* [stream-cli chat](stream-cli_chat.md) - Allows you to interact with your Chat applications + diff --git a/docs/stream-cli_chat_upload-import.md b/docs/stream-cli_chat_upload-import.md index ca75e02..76bdab2 100644 --- a/docs/stream-cli_chat_upload-import.md +++ b/docs/stream-cli_chat_upload-import.md @@ -20,9 +20,10 @@ $ stream-cli chat upload-import data.json --mode insert --output-format tree ### Options ``` - -h, --help help for upload-import - -m, --mode string [optional] Import mode. Canbe upsert or insert (default "upsert") - -o, --output-format string [optional] Output format. Can be json or tree (default "json") + -h, --help help for upload-import + --lighter-validation-id [optional] allows to pass ! in channel ID + -m, --mode string [optional] Import mode. Canbe upsert or insert (default "upsert") + -o, --output-format string [optional] Output format. Can be json or tree (default "json") ``` ### Options inherited from parent commands diff --git a/docs/stream-cli_chat_upsert-pushprovider.md b/docs/stream-cli_chat_upsert-pushprovider.md index 523784c..ec9f937 100644 --- a/docs/stream-cli_chat_upsert-pushprovider.md +++ b/docs/stream-cli_chat_upsert-pushprovider.md @@ -14,7 +14,7 @@ Create or updates a push provider description disabled_at disabled_reason - + apn_auth_key apn_key_id apn_team_id @@ -23,10 +23,10 @@ Create or updates a push provider firebase_notification_template firebase_apn_template firebase_credentials - + huawei_app_id huawei_app_secret - + xiaomi_package_name xiaomi_app_secret diff --git a/docs/stream-cli_chat_validate-import.md b/docs/stream-cli_chat_validate-import.md index a8e995a..26c23b2 100644 --- a/docs/stream-cli_chat_validate-import.md +++ b/docs/stream-cli_chat_validate-import.md @@ -17,8 +17,9 @@ $ stream-cli chat validate-import data.json ### Options ``` - -h, --help help for validate-import - -o, --output-format string [optional] Output format. Can be json or tree (default "json") + -h, --help help for validate-import + --lighter-validation-id [optional] allows to pass ! in channel ID + -o, --output-format string [optional] Output format. Can be json or tree (default "json") ``` ### Options inherited from parent commands diff --git a/pkg/cmd/chat/message/message.go b/pkg/cmd/chat/message/message.go index e509431..73b177b 100644 --- a/pkg/cmd/chat/message/message.go +++ b/pkg/cmd/chat/message/message.go @@ -21,6 +21,7 @@ func NewCmds() []*cobra.Command { deleteCmd(), flagCmd(), translateCmd(), + updateMessageCmd(), } } @@ -333,3 +334,53 @@ func translateCmd() *cobra.Command { return cmd } + +func updateMessageCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "update-message --message-id [id] --user [user-id] --text [text]", + Short: "Update an existing message", + Long: heredoc.Doc(` + Update a message by providing the message ID, user ID, and new message text. + This fully overwrites the message content while preserving metadata. + `), + Example: heredoc.Doc(` + # Update a message by ID + $ stream-cli chat update-message --message-id msgid-123 --user user123 --text "Updated message text" + `), + 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") + userID, _ := cmd.Flags().GetString("user") + text, _ := cmd.Flags().GetString("text") + + updatedMsg := &stream.Message{ + ID: msgID, + Text: text, + User: &stream.User{ID: userID}, + } + + _, err = c.UpdateMessage(cmd.Context(), updatedMsg, msgID) + if err != nil { + return err + } + + cmd.Println("Successfully updated message.") + return nil + }, + } + + fl := cmd.Flags() + fl.String("message-id", "", "[required] ID of the message to update") + fl.String("user", "", "[required] User ID performing the update") + fl.String("text", "", "[required] New message text") + + _ = cmd.MarkFlagRequired("message-id") + _ = cmd.MarkFlagRequired("user") + _ = cmd.MarkFlagRequired("text") + + return cmd +} diff --git a/pkg/cmd/chat/message/message_test.go b/pkg/cmd/chat/message/message_test.go index bf1c514..2daff7e 100644 --- a/pkg/cmd/chat/message/message_test.go +++ b/pkg/cmd/chat/message/message_test.go @@ -2,7 +2,9 @@ package message import ( "bytes" + "context" "os" + "regexp" "testing" "github.com/stretchr/testify/require" @@ -153,3 +155,50 @@ func TestTranslateMessage(t *testing.T) { require.NoError(t, err) require.Contains(t, cmd.OutOrStdout().(*bytes.Buffer).String(), "szia") } + +func TestUpdateMessage(t *testing.T) { + cmd := test.GetRootCmdWithSubCommands(NewCmds()...) + ch := test.InitChannel(t) + u := test.CreateUser() + + t.Cleanup(func() { + test.DeleteChannel(ch) + test.DeleteUser(u) + }) + + // Step 1: Send original message + cmd.SetArgs([]string{ + "send-message", + "-t", "messaging", + "-i", ch, + "-u", u, + "--text", "Original message", + }) + _, err := cmd.ExecuteC() + require.NoError(t, err) + + // Extract message ID from stdout + out := cmd.OutOrStdout().(*bytes.Buffer).String() + re := regexp.MustCompile(`Message id: \[(.+?)]`) + matches := re.FindStringSubmatch(out) + require.Len(t, matches, 2) + msgID := matches[1] + + // Step 2: Update the message + cmd.SetArgs([]string{ + "update-message", + "--message-id", msgID, + "--user", u, + "--text", "Updated message text", + }) + _, err = cmd.ExecuteC() + require.NoError(t, err) + require.Contains(t, cmd.OutOrStdout().(*bytes.Buffer).String(), "Successfully updated message.") + + // Step 3: Fetch and verify the update + client := test.InitClient() + ctx := context.Background() + resp, err := client.GetMessage(ctx, msgID) + require.NoError(t, err) + require.Equal(t, "Updated message text", resp.Message.Text) +}