Update data sent in bridge status updates

This commit is contained in:
Tulir Asokan 2021-06-09 19:15:53 +03:00
parent d851285787
commit b865205198

View file

@ -24,17 +24,19 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings"
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/Rhymen/go-whatsapp" "github.com/Rhymen/go-whatsapp"
"maunium.net/go/mautrix/id" "maunium.net/go/mautrix/id"
) )
type BridgeErrorCode string type BridgeErrorCode string
const ( const (
WANotLoggedIn BridgeErrorCode = "wa-not-logged-in" WANotLoggedIn BridgeErrorCode = "logged-out"
WANotConnected BridgeErrorCode = "wa-not-connected" WANotConnected BridgeErrorCode = "wa-not-connected"
WAConnecting BridgeErrorCode = "wa-connecting" WAConnecting BridgeErrorCode = "wa-connecting"
WATimeout BridgeErrorCode = "wa-timeout" WATimeout BridgeErrorCode = "wa-timeout"
@ -52,17 +54,24 @@ var bridgeHumanErrors = map[BridgeErrorCode]string{
} }
type BridgeState struct { type BridgeState struct {
OK bool `json:"ok"` OK bool `json:"ok"`
Timestamp int64 `json:"timestamp"` Timestamp int64 `json:"timestamp"`
TTL int `json:"ttl"` TTL int `json:"ttl"`
ErrorSource string `json:"error_source,omitempty"` ErrorSource string `json:"error_source,omitempty"`
Error BridgeErrorCode `json:"error,omitempty"` Error BridgeErrorCode `json:"error,omitempty"`
Message string `json:"message,omitempty"` Message string `json:"message,omitempty"`
UserID id.UserID `json:"user_id"` UserID id.UserID `json:"user_id"`
RemoteID string `json:"remote_id"`
RemoteName string `json:"remote_name"`
} }
func (pong *BridgeState) fill() { func (pong *BridgeState) fill(user *User) {
pong.UserID = user.MXID
pong.RemoteID = strings.TrimSuffix(user.JID, whatsapp.NewUserSuffix)
pong.RemoteName = fmt.Sprintf("+%s", pong.RemoteID)
pong.Timestamp = time.Now().Unix() pong.Timestamp = time.Now().Unix()
if !pong.OK { if !pong.OK {
pong.TTL = 60 pong.TTL = 60
@ -121,8 +130,7 @@ func (user *User) sendBridgeState(state BridgeState) {
return return
} }
state.UserID = user.MXID state.fill(user)
state.fill()
if user.prevBridgeStatus != nil && user.prevBridgeStatus.shouldDeduplicate(&state) { if user.prevBridgeStatus != nil && user.prevBridgeStatus.shouldDeduplicate(&state) {
return return
} }
@ -195,8 +203,7 @@ func (prov *ProvisioningAPI) BridgeStatePing(w http.ResponseWriter, r *http.Requ
resp.Error = WANotConnected resp.Error = WANotConnected
} }
} }
resp.UserID = user.MXID resp.fill(user)
resp.fill()
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 user.prevBridgeStatus = &resp