diff --git a/cmd/signaldctl/cmd/message/react/react-to-message.go b/cmd/signaldctl/cmd/message/react/react-to-message.go index d91c2f3..24ebb15 100644 --- a/cmd/signaldctl/cmd/message/react/react-to-message.go +++ b/cmd/signaldctl/cmd/message/react/react-to-message.go @@ -32,16 +32,25 @@ import ( ) var ( - account string - author v1.JsonAddress - timestamp int64 - emoji string - group string - remove bool + account string + threadAddress *v1.JsonAddress + group string + author v1.JsonAddress + timestamp int64 + emoji string + remove bool ReactMessageCmd = &cobra.Command{ - Use: "react ", + Use: "react thread author timestamp emoji", Short: "react to a message", + Long: `react to a message with a particular emoji + + arguments: + if the message being reacted to is in a 1-on-1 chat, the e164 or UUID of the person the chat is with. if the message is in a group, the group id. + the e164 or UUID of the author of the message being reacted to + the timestamp of the message to react to + the unicode emoji to send as the reaction + `, PreRun: func(cmd *cobra.Command, args []string) { if account == "" { account = config.Config.DefaultAccount @@ -50,19 +59,25 @@ var ( common.Must(cmd.Help()) log.Fatal("No account specified. Please specify with --account or set a default") } - if len(args) != 3 { + if len(args) != 4 { common.Must(cmd.Help()) + log.Fatal("not enough arguments") } + var err error - author, err = common.StringToAddress(args[0]) + threadAddress, group, err = common.StringToAddressOrGroup(args[0]) if err != nil { log.Fatal(err) } - timestamp, err = strconv.ParseInt(args[1], 10, 64) + author, err = common.StringToAddress(args[1]) if err != nil { - log.Fatal("Unable to parse timestamp", args[1], ":", err.Error()) + log.Fatal(err) } - emoji = args[2] + timestamp, err = strconv.ParseInt(args[2], 10, 64) + if err != nil { + log.Fatal("Unable to parse timestamp", args[2], ":", err.Error()) + } + emoji = args[3] }, Run: func(_ *cobra.Command, args []string) { go common.Signald.Listen(nil) @@ -77,10 +92,10 @@ var ( }, } - if group == "" { - req.RecipientAddress = &author + if threadAddress != nil { + req.RecipientAddress = threadAddress } else { - req.RecipientGroupID = group + req.RecipientAddress = &author } resp, err := req.Submit(common.Signald) diff --git a/cmd/signaldctl/common/signald.go b/cmd/signaldctl/common/signald.go index 3db14e8..de2cdc7 100644 --- a/cmd/signaldctl/common/signald.go +++ b/cmd/signaldctl/common/signald.go @@ -64,3 +64,18 @@ func StringToAddress(address string) (v1.JsonAddress, error) { } return v1.JsonAddress{}, NewInvalidAddress(address) } + +func StringToAddressOrGroup(identifier string) (*v1.JsonAddress, string, error) { + // if it starts with a +, assume it's an e164 + if strings.HasPrefix(identifier, "+") { + return &v1.JsonAddress{Number: identifier}, "", nil + } + + // if it parses as a UUID, assume it's a UUID + if _, err := uuid.Parse(identifier); err == nil { + return &v1.JsonAddress{UUID: identifier}, "", nil + } + + // if it doesn't start with a + and doesn't parse as a UUID, assume it's a group + return nil, identifier, nil +}