summary refs log tree commit diff
path: root/ModAS.Server/Controllers/DebugController.cs
blob: 39bff658630568dfccbc7ed2ecc0d05aeae3d380 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using ArcaneLibs.Extensions;
using LibMatrix;
using LibMatrix.Homeservers;
using Microsoft.AspNetCore.Mvc;
using ModAS.Server.Services;
using MxApiExtensions.Services;

namespace WebApplication1.Controllers;

[ApiController]
public class DebugController(ModASConfiguration config, AuthenticatedHomeserverProviderService authHsProvider) : ControllerBase {
    [HttpGet("/_matrix/_modas/debug")]
    public IActionResult Index() {
        return Ok(new {
            Request = Request.Headers,
            Response = Response.Headers
        });
    }

    [HttpGet("/_matrix/_modas/debug/config")]
    public IActionResult Config() {
        return Ok(config);
    }

    [HttpGet("/_matrix/_modas/debug/known_users")]
    public IActionResult KnownUsers() {
        return Ok(authHsProvider.KnownUsers.Keys);
    }

    [HttpGet("/_matrix/_modas/debug/test_locate_users")]
    public async IAsyncEnumerable<string> TestLocateUsers([FromQuery] string startUser) {
        List<string> foundUsers = [startUser], processedRooms = new List<string>();
        var foundNew = true;
        while (foundNew) {
            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;
                }

                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;
                    }
                }
            }
        }
    }
}