From daf2a8be34b1f99ccddf5a51e8a6eaf07f227d9c Mon Sep 17 00:00:00 2001 From: Finn Date: Thu, 18 Feb 2021 00:50:47 -0800 Subject: [PATCH] Add create group command, update react command --- .../cmd/group/create/create-group.go | 108 ++++++++++++++++++ cmd/signaldctl/cmd/group/root.go | 4 +- .../cmd/message/react/react-to-message.go | 40 +++---- .../cmd/message/read/mark-message-as-read.go | 2 +- cmd/signaldctl/common/signald.go | 9 ++ 5 files changed, 140 insertions(+), 23 deletions(-) create mode 100644 cmd/signaldctl/cmd/group/create/create-group.go diff --git a/cmd/signaldctl/cmd/group/create/create-group.go b/cmd/signaldctl/cmd/group/create/create-group.go new file mode 100644 index 0000000..011cb92 --- /dev/null +++ b/cmd/signaldctl/cmd/group/create/create-group.go @@ -0,0 +1,108 @@ +// Copyright © 2021 Finn Herzfeld +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package create + +import ( + "encoding/json" + "log" + "os" + + "github.com/jedib0t/go-pretty/v6/table" + "github.com/spf13/cobra" + "gopkg.in/yaml.v2" + + "gitlab.com/signald/signald-go/cmd/signaldctl/common" + "gitlab.com/signald/signald-go/cmd/signaldctl/config" + "gitlab.com/signald/signald-go/signald/client-protocol/v1" +) + +var ( + account string + adminMembers bool + timer int64 + avatar string + CreateGroupCmd = &cobra.Command{ + Use: "create [ [...]]", + Short: "create a group", + PreRun: func(cmd *cobra.Command, args []string) { + if account == "" { + account = config.Config.DefaultAccount + } + if account == "" { + log.Fatal("No account specified. Please specify with --account or set a default") + } + if len(args) == 0 { + common.Must(cmd.Help()) + log.Fatal("must at least specify a group title") + } + }, + Run: func(_ *cobra.Command, args []string) { + go common.Signald.Listen(nil) + req := v1.CreateGroupRequest{ + Account: account, + Title: args[0], + } + + for _, member := range args[1:] { + address := common.StringToAddress(member) + req.Members = append(req.Members, &address) + } + + if adminMembers { + req.MemberRole = "ADMINISTRATOR" + } + + resp, err := req.Submit(common.Signald) + if err != nil { + log.Fatal(err, "error communicating with signald") + } + switch common.OutputFormat { + case common.OutputFormatJSON: + err := json.NewEncoder(os.Stdout).Encode(resp) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatYAML: + err := yaml.NewEncoder(os.Stdout).Encode(resp) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault: + t := table.NewWriter() + t.SetOutputMirror(os.Stdout) + t.AppendHeader(table.Row{"ID", "Title", "Members"}) + + t.AppendRow(table.Row{resp.ID, resp.Title, len(resp.Members)}) + + if common.OutputFormat == common.OutputFormatCSV { + t.RenderCSV() + } else { + common.StylizeTable(t) + t.Render() + } + default: + log.Fatal("Unsupported output format") + } + }, + } +) + +func init() { + CreateGroupCmd.Flags().StringVarP(&account, "account", "a", "", "the signald account to use") + CreateGroupCmd.Flags().BoolVar(&adminMembers, "admin-members", false, "grants all members of the new group administrator permissions. otherwise only the group creator will be a group administrator.") + CreateGroupCmd.Flags().Int64VarP(&timer, "timer", "t", 0, "sets the disappearing message timer of the new group") + CreateGroupCmd.Flags().StringVar(&avatar, "avatar", "", "path to the image to use as the group avatar") +} diff --git a/cmd/signaldctl/cmd/group/root.go b/cmd/signaldctl/cmd/group/root.go index 2c514af..adc4ce5 100644 --- a/cmd/signaldctl/cmd/group/root.go +++ b/cmd/signaldctl/cmd/group/root.go @@ -19,6 +19,7 @@ import ( "github.com/spf13/cobra" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/accept" + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/create" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/leave" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/list" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/show" @@ -31,7 +32,8 @@ var GroupCmd = &cobra.Command{ func init() { GroupCmd.AddCommand(accept.AcceptGroupInvitationCmd) + GroupCmd.AddCommand(create.CreateGroupCmd) + GroupCmd.AddCommand(leave.LeaveGroupCmd) GroupCmd.AddCommand(list.ListGroupCmd) GroupCmd.AddCommand(show.ShowGroupCmd) - GroupCmd.AddCommand(leave.LeaveGroupCmd) } diff --git a/cmd/signaldctl/cmd/message/react/react-to-message.go b/cmd/signaldctl/cmd/message/react/react-to-message.go index 9abc9e9..c795da1 100644 --- a/cmd/signaldctl/cmd/message/react/react-to-message.go +++ b/cmd/signaldctl/cmd/message/react/react-to-message.go @@ -20,7 +20,7 @@ import ( "fmt" "log" "os" - "strings" + "strconv" "github.com/jedib0t/go-pretty/v6/table" "github.com/spf13/cobra" @@ -33,13 +33,14 @@ import ( var ( account string - author string + author v1.JsonAddress timestamp int64 - to string + emoji string + group string remove bool ReactMessageCmd = &cobra.Command{ - Use: "react ", + Use: "react ", Short: "react to a message", PreRun: func(cmd *cobra.Command, args []string) { if account == "" { @@ -48,10 +49,16 @@ var ( if account == "" { log.Fatal("No account specified. Please specify with --account or set a default") } - if len(args) != 1 { + if len(args) != 3 { common.Must(cmd.Help()) - log.Fatal("must provide exactly one reaction") } + author = common.StringToAddress(args[0]) + var err error + timestamp, err = strconv.ParseInt(args[1], 10, 64) + if err != nil { + log.Fatal("Unable to parse timestamp", args[1], ":", err.Error()) + } + emoji = args[2] }, Run: func(_ *cobra.Command, args []string) { go common.Signald.Listen(nil) @@ -59,17 +66,17 @@ var ( req := v1.ReactRequest{ Username: account, Reaction: &v1.JsonReaction{ - Emoji: args[0], + Emoji: emoji, Remove: remove, - TargetAuthor: &v1.JsonAddress{Number: author}, + TargetAuthor: &author, TargetSentTimestamp: timestamp, }, } - if strings.HasPrefix(to, "+") { - req.RecipientAddress = &v1.JsonAddress{Number: to} + if group == "" { + req.RecipientAddress = &author } else { - req.RecipientGroupID = to + req.RecipientGroupID = group } resp, err := req.Submit(common.Signald) @@ -129,15 +136,6 @@ var ( func init() { ReactMessageCmd.Flags().StringVarP(&account, "account", "a", "", "local account to use") - - ReactMessageCmd.Flags().StringVarP(&to, "to", "t", "", "phone number or group ID that the message was sent to") - common.Must(ReactMessageCmd.MarkFlagRequired("to")) - - ReactMessageCmd.Flags().Int64VarP(×tamp, "timestamp", "w", 0, "the timestamp of the message being reacted to") - common.Must(ReactMessageCmd.MarkFlagRequired("timestamp")) - - ReactMessageCmd.Flags().StringVar(&author, "author", "", "the phone number of the author of the message being reacted to") - common.Must(ReactMessageCmd.MarkFlagRequired("author")) - + ReactMessageCmd.Flags().StringVarP(&group, "group", "g", "", "the group ID of the group the original message was sent to, if it was a group message") ReactMessageCmd.Flags().BoolVarP(&remove, "remove", "r", false, "remove a reaction that was previously set") } diff --git a/cmd/signaldctl/cmd/message/read/mark-message-as-read.go b/cmd/signaldctl/cmd/message/read/mark-message-as-read.go index f18670c..f3ac826 100644 --- a/cmd/signaldctl/cmd/message/read/mark-message-as-read.go +++ b/cmd/signaldctl/cmd/message/read/mark-message-as-read.go @@ -30,7 +30,7 @@ var ( account string ReadMessageCmd = &cobra.Command{ - Use: "mark-read ", + Use: "mark-read [ [...]]", Short: "mark a message as read", PreRun: func(cmd *cobra.Command, args []string) { if account == "" { diff --git a/cmd/signaldctl/common/signald.go b/cmd/signaldctl/common/signald.go index 877b433..f47b075 100644 --- a/cmd/signaldctl/common/signald.go +++ b/cmd/signaldctl/common/signald.go @@ -2,10 +2,12 @@ package common import ( "log" + "strings" "github.com/jedib0t/go-pretty/v6/table" "gitlab.com/signald/signald-go/signald" + "gitlab.com/signald/signald-go/signald/client-protocol/v1" ) const ( @@ -36,3 +38,10 @@ func Must(err error) { func StylizeTable(t table.Writer) { t.SetStyle(table.StyleLight) } + +func StringToAddress(address string) v1.JsonAddress { + if strings.HasPrefix(address, "+") { + return v1.JsonAddress{Number: address} + } + return v1.JsonAddress{UUID: address} +}