about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs
blob: c6d788c2165d8b37f9ad88cca9a1f6024a7592d3 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System.Net.Http.Json;
using System.Text.Json;
using MatrixRoomUtils.Core.Extensions;
using MatrixRoomUtils.Core.Responses;

namespace MatrixRoomUtils.Core.Interfaces;

public class IHomeServer
{
    private Dictionary<string, ProfileResponse?> _profileCache = new();
    public string HomeServerDomain { get; set; }
    public string FullHomeServerDomain { get; set; }

    private protected HttpClient _httpClient { get; set; } = new();

    public async Task<string> ResolveHomeserverFromWellKnown(string homeserver)
    {
        if (RuntimeCache.HomeserverResolutionCache.ContainsKey(homeserver))
        {
            if (RuntimeCache.HomeserverResolutionCache[homeserver].ResolutionTime < DateTime.Now.AddHours(1))
            {
                Console.WriteLine($"Found cached homeserver: {RuntimeCache.HomeserverResolutionCache[homeserver].Result}");
                return RuntimeCache.HomeserverResolutionCache[homeserver].Result;
            }
            RuntimeCache.HomeserverResolutionCache.Remove(homeserver);
        }
        //throw new NotImplementedException();

        string result = null;
        Console.WriteLine($"Resolving homeserver: {homeserver}");
        if (!homeserver.StartsWith("http")) homeserver = "https://" + homeserver;
        if (await _httpClient.CheckSuccessStatus($"{homeserver}/.well-known/matrix/client"))
        {
            Console.WriteLine($"Got successful response for client well-known...");
            var resp = await _httpClient.GetFromJsonAsync<JsonElement>($"{homeserver}/.well-known/matrix/client");
            Console.WriteLine($"Response: {resp.ToString()}");
            var hs = resp.GetProperty("m.homeserver").GetProperty("base_url").GetString();
            result = hs;
        }
        else
        {
            Console.WriteLine($"No client well-known...");
            if (await _httpClient.CheckSuccessStatus($"{homeserver}/.well-known/matrix/server"))
            {
                var resp = await _httpClient.GetFromJsonAsync<JsonElement>($"{homeserver}/.well-known/matrix/server");
                var hs = resp.GetProperty("m.server").GetString();
                result = hs;
            }
            else
            {
                Console.WriteLine($"No server well-known...");
                if (await _httpClient.CheckSuccessStatus($"{homeserver}/_matrix/client/versions")) result = homeserver;
                else
                {
                    Console.WriteLine("No homeserver on shortname...");
                    if (await _httpClient.CheckSuccessStatus($"{homeserver.Replace("//", "//matrix.")}/_matrix/client/versions")) result = homeserver.Replace("//", "//matrix.");
                    else Console.WriteLine($"Failed to resolve homeserver, not on {homeserver}, nor do client or server well-knowns exist!");
                }
            }
        }

        if (result != null)
        {
            Console.WriteLine($"Resolved homeserver: {homeserver} -> {result}");
            RuntimeCache.HomeserverResolutionCache.TryAdd(homeserver, new()
            {
                Result = result,
                ResolutionTime = DateTime.Now
            });
            return result;
        }
        throw new InvalidDataException($"Failed to resolve homeserver, not on {homeserver}, nor do client or server well-knowns exist!");
    }
    public async Task<ProfileResponse> GetProfile(string mxid, bool debounce = false, bool cache = true)
    {
        if (cache)
        {
            if(debounce) await Task.Delay(Random.Shared.Next(100, 500));
            if (_profileCache.ContainsKey(mxid))
            {
                while (_profileCache[mxid] == null)
                {
                    Console.WriteLine($"Waiting for profile cache for {mxid}, currently {_profileCache[mxid]?.ToJson()} within {_profileCache.Count} profiles...");
                    await Task.Delay(Random.Shared.Next(50, 500));
                }
                return _profileCache[mxid];
            }
        }

        _profileCache.Add(mxid, null);
        var resp = await _httpClient.GetAsync($"/_matrix/client/r0/profile/{mxid}");
        var data = await resp.Content.ReadFromJsonAsync<JsonElement>();
        if(!resp.IsSuccessStatusCode) Console.WriteLine("Profile: " + data.ToString());
        var profile = data.Deserialize<ProfileResponse>();
        _profileCache[mxid] = profile;
        return profile;
    }
}