diff --git a/cmd/signaldctl/cmd/message/react/react-to-message.go b/cmd/signaldctl/cmd/message/react/react-to-message.go new file mode 100644 index 0000000..393c62b --- /dev/null +++ b/cmd/signaldctl/cmd/message/react/react-to-message.go @@ -0,0 +1,142 @@ +// 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 react + +import ( + "encoding/json" + "fmt" + "log" + "os" + "strings" + + "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 + author string + timestamp int64 + to string + remove bool + + ReactMessageCmd = &cobra.Command{ + Use: "react", + Short: "react to a message", + PreRun: func(_ *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) != 1 { + log.Fatal("must provide exactly one reaction") + } + }, + Run: func(_ *cobra.Command, args []string) { + go common.Signald.Listen(nil) + + req := v1.ReactRequest{ + Username: account, + Reaction: &v1.JsonReaction{ + Emoji: args[0], + Remove: remove, + TargetAuthor: &v1.JsonAddress{Number: author}, + TargetSentTimestamp: timestamp, + }, + } + + if strings.HasPrefix(to, "+") { + req.RecipientAddress = &v1.JsonAddress{Number: to} + } else { + req.RecipientGroupID = to + } + + resp, err := req.Submit(common.Signald) + if err != nil { + log.Fatal("error sending request to signald: ", err) + } + 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{"Number", "UUID", "Duration", "Send Error"}) + for _, result := range resp.Results { + if result.Success != nil { + t.AppendRow(table.Row{ + result.Address.Number, + result.Address.UUID, + fmt.Sprintf("%dms", result.Success.Duration), + "", + }) + } else { + var sendError string + if result.IdentityFailure != "" { + sendError = fmt.Sprintf("identity failure: %s\n", result.IdentityFailure) + } + if result.NetworkFailure { + sendError = "network failure" + } + if result.UnregisteredFailure { + sendError = "not on" + } + t.AppendRow(table.Row{result.Address.Number, result.Address.UUID, "", sendError}) + } + } + + if common.OutputFormat == common.OutputFormatCSV { + t.RenderCSV() + } else { + t.SetStyle(table.StyleLight) + t.Render() + } + default: + log.Fatal("Unsupported output format") + } + }, + } +) + +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().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 new file mode 100644 index 0000000..53d0204 --- /dev/null +++ b/cmd/signaldctl/cmd/message/read/mark-message-as-read.go @@ -0,0 +1,75 @@ +// 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 read + +import ( + "log" + "strconv" + + "github.com/spf13/cobra" + + "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 + to string + + ReadMessageCmd = &cobra.Command{ + Use: "mark-read", + Short: "mark a message as read", + PreRun: func(_ *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) < 2 { + log.Fatal("Usage: signaldctl message mark-read ") + } + }, + Run: func(_ *cobra.Command, args []string) { + go common.Signald.Listen(nil) + + timestamps := []int64{} + for _, arg := range args[1:] { + ts, err := strconv.ParseInt(arg, 10, 64) + if err != nil { + log.Fatal("unable to parse timestamp: ", arg) + } + timestamps = append(timestamps, ts) + } + + req := v1.MarkReadRequest{ + Account: account, + Timestamps: timestamps, + To: &v1.JsonAddress{Number: args[0]}, + } + + err := req.Submit(common.Signald) + if err != nil { + log.Fatal("error sending request to signald: ", err) + } + }, + } +) + +func init() { + ReadMessageCmd.Flags().StringVarP(&account, "account", "a", "", "local account to use") +} diff --git a/cmd/signaldctl/cmd/message/root.go b/cmd/signaldctl/cmd/message/root.go index d121601..5a3048b 100644 --- a/cmd/signaldctl/cmd/message/root.go +++ b/cmd/signaldctl/cmd/message/root.go @@ -18,6 +18,8 @@ package message import ( "github.com/spf13/cobra" + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/message/react" + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/message/read" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/message/send" ) @@ -28,4 +30,6 @@ var MessageCmd = &cobra.Command{ func init() { MessageCmd.AddCommand(send.SendMessageCmd) + MessageCmd.AddCommand(react.ReactMessageCmd) + MessageCmd.AddCommand(read.ReadMessageCmd) } diff --git a/signald/client-protocol/v1/requests.go b/signald/client-protocol/v1/requests.go index d1935a8..f9d19cb 100644 --- a/signald/client-protocol/v1/requests.go +++ b/signald/client-protocol/v1/requests.go @@ -17,11 +17,10 @@ func (r *AcceptInvitationRequest) Submit(conn *signald.Signald) (response JsonGr if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -32,11 +31,12 @@ func (r *AcceptInvitationRequest) Submit(conn *signald.Signald) (response JsonGr err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil @@ -50,11 +50,10 @@ func (r *ApproveMembershipRequest) Submit(conn *signald.Signald) (response JsonG if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -65,11 +64,12 @@ func (r *ApproveMembershipRequest) Submit(conn *signald.Signald) (response JsonG err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil @@ -83,11 +83,10 @@ func (r *GetGroupRequest) Submit(conn *signald.Signald) (response JsonGroupV2Inf if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -98,11 +97,12 @@ func (r *GetGroupRequest) Submit(conn *signald.Signald) (response JsonGroupV2Inf err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil @@ -116,11 +116,10 @@ func (r *GetLinkedDevicesRequest) Submit(conn *signald.Signald) (response Linked if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -131,11 +130,12 @@ func (r *GetLinkedDevicesRequest) Submit(conn *signald.Signald) (response Linked err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil @@ -149,11 +149,10 @@ func (r *GetProfileRequest) Submit(conn *signald.Signald) (response Profile, err if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -164,11 +163,12 @@ func (r *GetProfileRequest) Submit(conn *signald.Signald) (response Profile, err err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil @@ -182,11 +182,10 @@ func (r *JoinGroupRequest) Submit(conn *signald.Signald) (response JsonGroupJoin if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -197,11 +196,12 @@ func (r *JoinGroupRequest) Submit(conn *signald.Signald) (response JsonGroupJoin err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil @@ -214,11 +214,10 @@ func (r *ListContactsRequest) Submit(conn *signald.Signald) (response ProfileLis if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -229,11 +228,12 @@ func (r *ListContactsRequest) Submit(conn *signald.Signald) (response ProfileLis err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil @@ -246,11 +246,10 @@ func (r *ListGroupsRequest) Submit(conn *signald.Signald) (response GroupList, e if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -261,36 +260,65 @@ func (r *ListGroupsRequest) Submit(conn *signald.Signald) (response GroupList, e err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil } -func (r *MarkReadRequest) Submit(conn *signald.Signald) error { +func (r *MarkReadRequest) Submit(conn *signald.Signald) (err error) { r.Version = "v1" r.Type = "mark_read" if r.ID == "" { r.ID = signald.GenerateID() } + err = conn.RawRequest(r) + if err != nil { + log.Println("signald-go: error submitting request to signald") + return + } - return conn.RawRequest(r) + responseChannel := conn.GetResponseListener(r.ID) + defer conn.CloseResponseListener(r.ID) + + rawResponse := <-responseChannel + if rawResponse.Error != nil { + err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) + return + } + + return err } -func (r *ProtocolRequest) Submit(conn *signald.Signald) error { +func (r *ProtocolRequest) Submit(conn *signald.Signald) (err error) { r.Version = "v1" r.Type = "protocol" if r.ID == "" { r.ID = signald.GenerateID() } + err = conn.RawRequest(r) + if err != nil { + log.Println("signald-go: error submitting request to signald") + return + } - return conn.RawRequest(r) + responseChannel := conn.GetResponseListener(r.ID) + defer conn.CloseResponseListener(r.ID) + + rawResponse := <-responseChannel + if rawResponse.Error != nil { + err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) + return + } + + return err } @@ -301,11 +329,10 @@ func (r *ReactRequest) Submit(conn *signald.Signald) (response SendResponse, err if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -316,11 +343,12 @@ func (r *ReactRequest) Submit(conn *signald.Signald) (response SendResponse, err err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil @@ -328,14 +356,28 @@ func (r *ReactRequest) Submit(conn *signald.Signald) (response SendResponse, err } // Submit: Remove a linked device from the Signal account. Only allowed when the local device id is 1 -func (r *RemoveLinkedDeviceRequest) Submit(conn *signald.Signald) error { +func (r *RemoveLinkedDeviceRequest) Submit(conn *signald.Signald) (err error) { r.Version = "v1" r.Type = "remove_linked_device" if r.ID == "" { r.ID = signald.GenerateID() } + err = conn.RawRequest(r) + if err != nil { + log.Println("signald-go: error submitting request to signald") + return + } - return conn.RawRequest(r) + responseChannel := conn.GetResponseListener(r.ID) + defer conn.CloseResponseListener(r.ID) + + rawResponse := <-responseChannel + if rawResponse.Error != nil { + err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) + return + } + + return err } @@ -346,11 +388,10 @@ func (r *ResolveAddressRequest) Submit(conn *signald.Signald) (response JsonAddr if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -361,11 +402,12 @@ func (r *ResolveAddressRequest) Submit(conn *signald.Signald) (response JsonAddr err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil @@ -378,11 +420,10 @@ func (r *SendRequest) Submit(conn *signald.Signald) (response SendResponse, err if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -393,25 +434,40 @@ func (r *SendRequest) Submit(conn *signald.Signald) (response SendResponse, err err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil } -func (r *SetProfile) Submit(conn *signald.Signald) error { +func (r *SetProfile) Submit(conn *signald.Signald) (err error) { r.Version = "v1" r.Type = "set_profile" if r.ID == "" { r.ID = signald.GenerateID() } + err = conn.RawRequest(r) + if err != nil { + log.Println("signald-go: error submitting request to signald") + return + } - return conn.RawRequest(r) + responseChannel := conn.GetResponseListener(r.ID) + defer conn.CloseResponseListener(r.ID) + + rawResponse := <-responseChannel + if rawResponse.Error != nil { + err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) + return + } + + return err } @@ -422,11 +478,10 @@ func (r *UpdateGroupRequest) Submit(conn *signald.Signald) (response GroupInfo, if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -437,11 +492,12 @@ func (r *UpdateGroupRequest) Submit(conn *signald.Signald) (response GroupInfo, err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil @@ -454,11 +510,10 @@ func (r *VersionRequest) Submit(conn *signald.Signald) (response JsonVersionMess if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -469,11 +524,12 @@ func (r *VersionRequest) Submit(conn *signald.Signald) (response JsonVersionMess err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil diff --git a/signald/client-protocol/v1alpha1/requests.go b/signald/client-protocol/v1alpha1/requests.go index 65d3312..f3f39ff 100644 --- a/signald/client-protocol/v1alpha1/requests.go +++ b/signald/client-protocol/v1alpha1/requests.go @@ -17,11 +17,10 @@ func (r *AcceptInvitationRequest) Submit(conn *signald.Signald) (response JsonGr if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -32,11 +31,12 @@ func (r *AcceptInvitationRequest) Submit(conn *signald.Signald) (response JsonGr err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil @@ -50,11 +50,10 @@ func (r *ApproveMembershipRequest) Submit(conn *signald.Signald) (response JsonG if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -65,11 +64,12 @@ func (r *ApproveMembershipRequest) Submit(conn *signald.Signald) (response JsonG err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil @@ -83,11 +83,10 @@ func (r *GetGroupRequest) Submit(conn *signald.Signald) (response JsonGroupV2Inf if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -98,11 +97,12 @@ func (r *GetGroupRequest) Submit(conn *signald.Signald) (response JsonGroupV2Inf err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil @@ -116,11 +116,10 @@ func (r *GetLinkedDevicesRequest) Submit(conn *signald.Signald) (response Linked if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -131,11 +130,12 @@ func (r *GetLinkedDevicesRequest) Submit(conn *signald.Signald) (response Linked err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil @@ -149,11 +149,10 @@ func (r *JoinGroupRequest) Submit(conn *signald.Signald) (response JsonGroupJoin if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -164,37 +163,66 @@ func (r *JoinGroupRequest) Submit(conn *signald.Signald) (response JsonGroupJoin err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil } -func (r *ProtocolRequest) Submit(conn *signald.Signald) error { +func (r *ProtocolRequest) Submit(conn *signald.Signald) (err error) { r.Version = "v1alpha1" r.Type = "protocol" if r.ID == "" { r.ID = signald.GenerateID() } + err = conn.RawRequest(r) + if err != nil { + log.Println("signald-go: error submitting request to signald") + return + } - return conn.RawRequest(r) + responseChannel := conn.GetResponseListener(r.ID) + defer conn.CloseResponseListener(r.ID) + + rawResponse := <-responseChannel + if rawResponse.Error != nil { + err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) + return + } + + return err } // Submit: Remove a linked device from the Signal account. Only allowed when the local device id is 1 -func (r *RemoveLinkedDeviceRequest) Submit(conn *signald.Signald) error { +func (r *RemoveLinkedDeviceRequest) Submit(conn *signald.Signald) (err error) { r.Version = "v1alpha1" r.Type = "remove_linked_device" if r.ID == "" { r.ID = signald.GenerateID() } + err = conn.RawRequest(r) + if err != nil { + log.Println("signald-go: error submitting request to signald") + return + } - return conn.RawRequest(r) + responseChannel := conn.GetResponseListener(r.ID) + defer conn.CloseResponseListener(r.ID) + + rawResponse := <-responseChannel + if rawResponse.Error != nil { + err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) + return + } + + return err } @@ -205,11 +233,10 @@ func (r *UpdateGroupRequest) Submit(conn *signald.Signald) (response JsonGroupV2 if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -220,11 +247,12 @@ func (r *UpdateGroupRequest) Submit(conn *signald.Signald) (response JsonGroupV2 err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil diff --git a/signald/client-protocol/v1alpha2/requests.go b/signald/client-protocol/v1alpha2/requests.go index b5b99ba..018a254 100644 --- a/signald/client-protocol/v1alpha2/requests.go +++ b/signald/client-protocol/v1alpha2/requests.go @@ -17,11 +17,10 @@ func (r *UpdateGroupRequest) Submit(conn *signald.Signald) (response GroupInfo, if r.ID == "" { r.ID = signald.GenerateID() } - err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -32,11 +31,12 @@ func (r *UpdateGroupRequest) Submit(conn *signald.Signald) (response GroupInfo, err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil diff --git a/tools/generator/requests.go.tmpl b/tools/generator/requests.go.tmpl index 1b5b7f2..7f2967f 100644 --- a/tools/generator/requests.go.tmpl +++ b/tools/generator/requests.go.tmpl @@ -13,7 +13,7 @@ import ({{if .Responses}} {{range $type, $action := .Actions}} {{if ne $action.Request ""}} {{if ne $action.Doc ""}}// Submit: {{$action.Doc}}{{end}} -func (r *{{$action.Request}}) Submit(conn *signald.Signald) ({{if ne $action.Response ""}}response {{$action.Response}}, err {{end}}error) { +func (r *{{$action.Request}}) Submit(conn *signald.Signald) ({{if ne $action.Response ""}}response {{$action.Response}}, {{end}}err error) { r.Version = "{{$.Version}}" {{else}} {{if ne $action.Doc ""}}// {{$action.FnName}}: {{$action.Doc}}{{end}} @@ -23,12 +23,10 @@ func {{$action.FnName}}(conn *signald.Signald) ({{if ne $action.Response ""}}res if(r.ID == "") { r.ID = signald.GenerateID() } - -{{if ne $action.Response ""}} err = conn.RawRequest(r) if err != nil { log.Println("signald-go: error submitting request to signald") - return response, err + return } responseChannel := conn.GetResponseListener(r.ID) @@ -39,16 +37,18 @@ func {{$action.FnName}}(conn *signald.Signald) ({{if ne $action.Response ""}}res err = fmt.Errorf("signald error: %s", string(rawResponse.Error)) return } + +{{if ne $action.Response ""}} err = json.Unmarshal(rawResponse.Data, &response) if err != nil { rawResponseJson, _ := rawResponse.Data.MarshalJSON() log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson)) - return response, err + return } return response, nil {{else}} - return conn.RawRequest(r) + return err {{end}} } {{end}} \ No newline at end of file