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
This commit is contained in:
Finn 2021-10-17 15:01:20 -07:00
parent a535a63ba0
commit 28b7d16568
2 changed files with 45 additions and 15 deletions

View file

@ -33,15 +33,24 @@ import (
var ( var (
account string account string
threadAddress *v1.JsonAddress
group string
author v1.JsonAddress author v1.JsonAddress
timestamp int64 timestamp int64
emoji string emoji string
group string
remove bool remove bool
ReactMessageCmd = &cobra.Command{ ReactMessageCmd = &cobra.Command{
Use: "react <author> <timestamp> <emoji>", Use: "react thread author timestamp emoji",
Short: "react to a message", Short: "react to a message",
Long: `react to a message with a particular emoji
arguments:
<thread> 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.
<author> the e164 or UUID of the author of the message being reacted to
<timestamp> the timestamp of the message to react to
<emoji> the unicode emoji to send as the reaction
`,
PreRun: func(cmd *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
if account == "" { if account == "" {
account = config.Config.DefaultAccount account = config.Config.DefaultAccount
@ -50,19 +59,25 @@ var (
common.Must(cmd.Help()) common.Must(cmd.Help())
log.Fatal("No account specified. Please specify with --account or set a default") 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()) common.Must(cmd.Help())
log.Fatal("not enough arguments")
} }
var err error var err error
author, err = common.StringToAddress(args[0]) threadAddress, group, err = common.StringToAddressOrGroup(args[0])
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
timestamp, err = strconv.ParseInt(args[1], 10, 64) author, err = common.StringToAddress(args[1])
if err != nil { 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) { Run: func(_ *cobra.Command, args []string) {
go common.Signald.Listen(nil) go common.Signald.Listen(nil)
@ -77,10 +92,10 @@ var (
}, },
} }
if group == "" { if threadAddress != nil {
req.RecipientAddress = &author req.RecipientAddress = threadAddress
} else { } else {
req.RecipientGroupID = group req.RecipientAddress = &author
} }
resp, err := req.Submit(common.Signald) resp, err := req.Submit(common.Signald)

View file

@ -64,3 +64,18 @@ func StringToAddress(address string) (v1.JsonAddress, error) {
} }
return v1.JsonAddress{}, NewInvalidAddress(address) 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
}