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
} }
force := strings.ToLower(r.URL.Query().Get("force")) == "true"
if user.Conn == nil {
if !force {
jsonResponse(w, http.StatusNotFound, Error{
Error: "You're not connected",
ErrCode: "not connected",
})
}
} else {
err := user.Conn.Logout() err := user.Conn.Logout()
if err != nil { if err != nil {
user.log.Warnln("Error while logging out:", err) user.log.Warnln("Error while logging out:", err)
if !force {
jsonResponse(w, http.StatusInternalServerError, Error{ jsonResponse(w, http.StatusInternalServerError, Error{
Error: fmt.Sprintf("Unknown error while logging out: %v", err), Error: fmt.Sprintf("Unknown error while logging out: %v", err),
ErrCode: err.Error(), ErrCode: err.Error(),
}) })
return return
} }
}
_, err = user.Conn.Disconnect() _, err = user.Conn.Disconnect()
if err != nil { if err != nil {
user.log.Warnln("Error while disconnecting after logout:", err) user.log.Warnln("Error while disconnecting after logout:", err)
} }
user.Conn.RemoveHandlers() user.Conn.RemoveHandlers()
user.Conn = nil user.Conn = nil
}
user.bridge.Metrics.TrackConnectionState(user.JID, false)
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)