Refactor JSON RPC receive mode into an enum

This commit is contained in:
Mykola Balakin 2026-10-17 15:49:53 +03:00
parent 53fa03881f
commit 801b015516
No known key found for this signature in database
GPG Key ID: B9B5039ECA2ACA32
3 changed files with 27 additions and 12 deletions

View File

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

View File

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

View File

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