Skip to content

Commit

Permalink
Reconnect WhatsApp socket (e2e messaging) on 415 (`ConnectFailureNotF…
Browse files Browse the repository at this point in the history
…ound`) error
  • Loading branch information
javiercr committed Jun 6, 2024
1 parent df9251e commit 8c7e5a5
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -1040,12 +1040,19 @@ func (user *User) e2eeEventHandler(rawEvt any) {
user.BridgeState.Send(user.waState)
go user.sendMarkdownBridgeAlert(context.TODO(), "Error in WhatsApp connection: %s", evt.PermanentDisconnectDescription())
case events.PermanentDisconnect:
cf, ok := evt.(*events.LoggedOut)
if ok && cf.Reason == events.ConnectFailureLoggedOut && !cf.OnConnect && user.canReconnect() {
user.WADevice = nil
user.log.Debug().Msg("Doing full reconnect after WhatsApp 401 error")
go user.FullReconnect()
if user.canReconnect() {
switch e := evt.(type) {
case *events.LoggedOut:
if e.Reason == events.ConnectFailureLoggedOut && !e.OnConnect {
user.resetWADeviceAndReconnect(e.Reason)
}
case *events.ConnectFailure:
if e.Reason == events.ConnectFailureNotFound {
user.resetWADeviceAndReconnect(e.Reason)
}
}
}

user.waState = status.BridgeState{
StateEvent: status.StateUnknownError,
Error: WAPermanentError,
Expand All @@ -1058,6 +1065,12 @@ func (user *User) e2eeEventHandler(rawEvt any) {
}
}

func (user *User) resetWADeviceAndReconnect(reason events.ConnectFailureReason) {
user.log.Debug().Int("ConnectFailureReason", int(reason)).Msg("Resetting WADevice and reconnecting")
user.WADevice = nil
go user.FullReconnect()
}

func (user *User) saveInitialTable(currentUser types.UserInfo, tbl *table.LSTable) {
var newFBID int64
// TODO figure out why the contact IDs for self is different than the fbid in the ready event
Expand Down

0 comments on commit 8c7e5a5

Please sign in to comment.