From 28b7d16568ca457aa80fa329a00201afd1b66c3c Mon Sep 17 00:00:00 2001 From: Finn Date: Sun, 17 Oct 2021 15:01:20 -0700 Subject: [PATCH] Fix sending reactions previous implementation had serious limitations. I realize the docs are awkward, i'll look up how to use cobra one of these days --- .../cmd/message/react/react-to-message.go | 45 ++++++++++++------- cmd/signaldctl/common/signald.go | 15 +++++++ 2 files changed, 45 insertions(+), 15 deletions(-) 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 +}