From 801b015516edbb942efd167c10f7d9706a117a56 Mon Sep 17 00:00:00 2001 From: Mykola Balakin Date: Sat, 17 Oct 2026 15:49:53 +0300 Subject: [PATCH] Refactor JSON RPC receive mode into an enum --- src/client/client.go | 15 +++++++++++---- src/client/jsonrpc2.go | 12 ++++++------ src/main.go | 12 ++++++++++-- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/client/client.go b/src/client/client.go index 1df7b47..c892fd4 100644 --- a/src/client/client.go +++ b/src/client/client.go @@ -53,6 +53,13 @@ const ( JsonRpc ) +type JsonRpcReceiveMode int + +const ( + OnStart JsonRpcReceiveMode = iota + 1 + Manual +) + type GroupLinkState int const ( @@ -406,11 +413,11 @@ type SignalClient struct { signalCliApiConfig *utils.SignalCliApiConfig cliClient *CliClient receiveWebhookUrl string - receiveManualMode bool + jsonRpcReceiveMode JsonRpcReceiveMode } func NewSignalClient(signalCliConfig string, attachmentTmpDir string, avatarTmpDir string, signalCliMode SignalCliMode, - jsonRpc2ClientConfigPath string, signalCliApiConfigPath string, receiveWebhookUrl string, receiveManualMode bool) *SignalClient { + jsonRpc2ClientConfigPath string, signalCliApiConfigPath string, receiveWebhookUrl string, jsonRpcReceiveMode JsonRpcReceiveMode) *SignalClient { return &SignalClient{ signalCliConfig: signalCliConfig, attachmentTmpDir: attachmentTmpDir, @@ -420,7 +427,7 @@ func NewSignalClient(signalCliConfig string, attachmentTmpDir string, avatarTmpD jsonRpc2Clients: make(map[string]*JsonRpc2Client), signalCliApiConfigPath: signalCliApiConfigPath, receiveWebhookUrl: receiveWebhookUrl, - receiveManualMode: receiveManualMode, + jsonRpcReceiveMode: jsonRpcReceiveMode, } } @@ -444,7 +451,7 @@ func (s *SignalClient) Init(maxRetries int) error { tcpPortsNumberMapping := s.jsonRpc2ClientConfig.GetTcpPortsForNumbers() for number, tcpPort := range tcpPortsNumberMapping { - s.jsonRpc2Clients[number] = NewJsonRpc2Client(s.signalCliApiConfig, number, s.receiveManualMode) + s.jsonRpc2Clients[number] = NewJsonRpc2Client(s.signalCliApiConfig, number, s.jsonRpcReceiveMode) err := s.jsonRpc2Clients[number].Dial("127.0.0.1:"+strconv.FormatInt(tcpPort, 10), maxRetries) if err != nil { return err diff --git a/src/client/jsonrpc2.go b/src/client/jsonrpc2.go index 9c8c6de..f6876be 100644 --- a/src/client/jsonrpc2.go +++ b/src/client/jsonrpc2.go @@ -73,18 +73,18 @@ type JsonRpc2Client struct { accountsByChannelUuid map[string]string signalCliApiConfig *utils.SignalCliApiConfig number string - manualReceiveMode bool + jsonRpcReceiveMode JsonRpcReceiveMode receivedMessagesMutex sync.Mutex receivedResponsesMutex sync.Mutex receiveSubscriptionsMutex sync.Mutex address string } -func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number string, manualRcpReceiveMode bool) *JsonRpc2Client { +func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number string, jsonRpcReceiveMode JsonRpcReceiveMode) *JsonRpc2Client { return &JsonRpc2Client{ signalCliApiConfig: signalCliApiConfig, number: number, - manualReceiveMode: manualRcpReceiveMode, + jsonRpcReceiveMode: jsonRpcReceiveMode, receivedResponsesById: make(map[string]chan JsonRpc2MessageResponse), receivedMessagesChannels: make(map[string]chan JsonRpc2ReceivedMessage), receiveSubscriptions: make(map[string]*receiveSubscription), @@ -259,7 +259,7 @@ func (r *JsonRpc2Client) ReceiveData(number string, receiveWebhookUrl string) { // the envelope directly. Unwrap so the broadcast format is // the same in both modes and downstream consumers (e.g. the // websocket handler) don't have to know which mode is in use. - if r.manualReceiveMode { + if r.jsonRpcReceiveMode == Manual { var manualWrapper struct { Subscription int64 `json:"subscription"` Result json.RawMessage `json:"result"` @@ -378,7 +378,7 @@ func (r *JsonRpc2Client) GetReceiveChannel(account string) (chan JsonRpc2Receive return c, "", err } - if r.manualReceiveMode { + if r.jsonRpcReceiveMode == Manual { if err := r.acquireReceiveSubscription(account); err != nil { return c, "", fmt.Errorf("Couldn't create subscription for account %s: %w", account, err) } @@ -399,7 +399,7 @@ func (r *JsonRpc2Client) RemoveReceiveChannel(channelUuid string) { delete(r.accountsByChannelUuid, channelUuid) r.receivedMessagesMutex.Unlock() - if r.manualReceiveMode { + if r.jsonRpcReceiveMode == Manual { if err := r.releaseReceiveSubscription(account); err != nil { log.Errorf("Couldn't release subscription for account %s: %s", account, err.Error()) } diff --git a/src/main.go b/src/main.go index bbeb8fa..ee8cf0c 100644 --- a/src/main.go +++ b/src/main.go @@ -156,7 +156,15 @@ func main() { } } - signalCliManualReceive := utils.GetEnv("JSON_RPC_RECEIVE_MODE", "") == "manual" + jsonRpcReceiveMode := client.OnStart + jsonRpcReceiveModeValue := utils.GetEnv("JSON_RPC_RECEIVE_MODE", "") + if jsonRpcReceiveModeValue == "on-start" { + jsonRpcReceiveMode = client.OnStart + } else if jsonRpcReceiveModeValue == "manual" { + jsonRpcReceiveMode = client.Manual + } else if jsonRpcReceiveModeValue != "" { + log.Fatal("Invalid JSON_RPC_RECEIVE_MODE environment variable set!") + } webhookUrl := utils.GetEnv("RECEIVE_WEBHOOK_URL", "") if webhookUrl != "" && signalCliMode != client.JsonRpc { @@ -165,7 +173,7 @@ func main() { jsonRpc2ClientConfigPathPath := *signalCliConfig + "/jsonrpc2.yml" signalCliApiConfigPath := *signalCliConfig + "/api-config.yml" - signalClient := client.NewSignalClient(*signalCliConfig, *attachmentTmpDir, *avatarTmpDir, signalCliMode, jsonRpc2ClientConfigPathPath, signalCliApiConfigPath, webhookUrl, signalCliManualReceive) + signalClient := client.NewSignalClient(*signalCliConfig, *attachmentTmpDir, *avatarTmpDir, signalCliMode, jsonRpc2ClientConfigPathPath, signalCliApiConfigPath, webhookUrl, jsonRpcReceiveMode) err = signalClient.Init(60) if err != nil { log.Fatal("Couldn't init Signal Client: ", err.Error())