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

introduce option for recursively checking all child rooms as well

This commit is contained in:
June 2025-08-19 02:16:57 +02:00
commit 919127e255
Signed by: june
SSH key fingerprint: SHA256:o9EAq4Y9N9K0pBQeBTqhSDrND5E7oB+60ZNx0U1yPe0
4 changed files with 72 additions and 2 deletions

View file

@ -50,6 +50,7 @@ Version Support:
- Show member count broken down by maximum room version, Matrix server implementation and version and optionally homeserver name. - Show member count broken down by maximum room version, Matrix server implementation and version and optionally homeserver name.
- Check multiple rooms at once by setting the `--room` flag multiple times. - Check multiple rooms at once by setting the `--room` flag multiple times.
- Recursively check all the child rooms for the given rooms (spaces) by setting the `--recursive` flag.
## Usage ## Usage

View file

@ -23,6 +23,7 @@ type configOptions struct {
userID configOption[string] userID configOption[string]
token configOption[string] token configOption[string]
rooms configOption[rooms] rooms configOption[rooms]
recursive configOption[bool]
printHomeserverMemberCount configOption[bool] printHomeserverMemberCount configOption[bool]
homeserverVersionInfoTimeout configOption[time.Duration] homeserverVersionInfoTimeout configOption[time.Duration]
} }
@ -31,6 +32,7 @@ type Config struct {
UserID string UserID string
Token string Token string
Rooms rooms Rooms rooms
Recursive bool
PrintHomeserverMemberCount bool PrintHomeserverMemberCount bool
HomeserverVersionInfoTimeout time.Duration HomeserverVersionInfoTimeout time.Duration
} }
@ -39,6 +41,7 @@ var configOpts = configOptions{
userID: configOption[string]{"user-id", "", "The Matrix user ID to use.", "MRVC_USER_ID"}, userID: configOption[string]{"user-id", "", "The Matrix user ID to use.", "MRVC_USER_ID"},
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"},
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"},
} }
@ -85,6 +88,7 @@ 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 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())
@ -118,6 +122,14 @@ func Get() Config {
if err != nil { if err != nil {
log.Fatal("A Matrix room must be provided.") log.Fatal("A Matrix room must be provided.")
} }
config.Recursive = configOpts.recursive.getConfigValueWithDefault(recursiveFlag, visitedFlags, func(envVar string) bool {
parsedEnvVar, err := strconv.ParseBool(envVar)
if err != nil {
log.Printf("Error parsing %s:\n", configOpts.recursive.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, client, federationClient) roomInfoTree := roominfotree.Get(config.Rooms, config.Recursive, client, federationClient)
for roomID, roomInfo := range roomInfoTree { for roomID, roomInfo := range roomInfoTree {
fmt.Println("Room:") fmt.Println("Room:")

View file

@ -97,6 +97,60 @@ 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 {
var maxDepth *int
maxDepth = nil
suggestedOnly := false
var childRoomChunks []*mautrix.ChildRoomsChunk
hierarchyRequest := mautrix.ReqHierarchy{
MaxDepth: maxDepth,
SuggestedOnly: suggestedOnly,
}
finishedRequesting := false
for !finishedRequesting {
hierarchyResponse, err := client.Hierarchy(context.Background(), roomID, &hierarchyRequest)
if err != nil {
log.Fatal(err)
}
childRoomChunks = append(childRoomChunks, hierarchyResponse.Rooms...)
if hierarchyResponse.NextBatch == "" {
finishedRequesting = true
} else {
hierarchyRequest = mautrix.ReqHierarchy{
From: hierarchyResponse.NextBatch,
Limit: hierarchyRequest.Limit,
MaxDepth: hierarchyRequest.MaxDepth,
SuggestedOnly: hierarchyRequest.SuggestedOnly,
}
}
}
return childRoomChunks
}
func addChildRooms(givenRoomIDs []id.RoomID, client *mautrix.Client) []id.RoomID {
roomIDSet := make(map[id.RoomID]bool)
for _, givenRoomID := range givenRoomIDs {
roomIDSet[givenRoomID] = true
childRoomChunks := getChildRoomChunks(givenRoomID, client)
for _, childRoomChunk := range childRoomChunks {
roomIDSet[childRoomChunk.PublicRoomInfo.RoomID] = true
}
}
roomIDs := make([]id.RoomID, 0, len(roomIDSet))
for roomID := range roomIDSet {
roomIDs = append(roomIDs, roomID)
}
return roomIDs
}
func getMembersByHomeserverByRoomID(roomIDs []id.RoomID, client *mautrix.Client) map[id.RoomID](map[string]uint) { func getMembersByHomeserverByRoomID(roomIDs []id.RoomID, client *mautrix.Client) map[id.RoomID](map[string]uint) {
joinedMembersByRoomID := make(map[id.RoomID]*mautrix.RespJoinedMembers) joinedMembersByRoomID := make(map[id.RoomID]*mautrix.RespJoinedMembers)
for _, roomID := range roomIDs { for _, roomID := range roomIDs {
@ -174,8 +228,11 @@ func getServerVersionInfoByHomeserver(homeservers []string, federationClient *fc
return serverVersionInfoByHomeserver return serverVersionInfoByHomeserver
} }
func Get(rooms []string, client *mautrix.Client, federationClient *fclient.Client) RoomInfoTree { func Get(rooms []string, recursive bool, client *mautrix.Client, federationClient *fclient.Client) RoomInfoTree {
roomIDs, aliasesByRoomID := resolveRooms(rooms, client) roomIDs, aliasesByRoomID := resolveRooms(rooms, client)
if recursive {
roomIDs = addChildRooms(roomIDs, client)
}
membersByHomeserverByRoomID := getMembersByHomeserverByRoomID(roomIDs, client) membersByHomeserverByRoomID := getMembersByHomeserverByRoomID(roomIDs, client)
homeservers := getHomeservers(membersByHomeserverByRoomID) homeservers := getHomeservers(membersByHomeserverByRoomID)
serverVersionInfoByHomeserver := getServerVersionInfoByHomeserver(homeservers, federationClient) serverVersionInfoByHomeserver := getServerVersionInfoByHomeserver(homeservers, federationClient)