mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-07-30 04:52:46 +00:00
Synchronous invites (#1273)
* Refactor invites to be synchronous * Fix synchronous invites * Fix client API return type for send invite error * Linter * Restore PerformError on rsAPI.PerformInvite * Update sytest-whitelist * Don't override PerformError with normal errors * Fix error passing * Un-whitelist a couple of tests * Update sytest-whitelist * Try to handle multiple invite rejections better * nolint * Update gomatrixserverlib * Fix /v1/invite test * Remove replace from go.mod
This commit is contained in:
parent
6820b3e024
commit
6cb1a65809
23 changed files with 334 additions and 443 deletions
|
@ -46,7 +46,7 @@ func InviteV2(
|
|||
}
|
||||
}
|
||||
return processInvite(
|
||||
httpReq.Context(), inviteReq.Event(), inviteReq.RoomVersion(), inviteReq.InviteRoomState(), roomID, eventID, cfg, rsAPI, keys,
|
||||
httpReq.Context(), true, inviteReq.Event(), inviteReq.RoomVersion(), inviteReq.InviteRoomState(), roomID, eventID, cfg, rsAPI, keys,
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -75,12 +75,13 @@ func InviteV1(
|
|||
util.GetLogger(httpReq.Context()).Warnf("failed to extract stripped state from invite event")
|
||||
}
|
||||
return processInvite(
|
||||
httpReq.Context(), event, roomVer, strippedState, roomID, eventID, cfg, rsAPI, keys,
|
||||
httpReq.Context(), false, event, roomVer, strippedState, roomID, eventID, cfg, rsAPI, keys,
|
||||
)
|
||||
}
|
||||
|
||||
func processInvite(
|
||||
ctx context.Context,
|
||||
isInviteV2 bool,
|
||||
event gomatrixserverlib.Event,
|
||||
roomVer gomatrixserverlib.RoomVersion,
|
||||
strippedState []gomatrixserverlib.InviteV2StrippedState,
|
||||
|
@ -143,17 +144,31 @@ func processInvite(
|
|||
)
|
||||
|
||||
// Add the invite event to the roomserver.
|
||||
if perr := api.SendInvite(
|
||||
ctx, rsAPI, signedEvent.Headered(roomVer), strippedState, event.Origin(), nil,
|
||||
); perr != nil {
|
||||
util.GetLogger(ctx).WithError(err).Error("producer.SendInvite failed")
|
||||
return perr.JSONResponse()
|
||||
}
|
||||
|
||||
// Return the signed event to the originating server, it should then tell
|
||||
// the other servers in the room that we have been invited.
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusOK,
|
||||
JSON: gomatrixserverlib.RespInviteV2{Event: signedEvent},
|
||||
err = api.SendInvite(
|
||||
ctx, rsAPI, signedEvent.Headered(roomVer), strippedState, api.DoNotSendToOtherServers, nil,
|
||||
)
|
||||
switch e := err.(type) {
|
||||
case *api.PerformError:
|
||||
return e.JSONResponse()
|
||||
case nil:
|
||||
// Return the signed event to the originating server, it should then tell
|
||||
// the other servers in the room that we have been invited.
|
||||
if isInviteV2 {
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusOK,
|
||||
JSON: gomatrixserverlib.RespInviteV2{Event: signedEvent},
|
||||
}
|
||||
} else {
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusOK,
|
||||
JSON: gomatrixserverlib.RespInvite{Event: signedEvent},
|
||||
}
|
||||
}
|
||||
default:
|
||||
util.GetLogger(ctx).WithError(err).Error("api.SendInvite failed")
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusInternalServerError,
|
||||
JSON: jsonerror.InternalServerError(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ import (
|
|||
)
|
||||
|
||||
// MakeLeave implements the /make_leave API
|
||||
// nolint:gocyclo
|
||||
func MakeLeave(
|
||||
httpReq *http.Request,
|
||||
request *gomatrixserverlib.FederationRequest,
|
||||
|
@ -76,6 +77,23 @@ func MakeLeave(
|
|||
return jsonerror.InternalServerError()
|
||||
}
|
||||
|
||||
// If the user has already left then just return their last leave
|
||||
// event. This means that /send_leave will be a no-op, which helps
|
||||
// to reject invites multiple times - hopefully.
|
||||
for _, state := range queryRes.StateEvents {
|
||||
if state.Type() == gomatrixserverlib.MRoomMember && state.StateKeyEquals(userID) {
|
||||
if mem, merr := state.Membership(); merr == nil && mem == gomatrixserverlib.Leave {
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusOK,
|
||||
JSON: map[string]interface{}{
|
||||
"room_version": event.RoomVersion,
|
||||
"event": state,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check that the leave is allowed or not
|
||||
stateEvents := make([]*gomatrixserverlib.Event, len(queryRes.StateEvents))
|
||||
for i := range queryRes.StateEvents {
|
||||
|
@ -99,6 +117,7 @@ func MakeLeave(
|
|||
}
|
||||
|
||||
// SendLeave implements the /send_leave API
|
||||
// nolint:gocyclo
|
||||
func SendLeave(
|
||||
httpReq *http.Request,
|
||||
request *gomatrixserverlib.FederationRequest,
|
||||
|
@ -149,6 +168,48 @@ func SendLeave(
|
|||
}
|
||||
}
|
||||
|
||||
// Check if the user has already left. If so, no-op!
|
||||
queryReq := &api.QueryLatestEventsAndStateRequest{
|
||||
RoomID: roomID,
|
||||
StateToFetch: []gomatrixserverlib.StateKeyTuple{
|
||||
{
|
||||
EventType: gomatrixserverlib.MRoomMember,
|
||||
StateKey: *event.StateKey(),
|
||||
},
|
||||
},
|
||||
}
|
||||
queryRes := &api.QueryLatestEventsAndStateResponse{}
|
||||
err = rsAPI.QueryLatestEventsAndState(httpReq.Context(), queryReq, queryRes)
|
||||
if err != nil {
|
||||
util.GetLogger(httpReq.Context()).WithError(err).Error("rsAPI.QueryLatestEventsAndState failed")
|
||||
return jsonerror.InternalServerError()
|
||||
}
|
||||
// The room doesn't exist or we weren't ever joined to it. Might as well
|
||||
// no-op here.
|
||||
if !queryRes.RoomExists || len(queryRes.StateEvents) == 0 {
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusOK,
|
||||
JSON: struct{}{},
|
||||
}
|
||||
}
|
||||
// Check if we're recycling a previous leave event.
|
||||
if event.EventID() == queryRes.StateEvents[0].EventID() {
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusOK,
|
||||
JSON: struct{}{},
|
||||
}
|
||||
}
|
||||
// We are/were joined/invited/banned or something. Check if
|
||||
// we can no-op here.
|
||||
if len(queryRes.StateEvents) == 1 {
|
||||
if mem, merr := queryRes.StateEvents[0].Membership(); merr == nil && mem == gomatrixserverlib.Leave {
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusOK,
|
||||
JSON: struct{}{},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check that the event is signed by the server sending the request.
|
||||
redacted := event.Redact()
|
||||
verifyRequests := []gomatrixserverlib.VerifyJSONRequest{{
|
||||
|
@ -174,7 +235,8 @@ func SendLeave(
|
|||
if err != nil {
|
||||
util.GetLogger(httpReq.Context()).WithError(err).Error("event.Membership failed")
|
||||
return jsonerror.InternalServerError()
|
||||
} else if mem != gomatrixserverlib.Leave {
|
||||
}
|
||||
if mem != gomatrixserverlib.Leave {
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
JSON: jsonerror.BadJSON("The membership in the event content must be set to leave"),
|
||||
|
|
|
@ -90,16 +90,10 @@ func Setup(
|
|||
JSON: jsonerror.Forbidden("Forbidden by server ACLs"),
|
||||
}
|
||||
}
|
||||
res := InviteV1(
|
||||
return InviteV1(
|
||||
httpReq, request, vars["roomID"], vars["eventID"],
|
||||
cfg, rsAPI, keys,
|
||||
)
|
||||
return util.JSONResponse{
|
||||
Code: res.Code,
|
||||
JSON: []interface{}{
|
||||
res.Code, res.JSON,
|
||||
},
|
||||
}
|
||||
},
|
||||
)).Methods(http.MethodPut, http.MethodOptions)
|
||||
|
||||
|
|
|
@ -102,7 +102,8 @@ func (t *testRoomserverAPI) PerformInvite(
|
|||
ctx context.Context,
|
||||
req *api.PerformInviteRequest,
|
||||
res *api.PerformInviteResponse,
|
||||
) {
|
||||
) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *testRoomserverAPI) PerformJoin(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue