-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathreply.go
More file actions
144 lines (127 loc) · 3.79 KB
/
reply.go
File metadata and controls
144 lines (127 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package cmd
import (
"fmt"
"strconv"
"github.com/spf13/cobra"
"github.com/basecamp/hey-cli/internal/editor"
"github.com/basecamp/hey-cli/internal/htmlutil"
"github.com/basecamp/hey-cli/internal/output"
)
type replyCommand struct {
cmd *cobra.Command
message string
from string
}
func newReplyCommand() *replyCommand {
replyCommand := &replyCommand{}
replyCommand.cmd = &cobra.Command{
Use: "reply <thread-id>",
Short: "Reply to a thread",
Annotations: map[string]string{
"agent_notes": "Replies to the latest entry in a thread. Accepts message via -m, stdin, or $EDITOR.",
},
Example: ` hey reply 12345 -m "Thanks!"
echo "Detailed reply" | hey reply 12345`,
RunE: replyCommand.run,
Args: usageExactOneArg(),
}
replyCommand.cmd.Flags().StringVarP(&replyCommand.message, "message", "m", "", "Reply message (or opens $EDITOR)")
replyCommand.cmd.Flags().StringVar(&replyCommand.from, "from", "", "Sender email address (overrides default)")
return replyCommand
}
func (c *replyCommand) run(cmd *cobra.Command, args []string) error {
if err := requireAuth(); err != nil {
return err
}
threadID, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
return output.ErrUsage(fmt.Sprintf("invalid thread ID: %s", args[0]))
}
ctx := cmd.Context()
// Fetch topic page to extract recipients (To/CC/BCC).
topicResp, err := sdk.GetHTML(ctx, fmt.Sprintf("/topics/%d", threadID))
if err != nil {
return convertSDKError(err)
}
addressed := htmlutil.ParseTopicAddressed(string(topicResp.Data))
if len(addressed.To) == 0 && len(addressed.CC) == 0 && len(addressed.BCC) == 0 {
return output.ErrUsage("could not determine thread recipients")
}
// Fetch entries to find the latest entry ID for the reply.
entriesResp, err := sdk.GetHTML(ctx, fmt.Sprintf("/topics/%d/entries", threadID))
if err != nil {
return convertSDKError(err)
}
entries := htmlutil.ParseTopicEntriesHTML(string(entriesResp.Data))
if len(entries) == 0 {
return output.ErrNotFound("entries for thread", args[0])
}
latestEntryID := entries[len(entries)-1].ID
message := c.message
if message == "" {
if !stdinIsTerminal() {
message, err = readStdin()
if err != nil {
return err
}
if message == "" {
return output.ErrUsage("no message provided (use -m or --message to provide inline, or pipe to stdin)")
}
} else {
message, err = editor.Open("")
if err != nil {
return output.ErrAPI(0, fmt.Sprintf("could not open editor: %v", err))
}
if message == "" {
return output.ErrUsage("empty message, aborting")
}
}
}
hasSenderOverride := c.from != "" || cfg.DefaultSender != ""
if hasSenderOverride {
senderID, err := effectiveSenderID(ctx, c.from)
if err != nil {
return err
}
body := map[string]any{
"acting_sender_id": senderID,
"message": map[string]any{
"content": message,
},
}
addrMap := map[string]any{}
if len(addressed.To) > 0 {
addrMap["directly"] = addressed.To
}
if len(addressed.CC) > 0 {
addrMap["copied"] = addressed.CC
}
if len(addressed.BCC) > 0 {
addrMap["blindcopied"] = addressed.BCC
}
if len(addrMap) > 0 {
body["entry"] = map[string]any{
"addressed": addrMap,
}
}
if _, err := sdk.PostMutation(ctx, fmt.Sprintf("/entries/%d/replies.json", latestEntryID), body); err != nil {
return convertSDKError(err)
}
} else {
if err = sdk.Entries().CreateReply(ctx, latestEntryID, message, addressed.To, addressed.CC, addressed.BCC); err != nil {
return convertSDKError(err)
}
}
if writer.IsStyled() {
fmt.Fprintln(cmd.OutOrStdout(), "Reply sent.")
return nil
}
return writeOK(nil,
output.WithSummary("Reply sent"),
output.WithBreadcrumbs(output.Breadcrumb{
Action: "view",
Command: fmt.Sprintf("hey threads %d", threadID),
Description: "View the full thread",
}),
)
}