Update bridge state ping endpoint response format

This commit is contained in:
Tulir Asokan 2021-08-25 15:04:40 +03:00
parent b11a6360f4
commit 9f614a3a81

View file

@ -39,6 +39,7 @@ type BridgeStateEvent string
const ( const (
StateStarting BridgeStateEvent = "STARTING" StateStarting BridgeStateEvent = "STARTING"
StateUnconfigured BridgeStateEvent = "UNCONFIGURED" StateUnconfigured BridgeStateEvent = "UNCONFIGURED"
StateRunning BridgeStateEvent = "RUNNING"
StateConnecting BridgeStateEvent = "CONNECTING" StateConnecting BridgeStateEvent = "CONNECTING"
StateBackfilling BridgeStateEvent = "BACKFILLING" StateBackfilling BridgeStateEvent = "BACKFILLING"
StateConnected BridgeStateEvent = "CONNECTED" StateConnected BridgeStateEvent = "CONNECTED"
@ -84,6 +85,11 @@ type BridgeState struct {
RemoteName string `json:"remote_name,omitempty"` RemoteName string `json:"remote_name,omitempty"`
} }
type GlobalBridgeState struct {
RemoteStates map[string]BridgeState `json:"remoteState"`
BridgeState BridgeState `json:"bridgeState"`
}
func (pong BridgeState) fill(user *User) BridgeState { func (pong BridgeState) fill(user *User) BridgeState {
if user != nil { if user != nil {
pong.UserID = user.MXID pong.UserID = user.MXID
@ -211,14 +217,15 @@ func (prov *ProvisioningAPI) BridgeStatePing(w http.ResponseWriter, r *http.Requ
} }
userID := r.URL.Query().Get("user_id") userID := r.URL.Query().Get("user_id")
user := prov.bridge.GetUserByMXID(id.UserID(userID)) user := prov.bridge.GetUserByMXID(id.UserID(userID))
var resp BridgeState var global BridgeState
global.StateEvent = StateRunning
var remote BridgeState
if user.Conn == nil { if user.Conn == nil {
if user.Session == nil { if user.Session == nil {
resp.StateEvent = StateUnconfigured global.StateEvent = StateUnconfigured
resp.Error = WANotLoggedIn
} else { } else {
resp.StateEvent = StateBadCredentials remote.StateEvent = StateBadCredentials
resp.Error = WANotConnected remote.Error = WANotConnected
} }
} else { } else {
if user.Conn.IsConnected() && user.Conn.IsLoggedIn() { if user.Conn.IsConnected() && user.Conn.IsLoggedIn() {
@ -231,38 +238,45 @@ func (prov *ProvisioningAPI) BridgeStatePing(w http.ResponseWriter, r *http.Requ
return return
} }
user.log.Debugfln("Ping %d response: %v", pingID, err) user.log.Debugfln("Ping %d response: %v", pingID, err)
resp.StateEvent = StateTransientDisconnect remote.StateEvent = StateTransientDisconnect
if err == whatsapp.ErrPingFalse { if err == whatsapp.ErrPingFalse {
user.log.Debugln("Forwarding ping false error from provisioning API to HandleError") user.log.Debugln("Forwarding ping false error from provisioning API to HandleError")
go user.HandleError(err) go user.HandleError(err)
resp.Error = WAPingFalse remote.Error = WAPingFalse
} else if errors.Is(err, whatsapp.ErrConnectionTimeout) { } else if errors.Is(err, whatsapp.ErrConnectionTimeout) {
resp.Error = WATimeout remote.Error = WATimeout
} else if errors.Is(err, whatsapp.ErrWebsocketKeepaliveFailed) { } else if errors.Is(err, whatsapp.ErrWebsocketKeepaliveFailed) {
resp.Error = WAServerTimeout remote.Error = WAServerTimeout
} else if err != nil { } else if err != nil {
resp.Error = WAPingError remote.Error = WAPingError
} else { } else {
resp.StateEvent = StateConnected remote.StateEvent = StateConnected
} }
} else if user.Conn.IsLoginInProgress() { } else if user.Conn.IsLoginInProgress() {
resp.StateEvent = StateConnecting remote.StateEvent = StateConnecting
resp.Error = WAConnecting remote.Error = WAConnecting
} else if user.Conn.IsConnected() { } else if user.Conn.IsConnected() {
resp.StateEvent = StateUnconfigured global.StateEvent = StateUnconfigured
resp.Error = WANotLoggedIn
} else { } else {
if user.Session == nil { if user.Session == nil {
resp.StateEvent = StateUnconfigured global.StateEvent = StateUnconfigured
resp.Error = WANotLoggedIn
} else { } else {
resp.StateEvent = StateBadCredentials remote.StateEvent = StateBadCredentials
resp.Error = WANotConnected remote.Error = WANotConnected
} }
} }
} }
resp = resp.fill(user) global = global.fill(nil)
resp := GlobalBridgeState{BridgeState: global}
if len(remote.StateEvent) > 0 {
remote = remote.fill(user)
resp.RemoteStates = map[string]BridgeState{
remote.RemoteID: remote,
}
}
user.log.Debugfln("Responding bridge state in bridge status endpoint: %+v", resp) user.log.Debugfln("Responding bridge state in bridge status endpoint: %+v", resp)
jsonResponse(w, http.StatusOK, &resp) jsonResponse(w, http.StatusOK, &resp)
user.prevBridgeStatus = &resp if len(resp.RemoteStates) > 0 {
user.prevBridgeStatus = &remote
}
} }