diff options
Diffstat (limited to 'ModAS.Server/Controllers/DebugController.cs')
-rw-r--r-- | ModAS.Server/Controllers/DebugController.cs | 94 |
1 files changed, 65 insertions, 29 deletions
diff --git a/ModAS.Server/Controllers/DebugController.cs b/ModAS.Server/Controllers/DebugController.cs index d3c7ad0..f0fe91e 100644 --- a/ModAS.Server/Controllers/DebugController.cs +++ b/ModAS.Server/Controllers/DebugController.cs @@ -1,14 +1,17 @@ +using System.Collections.Frozen; using ArcaneLibs.Extensions; +using Elastic.Apm; +using Elastic.Apm.Api; using LibMatrix; using LibMatrix.Homeservers; using Microsoft.AspNetCore.Mvc; using ModAS.Server.Services; using MxApiExtensions.Services; -namespace WebApplication1.Controllers; +namespace ModAS.Server.Controllers; [ApiController] -public class DebugController(ModASConfiguration config, AuthenticatedHomeserverProviderService authHsProvider) : ControllerBase { +public class DebugController(ModASConfiguration config, UserProviderService authHsProvider, RoomContextService roomContextService) : ControllerBase { [HttpGet("/_matrix/_modas/debug")] public IActionResult Index() { return Ok(new { @@ -29,40 +32,73 @@ public class DebugController(ModASConfiguration config, AuthenticatedHomeserverP [HttpGet("/_matrix/_modas/debug/test_locate_users")] public async IAsyncEnumerable<string> TestLocateUsers([FromQuery] string startUser) { - List<string> foundUsers = [startUser], processedRooms = new List<string>(); + List<AuthenticatedHomeserverGeneric> foundUsers = (await authHsProvider.GetValidUsers()).Select(x=>x.Value).ToList(); + if(!foundUsers.Any(x=>x.WhoAmI.UserId == startUser)) { + foundUsers.Add(await authHsProvider.GetImpersonatedHomeserver(startUser)); + } + + List<string> processedRooms = [], processedUsers = []; var foundNew = true; while (foundNew) { + var span1 = currentTransaction.StartSpan("iterateUsers", ApiConstants.TypeApp); foundNew = false; - foreach (var user in foundUsers.ToList()) { - AuthenticatedHomeserverGeneric? ahs = null; - try { - ahs = await authHsProvider.GetImpersonatedHomeserver(user); - await ahs.GetJoinedRooms(); - } - catch (MatrixException e) { - if (e is { ErrorCode: "M_FORBIDDEN" }) continue; - throw; - } + var usersToProcess = foundUsers.Where(x => !processedUsers.Any(y=>x.WhoAmI.UserId == y)).ToFrozenSet(); + Console.WriteLine($"Got {usersToProcess.Count} users: {string.Join(", ", usersToProcess)}"); - if(ahs is null) continue; - var rooms = await ahs.GetJoinedRooms(); - Console.WriteLine($"Got {rooms.Count} rooms"); - rooms.RemoveAll(r => processedRooms.Contains(r.RoomId)); - processedRooms.AddRange(rooms.Select(r => r.RoomId)); - foundNew = rooms.Count > 0; - Console.WriteLine($"Found {rooms.Count} new rooms"); - - var roomMemberTasks = rooms.Select(r => r.GetMembersListAsync(false)).ToAsyncEnumerable(); - await foreach (var roomMembers in roomMemberTasks) { - Console.WriteLine($"Got {roomMembers.Count} members"); - foreach (var member in roomMembers) { - if (!member.StateKey.EndsWith(':' + config.ServerName)) continue; - if (foundUsers.Contains(member.StateKey)) continue; - foundUsers.Add(member.StateKey); - yield return member.StateKey; + var rooms = usersToProcess.Select(async x => await x.GetJoinedRooms()); + var roomLists = rooms.ToAsyncEnumerable(); + await foreach (var roomList in roomLists) { + if (roomList is null) continue; + foreach (var room in roomList) { + if (processedRooms.Contains(room.RoomId)) continue; + processedRooms.Add(room.RoomId); + var roomMembers = await room.GetMembersListAsync(false); + foreach (var roomMember in roomMembers) { + if (roomMember.StateKey.EndsWith(':' + config.ServerName) && !foundUsers.Any(x=>x.WhoAmI.UserId == roomMember.StateKey)) { + foundUsers.Add(await authHsProvider.GetImpersonatedHomeserver(roomMember.StateKey)); + foundNew = true; + yield return roomMember.StateKey; + } } } } + + // await foreach (var task in tasks) { + // if (task is null) continue; + // foreach (var user in task) { + // if (foundUsers.Contains(user)) continue; + // foundUsers.Add(user); + // foundNew = true; + // yield return user; + // } + // } + + span1.End(); } } + + [HttpGet("/_matrix/_modas/debug/room_contexts")] + public IActionResult RoomContexts() { + return Ok(roomContextService.RoomContexts.Values); + } + + [HttpGet("/_matrix/_modas/debug/room_contexts/{roomId}")] + public async Task<IActionResult> RoomContext(string roomId) { + var roomContext = await roomContextService.GetRoomContext(roomId); + if (roomContext is null) return NotFound("Room not found"); + return Ok(roomContext); + } + + [HttpGet("/_matrix/_modas/debug/room_contexts/by_user/{userId}")] + public async IAsyncEnumerable<RoomContextService.RoomContext> RoomContextByUser(string userId) { + var user = await authHsProvider.GetImpersonatedHomeserver(userId); + var rooms = await user.GetJoinedRooms(); + var contexts = rooms.Select(x => roomContextService.GetRoomContext(x.RoomId)).ToAsyncEnumerable(); + await foreach (var context in contexts) { + if (context is null) continue; + yield return context; + } + } + + private static ITransaction currentTransaction => Agent.Tracer.CurrentTransaction; } \ No newline at end of file |