about summary refs log tree commit diff
path: root/LibMatrix/Services/HomeserverProviderService.cs
blob: a43f518654a42177383d73da199b7f2b4a7bbb1d (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
69
70
71
72
73
74
75
using System.Net.Http.Json;
using ArcaneLibs.Extensions;
using LibMatrix.Homeservers;
using LibMatrix.Responses;
using Microsoft.Extensions.Logging;

namespace LibMatrix.Services;

public class HomeserverProviderService {
    private readonly ILogger<HomeserverProviderService> _logger;
    private readonly HomeserverResolverService _homeserverResolverService;

    public HomeserverProviderService(ILogger<HomeserverProviderService> logger, HomeserverResolverService homeserverResolverService) {
        _logger = logger;
        _homeserverResolverService = homeserverResolverService;
    }

    private static Dictionary<string, SemaphoreSlim> _authenticatedHomeserverSemaphore = new();
    private static Dictionary<string, AuthenticatedHomeserverGeneric> _authenticatedHomeserverCache = new();

    private static Dictionary<string, SemaphoreSlim> _remoteHomeserverSemaphore = new();
    private static Dictionary<string, RemoteHomeserver> _remoteHomeserverCache = new();

    public async Task<AuthenticatedHomeserverGeneric> GetAuthenticatedWithToken(string homeserver, string accessToken, string? proxy = null) {
        var sem = _authenticatedHomeserverSemaphore.GetOrCreate(homeserver + accessToken, _ => new SemaphoreSlim(1, 1));
        await sem.WaitAsync();
        lock (_authenticatedHomeserverCache) {
            if (_authenticatedHomeserverCache.ContainsKey(homeserver + accessToken)) {
                sem.Release();
                return _authenticatedHomeserverCache[homeserver + accessToken];
            }
        }

        // var domain = proxy ?? (await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver)).client;

        var rhs = await RemoteHomeserver.Create(homeserver);
        var serverVersion = await rhs.GetServerVersionAsync();
        

        AuthenticatedHomeserverGeneric hs;
        if (true) {
            hs = await AuthenticatedHomeserverGeneric.Create<AuthenticatedHomeserverMxApiExtended>(homeserver, accessToken);
        }
        else {
            hs = await AuthenticatedHomeserverGeneric.Create<AuthenticatedHomeserverSynapse>(homeserver, accessToken);
        }

        // (() => hs.WhoAmI) = (await hs._httpClient.GetFromJsonAsync<WhoAmIResponse>("/_matrix/client/v3/account/whoami"))!;

        lock (_authenticatedHomeserverCache)
            _authenticatedHomeserverCache[homeserver + accessToken] = hs;
        sem.Release();

        return hs;
    }

    public async Task<RemoteHomeserver> GetRemoteHomeserver(string homeserver, string? proxy = null) {
        var hs = await RemoteHomeserver.Create(proxy ?? homeserver);
        // hs._httpClient.Dispose();
        // hs._httpClient = new MatrixHttpClient { BaseAddress = new Uri(hs.ServerName) };
        // hs._httpClient.Timeout = TimeSpan.FromSeconds(120);
        return hs;
    }

    public async Task<LoginResponse> Login(string homeserver, string user, string password, string? proxy = null) {
        var hs = await GetRemoteHomeserver(homeserver, proxy);
        var payload = new LoginRequest {
            Identifier = new LoginRequest.LoginIdentifier { User = user },
            Password = password
        };
        var resp = await hs.ClientHttpClient.PostAsJsonAsync("/_matrix/client/v3/login", payload);
        var data = await resp.Content.ReadFromJsonAsync<LoginResponse>();
        return data!;
    }
}