Add create group command, update react command

This commit is contained in:
Finn 2021-02-18 00:50:47 -08:00
parent 2e75854f8e
commit daf2a8be34
5 changed files with 140 additions and 23 deletions

View file

@ -0,0 +1,108 @@
// Copyright © 2021 Finn Herzfeld <finn@janky.solutions>
//
// 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 <http://www.gnu.org/licenses/>.
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 <group title> [<member number or UUID> [<member number or UUID>...]]",
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")
}

View file

@ -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)
}

View file

@ -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 <emoji>",
Use: "react <author> <timestamp> <emoji>",
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(&timestamp, "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")
}

View file

@ -30,7 +30,7 @@ var (
account string
ReadMessageCmd = &cobra.Command{
Use: "mark-read <author> <timestamp>",
Use: "mark-read <author> <timestamp> [<timestamp> [<timestamp>...]]",
Short: "mark a message as read",
PreRun: func(cmd *cobra.Command, args []string) {
if account == "" {

View file

@ -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}
}