1
0
Fork 0
mirror of https://codeberg.org/june64/mrvc.git synced 2026-01-10 16:06:33 +01:00

introduce configuration options for recursively getting rooms

Introduce option for configuring maximum depth and option for only
including suggested rooms.
This commit is contained in:
June 2025-08-19 02:53:07 +02:00
commit 6309b94498
Signed by: june
SSH key fingerprint: SHA256:o9EAq4Y9N9K0pBQeBTqhSDrND5E7oB+60ZNx0U1yPe0
3 changed files with 38 additions and 12 deletions

View file

@ -24,6 +24,8 @@ type configOptions struct {
token configOption[string] token configOption[string]
rooms configOption[rooms] rooms configOption[rooms]
recursive configOption[bool] recursive configOption[bool]
recursionMaxDepth configOption[int]
recursionSuggestedOnly configOption[bool]
printHomeserverMemberCount configOption[bool] printHomeserverMemberCount configOption[bool]
homeserverVersionInfoTimeout configOption[time.Duration] homeserverVersionInfoTimeout configOption[time.Duration]
} }
@ -33,6 +35,8 @@ type Config struct {
Token string Token string
Rooms rooms Rooms rooms
Recursive bool Recursive bool
RecursionMaxDepth *int
RecursionSuggestedOnly bool
PrintHomeserverMemberCount bool PrintHomeserverMemberCount bool
HomeserverVersionInfoTimeout time.Duration HomeserverVersionInfoTimeout time.Duration
} }
@ -42,6 +46,8 @@ var configOpts = configOptions{
token: configOption[string]{"token", "", "The Matrix access token to use.", "MRVC_TOKEN"}, token: configOption[string]{"token", "", "The Matrix access token to use.", "MRVC_TOKEN"},
rooms: configOption[rooms]{"room", []string{}, "The Matrix room to check. The flag can be set multiple times to check multiple rooms.", "MRVC_ROOM"}, rooms: configOption[rooms]{"room", []string{}, "The Matrix room to check. The flag can be set multiple times to check multiple rooms.", "MRVC_ROOM"},
recursive: configOption[bool]{"recursive", false, "Recursively check the child rooms for the given rooms (spaces) as well.", "MRVC_RECURSIVE"}, recursive: configOption[bool]{"recursive", false, "Recursively check the child rooms for the given rooms (spaces) as well.", "MRVC_RECURSIVE"},
recursionMaxDepth: configOption[int]{"recursion-max-depth", 0, "The maximum depth when recursively getting child rooms. Defaults to 0, which gets interpreted as the servers default.", "MRVC_RECURSION_MAX_DEPTH"},
recursionSuggestedOnly: configOption[bool]{"recursion-suggested-only", false, "Only include the suggested children, when recursively getting child rooms.", "MRVC_RECURSION_SUGGESTED_ONLY"},
printHomeserverMemberCount: configOption[bool]{"print-homeserver-member-count", false, "Print the member count for each homeserver.", "MRVC_PRINT_HOMESERVER_MEMBER_COUNT"}, printHomeserverMemberCount: configOption[bool]{"print-homeserver-member-count", false, "Print the member count for each homeserver.", "MRVC_PRINT_HOMESERVER_MEMBER_COUNT"},
homeserverVersionInfoTimeout: configOption[time.Duration]{"homeserver-version-info-timeout", time.Second * 5, "Timeout for getting the homeservers version information per homeserver.", "MRVC_HOMESERVER_VERSION_INFO_TIMEOUT"}, homeserverVersionInfoTimeout: configOption[time.Duration]{"homeserver-version-info-timeout", time.Second * 5, "Timeout for getting the homeservers version information per homeserver.", "MRVC_HOMESERVER_VERSION_INFO_TIMEOUT"},
} }
@ -89,6 +95,8 @@ func (configOpt configOption[T]) getConfigValueWithError(configFlag *T, visitedF
var userIdFlag = flag.String(configOpts.userID.getFlagArgs()) var userIdFlag = flag.String(configOpts.userID.getFlagArgs())
var tokenFlag = flag.String(configOpts.token.getFlagArgs()) var tokenFlag = flag.String(configOpts.token.getFlagArgs())
var recursiveFlag = flag.Bool(configOpts.recursive.getFlagArgs()) var recursiveFlag = flag.Bool(configOpts.recursive.getFlagArgs())
var recursionMaxDepthFlag = flag.Int(configOpts.recursionMaxDepth.getFlagArgs())
var recursionSuggestedOnlyFlag = flag.Bool(configOpts.recursionSuggestedOnly.getFlagArgs())
var printHomeserverMemberCountFlag = flag.Bool(configOpts.printHomeserverMemberCount.getFlagArgs()) var printHomeserverMemberCountFlag = flag.Bool(configOpts.printHomeserverMemberCount.getFlagArgs())
var homeserverVersionInfoTimeoutFlag = flag.Duration(configOpts.homeserverVersionInfoTimeout.getFlagArgs()) var homeserverVersionInfoTimeoutFlag = flag.Duration(configOpts.homeserverVersionInfoTimeout.getFlagArgs())
@ -130,6 +138,28 @@ func Get() Config {
} }
return parsedEnvVar return parsedEnvVar
}) })
givenRecursionMaxDepth := configOpts.recursionMaxDepth.getConfigValueWithDefault(recursionMaxDepthFlag, visitedFlags, func(envVar string) int {
parsedEnvVar, err := strconv.Atoi(envVar)
if err != nil {
log.Printf("Error parsing %s:\n", configOpts.recursionMaxDepth.envVarName)
log.Fatal(err)
}
return parsedEnvVar
})
// Since we actually want an *int, interpret 0 as nil as described in the help text.
if givenRecursionMaxDepth != 0 {
config.RecursionMaxDepth = &givenRecursionMaxDepth
} else {
config.RecursionMaxDepth = nil
}
config.RecursionSuggestedOnly = configOpts.recursionSuggestedOnly.getConfigValueWithDefault(recursionSuggestedOnlyFlag, visitedFlags, func(envVar string) bool {
parsedEnvVar, err := strconv.ParseBool(envVar)
if err != nil {
log.Printf("Error parsing %s:\n", configOpts.recursionSuggestedOnly.envVarName)
log.Fatal(err)
}
return parsedEnvVar
})
config.PrintHomeserverMemberCount = configOpts.printHomeserverMemberCount.getConfigValueWithDefault(printHomeserverMemberCountFlag, visitedFlags, func(envVar string) bool { config.PrintHomeserverMemberCount = configOpts.printHomeserverMemberCount.getConfigValueWithDefault(printHomeserverMemberCountFlag, visitedFlags, func(envVar string) bool {
parsedEnvVar, err := strconv.ParseBool(envVar) parsedEnvVar, err := strconv.ParseBool(envVar)
if err != nil { if err != nil {

View file

@ -73,7 +73,7 @@ func main() {
fclient.WithTimeout(config.HomeserverVersionInfoTimeout), fclient.WithTimeout(config.HomeserverVersionInfoTimeout),
) )
roomInfoTree := roominfotree.Get(config.Rooms, config.Recursive, client, federationClient) roomInfoTree := roominfotree.Get(config.Rooms, config.Recursive, config.RecursionMaxDepth, config.RecursionSuggestedOnly, client, federationClient)
for roomID, roomInfo := range roomInfoTree { for roomID, roomInfo := range roomInfoTree {
fmt.Println("Room:") fmt.Println("Room:")

View file

@ -97,16 +97,12 @@ func resolveRooms(rooms []string, client *mautrix.Client) ([]id.RoomID, map[id.R
return roomIDs, aliasesByRoomID return roomIDs, aliasesByRoomID
} }
func getChildRoomChunks(roomID id.RoomID, client *mautrix.Client) []*mautrix.ChildRoomsChunk { func getChildRoomChunks(roomID id.RoomID, recursionMaxDepth *int, recursionSuggestedOnly bool, client *mautrix.Client) []*mautrix.ChildRoomsChunk {
var maxDepth *int
maxDepth = nil
suggestedOnly := false
var childRoomChunks []*mautrix.ChildRoomsChunk var childRoomChunks []*mautrix.ChildRoomsChunk
hierarchyRequest := mautrix.ReqHierarchy{ hierarchyRequest := mautrix.ReqHierarchy{
MaxDepth: maxDepth, MaxDepth: recursionMaxDepth,
SuggestedOnly: suggestedOnly, SuggestedOnly: recursionSuggestedOnly,
} }
finishedRequesting := false finishedRequesting := false
for !finishedRequesting { for !finishedRequesting {
@ -132,13 +128,13 @@ func getChildRoomChunks(roomID id.RoomID, client *mautrix.Client) []*mautrix.Chi
return childRoomChunks return childRoomChunks
} }
func addChildRooms(givenRoomIDs []id.RoomID, client *mautrix.Client) []id.RoomID { func addChildRooms(givenRoomIDs []id.RoomID, recursionMaxDepth *int, recursionSuggestedOnly bool, client *mautrix.Client) []id.RoomID {
roomIDSet := make(map[id.RoomID]bool) roomIDSet := make(map[id.RoomID]bool)
for _, givenRoomID := range givenRoomIDs { for _, givenRoomID := range givenRoomIDs {
roomIDSet[givenRoomID] = true roomIDSet[givenRoomID] = true
childRoomChunks := getChildRoomChunks(givenRoomID, client) childRoomChunks := getChildRoomChunks(givenRoomID, recursionMaxDepth, recursionSuggestedOnly, client)
for _, childRoomChunk := range childRoomChunks { for _, childRoomChunk := range childRoomChunks {
roomIDSet[childRoomChunk.PublicRoomInfo.RoomID] = true roomIDSet[childRoomChunk.PublicRoomInfo.RoomID] = true
} }
@ -228,10 +224,10 @@ func getServerVersionInfoByHomeserver(homeservers []string, federationClient *fc
return serverVersionInfoByHomeserver return serverVersionInfoByHomeserver
} }
func Get(rooms []string, recursive bool, client *mautrix.Client, federationClient *fclient.Client) RoomInfoTree { func Get(rooms []string, recursive bool, recursionMaxDepth *int, recursionSuggestedOnly bool, client *mautrix.Client, federationClient *fclient.Client) RoomInfoTree {
roomIDs, aliasesByRoomID := resolveRooms(rooms, client) roomIDs, aliasesByRoomID := resolveRooms(rooms, client)
if recursive { if recursive {
roomIDs = addChildRooms(roomIDs, client) roomIDs = addChildRooms(roomIDs, recursionMaxDepth, recursionSuggestedOnly, client)
} }
membersByHomeserverByRoomID := getMembersByHomeserverByRoomID(roomIDs, client) membersByHomeserverByRoomID := getMembersByHomeserverByRoomID(roomIDs, client)
homeservers := getHomeservers(membersByHomeserverByRoomID) homeservers := getHomeservers(membersByHomeserverByRoomID)