From 29f5ae45c4b22f463003b23e355b951831f08b3e Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 4 Jul 2019 15:08:58 +0300 Subject: [PATCH] Print clean websocket disconnections if they weren't announced by the server beforehand Also print message send errors when Session is not nil even if Connection is nil --- user.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/user.go b/user.go index 3b72bde..c7ee645 100644 --- a/user.go +++ b/user.go @@ -53,6 +53,8 @@ type User struct { ConnectionErrors int + cleanDisconnection bool + messages chan PortalMessage syncLock sync.Mutex } @@ -216,7 +218,7 @@ func (user *User) RestoreSession() bool { } func (user *User) IsLoggedIn() bool { - return user.Conn != nil + return user.Session != nil || user.Conn != nil } func (user *User) Login(ce *CommandEvent) { @@ -377,8 +379,9 @@ func (user *User) HandleError(err error) { } if closed, ok := err.(*whatsapp.ErrConnectionClosed); ok { user.Connected = false - if closed.Code == 1000 { - // Normal closure + if closed.Code == 1000 && user.cleanDisconnection { + user.cleanDisconnection = false + user.log.Infoln("Clean disconnection by server") return } go user.tryReconnect(fmt.Sprintf("Your WhatsApp connection was closed with websocket status code %d", closed.Code)) @@ -601,6 +604,7 @@ func (user *User) HandleCommand(cmd whatsappExt.Command) { msg = fmt.Sprintf("\u26a0 Your WhatsApp connection was closed by the server (reason code: %s).\n\n"+ "Use the `reconnect` command to reconnect.", cmd.Kind) } + user.cleanDisconnection = true go user.bridge.Bot.SendMessageEvent(user.ManagementRoom, mautrix.EventMessage, format.RenderMarkdown(msg)) } }