@@ -39,9 +39,13 @@ const (
3939 ConstSpaceParentEventType = "m.space.parent"
4040)
4141
42- // Walk the room hierarchy to retrieve room information until either
43- // no room left, or provided limit reached. If limit provided is -1, then this is
44- // treated as no limit.
42+ // Traverse the room hierarchy using the provided walker up to the provided limit,
43+ // returning a new walker which can be used to fetch the next page.
44+ //
45+ // If limit is -1, this is treated as no limit, and the entire hierarchy will be traversed.
46+ //
47+ // If returned walker is nil, then there are no more rooms left to traverse. This method does not modify the provided walker, so it
48+ // can be cached.
4549func (querier * Queryer ) QueryNextRoomHierarchyPage (ctx context.Context , walker roomserver.RoomHierarchyWalker , limit int ) ([]fclient.MSC2946Room , * roomserver.RoomHierarchyWalker , error ) {
4650 if authorised , _ := authorised (ctx , querier , walker .Caller , walker .RootRoomID , nil ); ! authorised {
4751 return nil , nil , roomserver.ErrRoomUnknownOrNotAllowed {Err : fmt .Errorf ("room is unknown/forbidden" )}
@@ -234,7 +238,7 @@ func authorisedServer(ctx context.Context, querier *Queryer, roomID spec.RoomID,
234238 }
235239
236240 if rule == spec .Restricted {
237- allowJoinedToRoomIDs = append (allowJoinedToRoomIDs , restrictedJoinRuleAllowedRooms (ctx , joinRuleEv , "m.room_membership" )... )
241+ allowJoinedToRoomIDs = append (allowJoinedToRoomIDs , restrictedJoinRuleAllowedRooms (ctx , joinRuleEv )... )
238242 }
239243 }
240244
@@ -308,7 +312,7 @@ func authorisedUser(ctx context.Context, querier *Queryer, clientCaller *userapi
308312 } else if rule == spec .Public || rule == spec .Knock {
309313 allowed = true
310314 } else if rule == spec .Restricted {
311- allowedRoomIDs := restrictedJoinRuleAllowedRooms (ctx , joinRuleEv , "m.room_membership" )
315+ allowedRoomIDs := restrictedJoinRuleAllowedRooms (ctx , joinRuleEv )
312316 // check parent is in the allowed set
313317 for _ , a := range allowedRoomIDs {
314318 if * parentRoomID == a {
@@ -342,6 +346,7 @@ func authorisedUser(ctx context.Context, querier *Queryer, clientCaller *userapi
342346 return false , false
343347}
344348
349+ // helper function to fetch a state event
345350func stateEvent (ctx context.Context , querier * Queryer , roomID spec.RoomID , evType , stateKey string ) * types.HeaderedEvent {
346351 var queryRes roomserver.QueryCurrentStateResponse
347352 tuple := gomatrixserverlib.StateKeyTuple {
@@ -358,6 +363,7 @@ func stateEvent(ctx context.Context, querier *Queryer, roomID spec.RoomID, evTyp
358363 return queryRes .StateEvents [tuple ]
359364}
360365
366+ // returns true if the current server is participating in the provided room
361367func roomExists (ctx context.Context , querier * Queryer , roomID spec.RoomID ) bool {
362368 var queryRes roomserver.QueryServerJoinedToRoomResponse
363369 err := querier .QueryServerJoinedToRoom (ctx , & roomserver.QueryServerJoinedToRoomRequest {
@@ -473,6 +479,7 @@ func childReferences(querier *Queryer, suggestedOnly bool, roomID spec.RoomID) (
473479 return el , nil
474480}
475481
482+ // fetch public room information for provided room
476483func publicRoomsChunk (ctx context.Context , querier * Queryer , roomID spec.RoomID ) * fclient.PublicRoom {
477484 pubRooms , err := roomserver .PopulatePublicRooms (ctx , []string {roomID .String ()}, querier )
478485 if err != nil {
@@ -498,7 +505,8 @@ func stripped(ev gomatrixserverlib.PDU) *fclient.MSC2946StrippedEvent {
498505 }
499506}
500507
501- func restrictedJoinRuleAllowedRooms (ctx context.Context , joinRuleEv * types.HeaderedEvent , allowType string ) (allows []spec.RoomID ) {
508+ // given join_rule event, return list of rooms where membership of that room allows joining.
509+ func restrictedJoinRuleAllowedRooms (ctx context.Context , joinRuleEv * types.HeaderedEvent ) (allows []spec.RoomID ) {
502510 rule , _ := joinRuleEv .JoinRule ()
503511 if rule != spec .Restricted {
504512 return nil
@@ -509,7 +517,7 @@ func restrictedJoinRuleAllowedRooms(ctx context.Context, joinRuleEv *types.Heade
509517 return nil
510518 }
511519 for _ , allow := range jrContent .Allow {
512- if allow .Type == allowType {
520+ if allow .Type == spec . MRoomMembership {
513521 allowedRoomID , err := spec .NewRoomID (allow .RoomID )
514522 if err != nil {
515523 util .GetLogger (ctx ).Warnf ("invalid room ID '%s' found in join_rule on room %s: %s" , allow .RoomID , joinRuleEv .RoomID (), err )
0 commit comments