Merge pull request #151 from rreuvekamp/fix/142-reconnect-command-doesnt-work-v2

Fix #142
This commit is contained in:
Tulir Asokan 2020-03-26 19:29:36 +02:00 committed by GitHub
commit 8dd5f6a0d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 13 deletions

View file

@ -231,7 +231,18 @@ func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) {
}
return
}
err := ce.User.Conn.Restore()
wasConnected := true
sess, err := ce.User.Conn.Disconnect()
if err == whatsapp.ErrNotConnected {
wasConnected = false
} else if err != nil {
ce.User.log.Warnln("Error while disconnecting:", err)
} else if len(sess.Wid) > 0 {
ce.User.SetSession(&sess)
}
err = ce.User.Conn.Restore()
if err == whatsapp.ErrInvalidSession {
if ce.User.Session != nil {
ce.User.log.Debugln("Got invalid session error when reconnecting, but user has session. Retrying using RestoreWithSession()...")
@ -247,12 +258,13 @@ func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) {
} else if err == whatsapp.ErrLoginInProgress {
ce.Reply("A login or reconnection is already in progress.")
return
} else if err == whatsapp.ErrAlreadyLoggedIn {
ce.Reply("You were already connected.")
return
}
if err != nil {
ce.User.log.Warnln("Error while reconnecting:", err)
if err == whatsapp.ErrAlreadyLoggedIn {
ce.Reply("You were already connected.")
} else if err.Error() == "restore session connection timed out" {
if err.Error() == "restore session connection timed out" {
ce.Reply("Reconnection timed out. Is WhatsApp on your phone reachable?")
} else {
ce.Reply("Unknown error while reconnecting: %v", err)
@ -267,7 +279,14 @@ func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) {
return
}
ce.User.ConnectionErrors = 0
ce.Reply("Reconnected successfully.")
var msg string
if wasConnected {
msg = "Reconnected successfully."
} else {
msg = "Connected successfully."
}
ce.Reply(msg)
ce.User.PostLogin()
}

View file

@ -156,7 +156,18 @@ func (prov *ProvisioningAPI) Reconnect(w http.ResponseWriter, r *http.Request) {
}
return
}
err := user.Conn.Restore()
wasConnected := true
sess, err := user.Conn.Disconnect()
if err == whatsapp.ErrNotConnected {
wasConnected = false
} else if err != nil {
user.log.Warnln("Error while disconnecting:", err)
} else if len(sess.Wid) > 0 {
user.SetSession(&sess)
}
err = user.Conn.Restore()
if err == whatsapp.ErrInvalidSession {
if user.Session != nil {
user.log.Debugln("Got invalid session error when reconnecting, but user has session. Retrying using RestoreWithSession()...")
@ -178,15 +189,16 @@ func (prov *ProvisioningAPI) Reconnect(w http.ResponseWriter, r *http.Request) {
ErrCode: "login in progress",
})
return
}
if err != nil {
user.log.Warnln("Error while reconnecting:", err)
if err == whatsapp.ErrAlreadyLoggedIn {
} else if err == whatsapp.ErrAlreadyLoggedIn {
jsonResponse(w, http.StatusConflict, Error{
Error: "You were already connected.",
ErrCode: err.Error(),
})
} else if err.Error() == "restore session connection timed out" {
return
}
if err != nil {
user.log.Warnln("Error while reconnecting:", err)
if err.Error() == "restore session connection timed out" {
jsonResponse(w, http.StatusForbidden, Error{
Error: "Reconnection timed out. Is WhatsApp on your phone reachable?",
ErrCode: err.Error(),
@ -208,7 +220,15 @@ func (prov *ProvisioningAPI) Reconnect(w http.ResponseWriter, r *http.Request) {
}
user.ConnectionErrors = 0
user.PostLogin()
jsonResponse(w, http.StatusOK, Response{true, "Reconnected successfully."})
var msg string
if wasConnected {
msg = "Reconnected successfully."
} else {
msg = "Connected successfully."
}
jsonResponse(w, http.StatusOK, Response{true, msg})
}
func (prov *ProvisioningAPI) Ping(w http.ResponseWriter, r *http.Request) {