Skip to content

Commit

Permalink
Add id parameter to appservice ping
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Mar 11, 2023
1 parent c036250 commit ff65890
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
13 changes: 13 additions & 0 deletions appservice/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,19 @@ func (as *AppService) PostPing(w http.ResponseWriter, r *http.Request) {
if !as.CheckServerToken(w, r) {
return
}
body, err := io.ReadAll(r.Body)
if err != nil || len(body) == 0 || !json.Valid(body) {
Error{
ErrorCode: ErrNotJSON,
HTTPStatus: http.StatusBadRequest,
Message: "Missing request body",
}.Write(w)
return
}

var txn mautrix.ReqAppservicePing
_ = json.Unmarshal(body, &txn)
as.Log.Debug().Str("txn_id", txn.TxnID).Msg("Received ping from homeserver")

w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
Expand Down
10 changes: 7 additions & 3 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,10 @@ func (br *Bridge) ensureConnection() {
}

if br.SpecVersions.UnstableFeatures["fi.mau.msc2659"] && br.AS.Host.Port != 0 {
resp, err := br.Bot.AppservicePing()
txnID := br.Bot.TxnID()
resp, err := br.Bot.AppservicePing(br.Config.AppService.ID, txnID)
if err != nil {
evt := br.ZLog.WithLevel(zerolog.FatalLevel).Err(err)
evt := br.ZLog.WithLevel(zerolog.FatalLevel).Err(err).Str("txn_id", txnID)
var httpErr mautrix.HTTPError
if errors.As(err, &httpErr) && httpErr.RespError != nil {
if val, ok := httpErr.RespError.ExtraData["body"].(string); ok {
Expand All @@ -320,7 +321,10 @@ func (br *Bridge) ensureConnection() {
evt.Msg("Homeserver -> bridge connection is not working")
os.Exit(13)
}
br.ZLog.Debug().Int64("duration_ms", resp.DurationMS).Msg("Homeserver -> bridge connection works")
br.ZLog.Debug().
Str("txn_id", txnID).
Int64("duration_ms", resp.DurationMS).
Msg("Homeserver -> bridge connection works")
} else {
br.ZLog.Debug().Msg("Homeserver does not support checking status of homeserver -> bridge connection")
}
Expand Down
5 changes: 3 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1963,10 +1963,11 @@ func (cli *Client) BatchSend(roomID id.RoomID, req *ReqBatchSend) (resp *RespBat
return
}

func (cli *Client) AppservicePing() (resp *RespAppservicePing, err error) {
func (cli *Client) AppservicePing(id, txnID string) (resp *RespAppservicePing, err error) {
_, err = cli.MakeFullRequest(FullRequest{
Method: http.MethodPost,
URL: cli.BuildClientURL("unstable", "fi.mau.msc2659", "appservice", "ping"),
URL: cli.BuildClientURL("unstable", "fi.mau.msc2659", "appservice", id, "ping"),
RequestJSON: &ReqAppservicePing{TxnID: txnID},
ResponseJSON: &resp,
// This endpoint intentionally returns 50x, so don't retry
MaxAttempts: 1,
Expand Down
4 changes: 4 additions & 0 deletions requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,10 @@ func (req *ReqHierarchy) Query() map[string]string {
return query
}

type ReqAppservicePing struct {
TxnID string `json:"transaction_id,omitempty"`
}

type ReqBeeperMergeRoom struct {
NewRoom ReqCreateRoom `json:"create"`
Key string `json:"key"`
Expand Down

0 comments on commit ff65890

Please sign in to comment.