Add force param to logout provisioning API

This commit is contained in:
Tulir Asokan 2020-11-19 19:18:34 +02:00
parent e6ccdb83b7
commit b24672d6b4

View file

@ -92,6 +92,7 @@ func (prov *ProvisioningAPI) DeleteSession(w http.ResponseWriter, r *http.Reques
_, _ = user.Conn.Disconnect() _, _ = user.Conn.Disconnect()
user.Conn.RemoveHandlers() user.Conn.RemoveHandlers()
user.Conn = nil user.Conn = nil
user.bridge.Metrics.TrackConnectionState(user.JID, false)
} }
jsonResponse(w, http.StatusOK, Response{true, "Session information purged"}) jsonResponse(w, http.StatusOK, Response{true, "Session information purged"})
} }
@ -281,22 +282,38 @@ func (prov *ProvisioningAPI) Logout(w http.ResponseWriter, r *http.Request) {
return return
} }
err := user.Conn.Logout() force := strings.ToLower(r.URL.Query().Get("force")) == "true"
if err != nil {
user.log.Warnln("Error while logging out:", err) if user.Conn == nil {
jsonResponse(w, http.StatusInternalServerError, Error{ if !force {
Error: fmt.Sprintf("Unknown error while logging out: %v", err), jsonResponse(w, http.StatusNotFound, Error{
ErrCode: err.Error(), Error: "You're not connected",
}) ErrCode: "not connected",
return })
}
} else {
err := user.Conn.Logout()
if err != nil {
user.log.Warnln("Error while logging out:", err)
if !force {
jsonResponse(w, http.StatusInternalServerError, Error{
Error: fmt.Sprintf("Unknown error while logging out: %v", err),
ErrCode: err.Error(),
})
return
}
}
_, err = user.Conn.Disconnect()
if err != nil {
user.log.Warnln("Error while disconnecting after logout:", err)
}
user.Conn.RemoveHandlers()
user.Conn = nil
} }
_, err = user.Conn.Disconnect()
if err != nil { user.bridge.Metrics.TrackConnectionState(user.JID, false)
user.log.Warnln("Error while disconnecting after logout:", err)
}
user.Conn.RemoveHandlers()
user.Conn = nil
user.removeFromJIDMap() user.removeFromJIDMap()
// TODO this causes a foreign key violation, which should be fixed // TODO this causes a foreign key violation, which should be fixed
//ce.User.JID = "" //ce.User.JID = ""
user.SetSession(nil) user.SetSession(nil)