Add Resident flag to QueryRestrictedJoinAllowedResponse

This commit is contained in:
Neil Alexander 2022-05-23 12:36:33 +01:00
parent 919d9942b6
commit 41b6bf56e5
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
2 changed files with 12 additions and 10 deletions

View file

@ -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.

View file

@ -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
}