Skip to content

Commit

Permalink
event: allow blocking acks on event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Feb 10, 2025
1 parent 495e4eb commit 8a34d8f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion call.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (cli *Client) handleCallEvent(node *waBinary.Node) {
go cli.sendAck(node)
defer cli.maybeDeferredAck(node)

if len(node.GetChildren()) != 1 {
cli.dispatchEvent(&events.UnknownCallEvent{Node: node})
Expand Down
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ type Client struct {
// AutoReconnectHook is called when auto-reconnection fails. If the function returns false,
// the client will not attempt to reconnect. The number of retries can be read from AutoReconnectErrors.
AutoReconnectHook func(error) bool
// If SynchronousAck is set, acks for messages will only be sent after all event handlers return.
SynchronousAck bool

DisableLoginAutoReconnect bool

Expand Down
2 changes: 1 addition & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (cli *Client) handleEncryptedMessage(node *waBinary.Node) {
if len(info.PushName) > 0 && info.PushName != "-" {
go cli.updatePushName(info.Sender, info, info.PushName)
}
go cli.sendAck(node)
defer cli.maybeDeferredAck(node)
if info.Sender.Server == types.NewsletterServer {
cli.handlePlaintextMessage(info, node)
} else {
Expand Down
20 changes: 10 additions & 10 deletions notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func (cli *Client) handleNotification(node *waBinary.Node) {
if !ag.OK() {
return
}
go cli.sendAck(node)
defer cli.maybeDeferredAck(node)
switch notifType {
case "encrypt":
go cli.handleEncryptNotification(node)
Expand All @@ -386,30 +386,30 @@ func (cli *Client) handleNotification(node *waBinary.Node) {
case "account_sync":
go cli.handleAccountSyncNotification(node)
case "devices":
go cli.handleDeviceNotification(node)
cli.handleDeviceNotification(node)
case "fbid:devices":
go cli.handleFBDeviceNotification(node)
cli.handleFBDeviceNotification(node)
case "w:gp2":
evt, err := cli.parseGroupNotification(node)
if err != nil {
cli.Log.Errorf("Failed to parse group notification: %v", err)
} else {
go cli.dispatchEvent(evt)
cli.dispatchEvent(evt)
}
case "picture":
go cli.handlePictureNotification(node)
cli.handlePictureNotification(node)
case "mediaretry":
go cli.handleMediaRetryNotification(node)
cli.handleMediaRetryNotification(node)
case "privacy_token":
go cli.handlePrivacyTokenNotification(node)
cli.handlePrivacyTokenNotification(node)
case "link_code_companion_reg":
go cli.tryHandleCodePairNotification(node)
case "newsletter":
go cli.handleNewsletterNotification(node)
cli.handleNewsletterNotification(node)
case "mex":
go cli.handleMexNotification(node)
cli.handleMexNotification(node)
case "status":
go cli.handleStatusNotification(node)
cli.handleStatusNotification(node)
// Other types: business, disappearing_mode, server, status, pay, psa
default:
cli.Log.Debugf("Unhandled notification with type %s", notifType)
Expand Down
15 changes: 13 additions & 2 deletions receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

func (cli *Client) handleReceipt(node *waBinary.Node) {
defer cli.maybeDeferredAck(node)
receipt, err := cli.parseReceipt(node)
if err != nil {
cli.Log.Warnf("Failed to parse receipt: %v", err)
Expand All @@ -28,9 +29,8 @@ func (cli *Client) handleReceipt(node *waBinary.Node) {
}
}()
}
go cli.dispatchEvent(receipt)
cli.dispatchEvent(receipt)
}
go cli.sendAck(node)
}

func (cli *Client) handleGroupedReceipt(partialReceipt events.Receipt, participants *waBinary.Node) {
Expand Down Expand Up @@ -96,6 +96,17 @@ func (cli *Client) parseReceipt(node *waBinary.Node) (*events.Receipt, error) {
return &receipt, nil
}

func (cli *Client) maybeDeferredAck(node *waBinary.Node) func() {
if cli.SynchronousAck {
return func() {
go cli.sendAck(node)
}
} else {
go cli.sendAck(node)
return func() {}
}
}

func (cli *Client) sendAck(node *waBinary.Node) {
attrs := waBinary.Attrs{
"class": node.Tag,
Expand Down

0 comments on commit 8a34d8f

Please sign in to comment.