From 41b6bf56e5ed2202071d738f5f64e491d8496739 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Mon, 23 May 2022 12:36:33 +0100 Subject: [PATCH] Add `Resident` flag to `QueryRestrictedJoinAllowedResponse` --- roomserver/api/query.go | 3 ++- roomserver/internal/query/query.go | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/roomserver/api/query.go b/roomserver/api/query.go index e6460ab21..33a235829 100644 --- a/roomserver/api/query.go +++ b/roomserver/api/query.go @@ -354,7 +354,8 @@ type QueryRestrictedJoinAllowedRequest struct { } type QueryRestrictedJoinAllowedResponse struct { - Allowed bool `json:"allowed"` + Resident bool `json:"resident"` // Is our homeserver in the relevant rooms? + Allowed bool `json:"allowed"` // Is the join allowed by the rules? } // MarshalJSON stringifies the room ID and StateKeyTuple keys so they can be sent over the wire in HTTP API mode. diff --git a/roomserver/internal/query/query.go b/roomserver/internal/query/query.go index 103f1cb86..5b37c4185 100644 --- a/roomserver/internal/query/query.go +++ b/roomserver/internal/query/query.go @@ -790,6 +790,10 @@ func (r *Queryer) QueryRestrictedJoinAllowed(ctx context.Context, req *api.Query if joinRules.JoinRule != gomatrixserverlib.Restricted { return nil } + // Start off by populating the "resident" flag in the response. If we + // come across any rooms in the request that are missing, we will unset + // the flag. + res.Resident = true // Step through the join rules and see if the user matches any of them. for _, rule := range joinRules.Allow { // We only understand "m.room_membership" rules at this point in @@ -800,21 +804,17 @@ func (r *Queryer) QueryRestrictedJoinAllowed(ctx context.Context, req *api.Query // See if the room exists. If it doesn't exist or if it's a stub // room entry then we can't check memberships. targetRoomInfo, err := r.DB.RoomInfo(ctx, rule.RoomID) - if err != nil { - continue - } - if targetRoomInfo == nil || targetRoomInfo.IsStub { + if err != nil || targetRoomInfo == nil || targetRoomInfo.IsStub { + res.Resident = false continue } // First of all work out if *we* are still in the room, otherwise // it's possible that the memberships will be out of date. isIn, err := r.DB.GetLocalServerInRoom(ctx, targetRoomInfo.RoomNID) - if err != nil { - continue - } - if !isIn { - // We aren't in the room, so we can no longer tell if the room + if err != nil || !isIn { + // If we aren't in the room, we can no longer tell if the room // memberships are up-to-date. + res.Resident = false continue } // At this point we're happy that we are in the room, so now let's @@ -825,6 +825,7 @@ func (r *Queryer) QueryRestrictedJoinAllowed(ctx context.Context, req *api.Query } // If the user is in the room then we will allow the membership. if isIn { + res.Resident = true res.Allowed = true return nil }