Skip to content
This repository was archived by the owner on Nov 25, 2024. It is now read-only.

Commit ad1de7d

Browse files
committed
add comments
1 parent 97f2bef commit ad1de7d

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

clientapi/routing/room_hierarchy.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,20 @@ import (
2929
log "github.com/sirupsen/logrus"
3030
)
3131

32+
// For storing pagination information for room hierarchies
3233
type RoomHierarchyPaginationCache struct {
3334
cache map[string]roomserverAPI.RoomHierarchyWalker
3435
mu sync.Mutex
3536
}
3637

38+
// Create a new, empty, pagination cache.
3739
func NewRoomHierarchyPaginationCache() RoomHierarchyPaginationCache {
3840
return RoomHierarchyPaginationCache{
3941
cache: map[string]roomserverAPI.RoomHierarchyWalker{},
4042
}
4143
}
4244

45+
// Get a cached page, or nil if there is no associated page in the cache.
4346
func (c *RoomHierarchyPaginationCache) Get(token string) *roomserverAPI.RoomHierarchyWalker {
4447
c.mu.Lock()
4548
defer c.mu.Unlock()
@@ -51,6 +54,7 @@ func (c *RoomHierarchyPaginationCache) Get(token string) *roomserverAPI.RoomHier
5154
}
5255
}
5356

57+
// Add a cache line to the pagination cache.
5458
func (c *RoomHierarchyPaginationCache) AddLine(line roomserverAPI.RoomHierarchyWalker) string {
5559
c.mu.Lock()
5660
defer c.mu.Unlock()
@@ -166,6 +170,7 @@ func QueryRoomHierarchy(req *http.Request, device *userapi.Device, roomIDStr str
166170

167171
}
168172

173+
// Success response for /_matrix/client/v1/rooms/{roomID}/hierarchy
169174
type RoomHierarchyClientResponse struct {
170175
Rooms []fclient.MSC2946Room `json:"rooms"`
171176
NextBatch string `json:"next_batch,omitempty"`

roomserver/api/api.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ type QueryEventsAPI interface {
125125
}
126126

127127
type QueryRoomHierarchyAPI interface {
128+
// Traverse the room hierarchy using the provided walker up to the provided limit,
129+
// returning a new walker which can be used to fetch the next page.
130+
//
131+
// If limit is -1, this is treated as no limit, and the entire hierarchy will be traversed.
132+
//
133+
// If returned walker is nil, then there are no more rooms left to traverse. This method does not modify the provided walker, so it
134+
// can be cached.
128135
QueryNextRoomHierarchyPage(ctx context.Context, walker RoomHierarchyWalker, limit int) ([]fclient.MSC2946Room, *RoomHierarchyWalker, error)
129136
}
130137

roomserver/api/query.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,8 @@ type QueryRoomHierarchyRequest struct {
515515
//
516516
// Used for implementing space summaries / room hierarchies
517517
//
518-
// Use NewRoomHierarchyWalker on the roomserver API to construct this.
518+
// Use NewRoomHierarchyWalker to construct this, and QueryNextRoomHierarchyPage on the roomserver API
519+
// to traverse the room hierarchy.
519520
type RoomHierarchyWalker struct {
520521
RootRoomID spec.RoomID
521522
Caller types.DeviceOrServerName
@@ -532,6 +533,9 @@ type RoomHierarchyWalkerQueuedRoom struct {
532533
Vias []string // vias to query this room by
533534
}
534535

536+
// Create a new room hierarchy walker, starting from the provided root room ID.
537+
//
538+
// Use the resulting struct with QueryNextRoomHierarchyPage on the roomserver API to traverse the room hierarchy.
535539
func NewRoomHierarchyWalker(caller types.DeviceOrServerName, roomID spec.RoomID, suggestedOnly bool, maxDepth int) RoomHierarchyWalker {
536540
walker := RoomHierarchyWalker{
537541
RootRoomID: roomID,
@@ -549,17 +553,21 @@ func NewRoomHierarchyWalker(caller types.DeviceOrServerName, roomID spec.RoomID,
549553
return walker
550554
}
551555

556+
// A set of room IDs.
552557
type RoomSet map[spec.RoomID]struct{}
553558

559+
// Create a new empty room set.
554560
func NewRoomSet() RoomSet {
555561
return RoomSet{}
556562
}
557563

564+
// Check if a room ID is in a room set.
558565
func (s RoomSet) Contains(val spec.RoomID) bool {
559566
_, ok := s[val]
560567
return ok
561568
}
562569

570+
// Add a room ID to a room set.
563571
func (s RoomSet) Add(val spec.RoomID) {
564572
s[val] = struct{}{}
565573
}

roomserver/internal/query/query_room_hierarchy.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
4549
func (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
345350
func 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
361367
func 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
476483
func 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

Comments
 (0)