From ce7f701ba6ba16baf8ff9d7dd2a7af58178ec844 Mon Sep 17 00:00:00 2001 From: Mark Mennell Date: Wed, 11 Nov 2026 14:52:46 +0800 Subject: [PATCH] fmsg edit: edit or revert the body of a sent message (FMSG-008) Co-Authored-By: Claude Fable 5.2 --- README.md | 5 +++ cmd/edit.go | 83 +++++++++++++++++++++++++++++++++++++ internal/api/client.go | 42 +++++++++++++++++++ internal/api/client_test.go | 39 +++++++++++++++++ 4 files changed, 169 insertions(+) create mode 100644 cmd/edit.go diff --git a/README.md b/README.md index 277dfb0..821ef1d 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,7 @@ Programmatic clients use API keys issued by fmsg-webapi. | `fmsg update [file\|text\|-]` | Update a draft message | | `fmsg del ` | Delete a draft message by ID | | `fmsg add-to [recipient...]` | Add additional recipients to a message | +| `fmsg edit ` | Replace the body of a message you sent (FMSG-008 edit); `fmsg edit --revert` restores the original | | `fmsg attach ` | Upload a file attachment to a message | | `fmsg get-attach ` | Download an attachment | | `fmsg get-data [output-file]` | Download message body data (stdout if no output file) | @@ -221,6 +222,10 @@ fmsg draft send 42 fmsg add-to 101 @other@example.com fmsg add-to 101 @cc1@example.com @cc2@example.com +# Edit a message you already sent, then revert it +fmsg edit 101 "See you there at 5pm" +fmsg edit 101 --revert + # Update a draft message fmsg update 42 --topic "New topic" fmsg update 42 --to @newrecipient@example.com "Updated body text" diff --git a/cmd/edit.go b/cmd/edit.go new file mode 100644 index 0000000..eb98e8e --- /dev/null +++ b/cmd/edit.go @@ -0,0 +1,83 @@ +package cmd + +import ( + "errors" + "fmt" + "io" + "os" + + "github.com/spf13/cobra" +) + +var editRevert bool + +var editCmd = &cobra.Command{ + Use: "edit [file|text|-]", + Short: "Edit the body of a message you sent, or revert it to the original", + Long: `Edit the body of a message you sent (FMSG-008). + +The message content replaces the whole body and can be: + - A path to a file (the file contents are used) + - A text string (used directly) + - "-" to read from stdin + +Use --revert instead of content to show the message as first sent. +Participants see the new text marked as edited; the original stays stored +and visible to them. Drafts are changed with "fmsg update" instead.`, + Args: cobra.RangeArgs(1, 2), + RunE: func(cmd *cobra.Command, args []string) error { + if editRevert == (len(args) == 2) { + return errors.New("give the new content, or --revert, but not both") + } + + var data *string + if !editRevert { + var raw []byte + var err error + switch content := args[1]; content { + case "-": + raw, err = io.ReadAll(os.Stdin) + if err != nil { + return fmt.Errorf("reading stdin: %w", err) + } + default: + // Try as file first; fall back to using content directly as text. + if fileData, ferr := os.ReadFile(content); ferr == nil { + raw = fileData + } else { + raw = []byte(content) + } + } + text := string(raw) + data = &text + } + + client, _ := newAuthenticatedClient() + msgID, err := resolveMessageID(client, args[0]) + if err != nil { + return err + } + result, err := client.EditMessage(msgID, data) + if err != nil { + return err + } + + if jsonOutput { + return printJSON(result) + } + switch { + case !result.Sent: + fmt.Printf("Message %d already reads that way; nothing sent\n", msgID) + case editRevert: + fmt.Printf("Reverted message %d to its original body\n", msgID) + default: + fmt.Printf("Edited message %d\n", msgID) + } + return nil + }, +} + +func init() { + editCmd.Flags().BoolVar(&editRevert, "revert", false, "revert the message to its original body") + rootCmd.AddCommand(editCmd) +} diff --git a/internal/api/client.go b/internal/api/client.go index e649fd3..2dc8ef1 100644 --- a/internal/api/client.go +++ b/internal/api/client.go @@ -420,6 +420,48 @@ func (c *Client) AddRecipients(id int64, addTo []string) (*AddRecipientsResponse return &result, nil } +// EditMessageResponse identifies the edit message in force on a message after +// an edit call; ID, Time and SHA256 are nil when the message has none. Sent is +// false when the body already read that way and nothing was sent. +type EditMessageResponse struct { + ID *int64 `json:"id"` + Time *float64 `json:"time"` + SHA256 *string `json:"sha256"` + Sent bool `json:"sent"` +} + +// EditMessage replaces the body of a sent message the caller authored +// (FMSG-008), or reverts it to the original when data is nil. +func (c *Client) EditMessage(id int64, data *string) (*EditMessageResponse, error) { + body, err := json.Marshal(map[string]any{"data": data}) + if err != nil { + return nil, fmt.Errorf("encoding request: %w", err) + } + + req, err := http.NewRequest(http.MethodPost, c.BaseURL+"/fmsg/"+strconv.FormatInt(id, 10)+"/edit", bytes.NewReader(body)) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", "application/json") + + resp, err := c.do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if err := checkStatus(resp); err != nil { + return nil, err + } + + var result EditMessageResponse + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { + return nil, fmt.Errorf("decoding response: %w", err) + } + result.Sent = resp.StatusCode == http.StatusCreated + return &result, nil +} + // DeleteMessage deletes a message by ID. func (c *Client) DeleteMessage(id int64) error { req, err := http.NewRequest(http.MethodDelete, c.BaseURL+"/fmsg/"+strconv.FormatInt(id, 10), nil) diff --git a/internal/api/client_test.go b/internal/api/client_test.go index 7bc0edb..8682799 100644 --- a/internal/api/client_test.go +++ b/internal/api/client_test.go @@ -159,3 +159,42 @@ func TestUploadAttachmentReturnsServerResponse(t *testing.T) { t.Fatalf("upload response: got %+v", result) } } + +func TestEditMessageSendsDataAndReportsWhetherSent(t *testing.T) { + var bodies []string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/fmsg/7/edit" || r.Method != http.MethodPost { + t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path) + } + body, _ := io.ReadAll(r.Body) + bodies = append(bodies, string(body)) + w.Header().Set("Content-Type", "application/json") + if len(bodies) == 1 { + w.WriteHeader(http.StatusCreated) + _, _ = w.Write([]byte(`{"id":9,"time":12.5,"sha256":"ab"}`)) + return + } + _, _ = w.Write([]byte(`{"id":null,"time":null,"sha256":null}`)) + })) + defer srv.Close() + + client := New(srv.URL, &sequenceTokenProvider{}) + text := "fixed" + edited, err := client.EditMessage(7, &text) + if err != nil { + t.Fatal(err) + } + if !edited.Sent || edited.ID == nil || *edited.ID != 9 { + t.Fatalf("edit result = %+v", edited) + } + reverted, err := client.EditMessage(7, nil) + if err != nil { + t.Fatal(err) + } + if reverted.Sent || reverted.ID != nil { + t.Fatalf("no-op revert result = %+v", reverted) + } + if bodies[0] != `{"data":"fixed"}` || bodies[1] != `{"data":null}` { + t.Fatalf("request bodies = %q", bodies) + } +}