mirror of
https://github.com/element-hq/dendrite.git
synced 2025-09-14 05:12:26 +03:00
Add context.Context to the federation client (#225)
* Add context.Context to the federation client * gb vendor update github.com/matrix-org/gomatrixserverlib
This commit is contained in:
parent
086683459f
commit
029e71828a
17 changed files with 139 additions and 72 deletions
|
@ -1,6 +1,7 @@
|
|||
package gomatrixserverlib
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
@ -31,7 +32,7 @@ func NewFederationClient(
|
|||
}
|
||||
}
|
||||
|
||||
func (ac *FederationClient) doRequest(r FederationRequest, resBody interface{}) error {
|
||||
func (ac *FederationClient) doRequest(ctx context.Context, r FederationRequest, resBody interface{}) error {
|
||||
if err := r.Sign(ac.serverName, ac.serverKeyID, ac.serverPrivateKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -41,7 +42,7 @@ func (ac *FederationClient) doRequest(r FederationRequest, resBody interface{})
|
|||
return err
|
||||
}
|
||||
|
||||
res, err := ac.client.Do(req)
|
||||
res, err := ac.client.Do(req.WithContext(ctx))
|
||||
if res != nil {
|
||||
defer res.Body.Close() // nolint: errcheck
|
||||
}
|
||||
|
@ -87,13 +88,15 @@ func (ac *FederationClient) doRequest(r FederationRequest, resBody interface{})
|
|||
var federationPathPrefix = "/_matrix/federation/v1"
|
||||
|
||||
// SendTransaction sends a transaction
|
||||
func (ac *FederationClient) SendTransaction(t Transaction) (res RespSend, err error) {
|
||||
func (ac *FederationClient) SendTransaction(
|
||||
ctx context.Context, t Transaction,
|
||||
) (res RespSend, err error) {
|
||||
path := federationPathPrefix + "/send/" + string(t.TransactionID) + "/"
|
||||
req := NewFederationRequest("PUT", t.Destination, path)
|
||||
if err = req.SetContent(t); err != nil {
|
||||
return
|
||||
}
|
||||
err = ac.doRequest(req, &res)
|
||||
err = ac.doRequest(ctx, req, &res)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -106,12 +109,14 @@ func (ac *FederationClient) SendTransaction(t Transaction) (res RespSend, err er
|
|||
// If this successfully returns an acceptable event we will sign it with our
|
||||
// server's key and pass it to SendJoin.
|
||||
// See https://matrix.org/docs/spec/server_server/unstable.html#joining-rooms
|
||||
func (ac *FederationClient) MakeJoin(s ServerName, roomID, userID string) (res RespMakeJoin, err error) {
|
||||
func (ac *FederationClient) MakeJoin(
|
||||
ctx context.Context, s ServerName, roomID, userID string,
|
||||
) (res RespMakeJoin, err error) {
|
||||
path := federationPathPrefix + "/make_join/" +
|
||||
url.PathEscape(roomID) + "/" +
|
||||
url.PathEscape(userID)
|
||||
req := NewFederationRequest("GET", s, path)
|
||||
err = ac.doRequest(req, &res)
|
||||
err = ac.doRequest(ctx, req, &res)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -119,7 +124,9 @@ func (ac *FederationClient) MakeJoin(s ServerName, roomID, userID string) (res R
|
|||
// remote matrix server.
|
||||
// This is used to join a room the local server isn't a member of.
|
||||
// See https://matrix.org/docs/spec/server_server/unstable.html#joining-rooms
|
||||
func (ac *FederationClient) SendJoin(s ServerName, event Event) (res RespSendJoin, err error) {
|
||||
func (ac *FederationClient) SendJoin(
|
||||
ctx context.Context, s ServerName, event Event,
|
||||
) (res RespSendJoin, err error) {
|
||||
path := federationPathPrefix + "/send_join/" +
|
||||
url.PathEscape(event.RoomID()) + "/" +
|
||||
url.PathEscape(event.EventID())
|
||||
|
@ -127,13 +134,15 @@ func (ac *FederationClient) SendJoin(s ServerName, event Event) (res RespSendJoi
|
|||
if err = req.SetContent(event); err != nil {
|
||||
return
|
||||
}
|
||||
err = ac.doRequest(req, &res)
|
||||
err = ac.doRequest(ctx, req, &res)
|
||||
return
|
||||
}
|
||||
|
||||
// SendInvite sends an invite m.room.member event to an invited server to be
|
||||
// signed by it. This is used to invite a user that is not on the local server.
|
||||
func (ac *FederationClient) SendInvite(s ServerName, event Event) (res RespInvite, err error) {
|
||||
func (ac *FederationClient) SendInvite(
|
||||
ctx context.Context, s ServerName, event Event,
|
||||
) (res RespInvite, err error) {
|
||||
path := federationPathPrefix + "/invite/" +
|
||||
url.PathEscape(event.RoomID()) + "/" +
|
||||
url.PathEscape(event.EventID())
|
||||
|
@ -141,7 +150,7 @@ func (ac *FederationClient) SendInvite(s ServerName, event Event) (res RespInvit
|
|||
if err = req.SetContent(event); err != nil {
|
||||
return
|
||||
}
|
||||
err = ac.doRequest(req, &res)
|
||||
err = ac.doRequest(ctx, req, &res)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -150,38 +159,44 @@ func (ac *FederationClient) SendInvite(s ServerName, event Event) (res RespInvit
|
|||
// server.
|
||||
// This is used to exchange a m.room.third_party_invite event for a m.room.member
|
||||
// one in a room the local server isn't a member of.
|
||||
func (ac *FederationClient) ExchangeThirdPartyInvite(s ServerName, builder EventBuilder) (err error) {
|
||||
func (ac *FederationClient) ExchangeThirdPartyInvite(
|
||||
ctx context.Context, s ServerName, builder EventBuilder,
|
||||
) (err error) {
|
||||
path := federationPathPrefix + "/exchange_third_party_invite/" +
|
||||
url.PathEscape(builder.RoomID)
|
||||
req := NewFederationRequest("PUT", s, path)
|
||||
if err = req.SetContent(builder); err != nil {
|
||||
return
|
||||
}
|
||||
err = ac.doRequest(req, nil)
|
||||
err = ac.doRequest(ctx, req, nil)
|
||||
return
|
||||
}
|
||||
|
||||
// LookupState retrieves the room state for a room at an event from a
|
||||
// remote matrix server as full matrix events.
|
||||
func (ac *FederationClient) LookupState(s ServerName, roomID, eventID string) (res RespState, err error) {
|
||||
func (ac *FederationClient) LookupState(
|
||||
ctx context.Context, s ServerName, roomID, eventID string,
|
||||
) (res RespState, err error) {
|
||||
path := federationPathPrefix + "/state/" +
|
||||
url.PathEscape(roomID) +
|
||||
"/?event_id=" +
|
||||
url.QueryEscape(eventID)
|
||||
req := NewFederationRequest("GET", s, path)
|
||||
err = ac.doRequest(req, &res)
|
||||
err = ac.doRequest(ctx, req, &res)
|
||||
return
|
||||
}
|
||||
|
||||
// LookupStateIDs retrieves the room state for a room at an event from a
|
||||
// remote matrix server as lists of matrix event IDs.
|
||||
func (ac *FederationClient) LookupStateIDs(s ServerName, roomID, eventID string) (res RespStateIDs, err error) {
|
||||
func (ac *FederationClient) LookupStateIDs(
|
||||
ctx context.Context, s ServerName, roomID, eventID string,
|
||||
) (res RespStateIDs, err error) {
|
||||
path := federationPathPrefix + "/state_ids/" +
|
||||
url.PathEscape(roomID) +
|
||||
"/?event_id=" +
|
||||
url.QueryEscape(eventID)
|
||||
req := NewFederationRequest("GET", s, path)
|
||||
err = ac.doRequest(req, &res)
|
||||
err = ac.doRequest(ctx, req, &res)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -190,10 +205,12 @@ func (ac *FederationClient) LookupStateIDs(s ServerName, roomID, eventID string)
|
|||
// being looked up on.
|
||||
// If the room alias doesn't exist on the remote server then a 404 gomatrix.HTTPError
|
||||
// is returned.
|
||||
func (ac *FederationClient) LookupRoomAlias(s ServerName, roomAlias string) (res RespDirectory, err error) {
|
||||
func (ac *FederationClient) LookupRoomAlias(
|
||||
ctx context.Context, s ServerName, roomAlias string,
|
||||
) (res RespDirectory, err error) {
|
||||
path := federationPathPrefix + "/query/directory?room_alias=" +
|
||||
url.QueryEscape(roomAlias)
|
||||
req := NewFederationRequest("GET", s, path)
|
||||
err = ac.doRequest(req, &res)
|
||||
err = ac.doRequest(ctx, req, &res)
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue