Skip to content

Commit

Permalink
Reconnect WhatsApp socket (e2e messaging) on 415 `ConnectFailureNotFo…
Browse files Browse the repository at this point in the history
…und` (#70)
  • Loading branch information
javiercr authored Jun 6, 2024
1 parent df9251e commit 0ec720c
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -1040,12 +1040,26 @@ 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()
switch e := evt.(type) {
case *events.LoggedOut:
if e.Reason == events.ConnectFailureLoggedOut && !e.OnConnect && user.canReconnect() {
user.resetWADevice()
user.log.Debug().Msg("Doing full reconnect after WhatsApp 401 error")
go user.FullReconnect()
}
case *events.ConnectFailure:
if e.Reason == events.ConnectFailureNotFound {
if user.E2EEClient != nil {
user.E2EEClient.Disconnect()
user.WADevice.Delete()
user.resetWADevice()
user.E2EEClient = nil
}
user.log.Debug().Msg("Reconnecting e2ee client after WhatsApp 415 error")
go user.connectE2EE()
}
}

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

func (user *User) resetWADevice() {
user.WADevice = nil
user.WADeviceID = 0
}

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 0ec720c

Please sign in to comment.