about summary refs log tree commit diff
path: root/LibMatrix/Interfaces/IHomeServer.cs
blob: 5e7e374f4fedc92b89d4452d2e0f2b842654a01c (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
using System.Net.Http.Json;
using LibMatrix.Extensions;
using LibMatrix.StateEventTypes.Spec;

namespace LibMatrix.Interfaces;

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

    public MatrixHttpClient _httpClient { get; set; } = new();

    public async Task<ProfileResponseEventData> GetProfile(string mxid) {
        if(mxid is null) throw new ArgumentNullException(nameof(mxid));
        if (_profileCache.ContainsKey(mxid)) {
            if (_profileCache[mxid] is SemaphoreSlim s) await s.WaitAsync();
            if (_profileCache[mxid] is ProfileResponseEventData p) return p;
        }
        _profileCache[mxid] = new SemaphoreSlim(1);

        var resp = await _httpClient.GetAsync($"/_matrix/client/v3/profile/{mxid}");
        var data = await resp.Content.ReadFromJsonAsync<ProfileResponseEventData>();
        if (!resp.IsSuccessStatusCode) Console.WriteLine("Profile: " + data);
        _profileCache[mxid] = data;

        return data;
    }
}