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
This commit is contained in:
Tulir Asokan 2019-07-04 15:08:58 +03:00
parent 2023f55f32
commit 29f5ae45c4

10
user.go
View file

@ -53,6 +53,8 @@ type User struct {
ConnectionErrors int ConnectionErrors int
cleanDisconnection bool
messages chan PortalMessage messages chan PortalMessage
syncLock sync.Mutex syncLock sync.Mutex
} }
@ -216,7 +218,7 @@ func (user *User) RestoreSession() bool {
} }
func (user *User) IsLoggedIn() bool { func (user *User) IsLoggedIn() bool {
return user.Conn != nil return user.Session != nil || user.Conn != nil
} }
func (user *User) Login(ce *CommandEvent) { func (user *User) Login(ce *CommandEvent) {
@ -377,8 +379,9 @@ func (user *User) HandleError(err error) {
} }
if closed, ok := err.(*whatsapp.ErrConnectionClosed); ok { if closed, ok := err.(*whatsapp.ErrConnectionClosed); ok {
user.Connected = false user.Connected = false
if closed.Code == 1000 { if closed.Code == 1000 && user.cleanDisconnection {
// Normal closure user.cleanDisconnection = false
user.log.Infoln("Clean disconnection by server")
return return
} }
go user.tryReconnect(fmt.Sprintf("Your WhatsApp connection was closed with websocket status code %d", closed.Code)) 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"+ 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) "Use the `reconnect` command to reconnect.", cmd.Kind)
} }
user.cleanDisconnection = true
go user.bridge.Bot.SendMessageEvent(user.ManagementRoom, mautrix.EventMessage, format.RenderMarkdown(msg)) go user.bridge.Bot.SendMessageEvent(user.ManagementRoom, mautrix.EventMessage, format.RenderMarkdown(msg))
} }
} }