Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
42 changes: 42 additions & 0 deletions docs/stream-cli_chat_update-message.md
Original file line number Diff line number Diff line change
@@ -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

51 changes: 51 additions & 0 deletions pkg/cmd/chat/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func NewCmds() []*cobra.Command {
deleteCmd(),
flagCmd(),
translateCmd(),
updateMessageCmd(),
}
}

Expand Down Expand Up @@ -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
}
49 changes: 49 additions & 0 deletions pkg/cmd/chat/message/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package message

import (
"bytes"
"context"
"os"
"regexp"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -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)
}