summary refs log tree commit diff
path: root/extra/admin-api/Utilities/Spacebar.AdminApi.TestClient/Pages/GuildDiscovery.razor
blob: ff233731ee7fe3a1e11d8d6547072ab37b7ab007 (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
@page "/GuildDiscovery"
@using System.Net.Http.Headers
@using System.Text.Json.Nodes
@using ArcaneLibs
@using ArcaneLibs.Blazor.Components
@using Spacebar.AdminApi.TestClient.Services
@using Spacebar.Models.AdminApi
@inject Config Config
<h3>GuildDiscovery</h3>
<LinkButton OnClickAsync="@SaveChangesAsync">Save</LinkButton>

<div class="guild-list">
    @foreach (var guild in _guilds) {
        if (guild == null) continue;

        <div class="@("guild-card " + (guild.DiscoveryExcluded ? "excluded" : ""))">
            <img class="guild-icon" src="@(guild.Icon is null ? $"{Config.CdnUrl}/embed/avatars/0.png" : $"{Config.CdnUrl}/icons/{guild.Id}/{guild.Icon}")" alt="@guild.Name"/>
            @if (guild.Banner is not null) {
                <img class="guild-banner" src="@($"{Config.CdnUrl}/banners/{guild.Id}/{guild.Banner}")" alt="@guild.Name"/>
            }
            <h4>@guild.Name</h4>
            <p>@guild.Description</p>

            <div class="guild-details">
                <div class="guild-stats">
                    <span>@guild.PresenceCount Online</span>
                    <span>@guild.MemberCount Members</span>
                </div>
                <label>Exclude</label>
                <InputCheckbox @bind-Value="@guild.DiscoveryExcluded"/>
                <label>Weight</label>
                <InputNumber @bind-Value="@guild.DiscoveryWeight" style="width: 100px;"/>
            </div>
        </div>
    }
</div>

@code {

    private List<DiscoverableGuildModel?> _guilds = [];

    protected override async Task OnInitializedAsync() {
        var hc = new StreamingHttpClient();
        hc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Config.AccessToken);
        var response = await hc.GetAsync(Config.AdminUrl + $"/_spacebar/admin/discovery/guilds?includeExcluded=true");
        if (!response.IsSuccessStatusCode) throw new Exception(await response.Content.ReadAsStringAsync());
        var content = response.Content.ReadFromJsonAsAsyncEnumerable<DiscoverableGuildModel>();
        await foreach (var guild in content) {
            _guilds.Add(guild);
            // var d = Task.Delay(25);
            await StateHasChangedAsync();
            // await d;
        }
    }

    private async Task StateHasChangedAsync() {
        StateHasChanged();
        await Task.Delay(1);
    }

    protected async Task SaveChangesAsync() {
        var hc = new StreamingHttpClient();
        hc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Config.AccessToken);
        var response = await hc.GetAsync(Config.AdminUrl + $"/_spacebar/admin/discovery/guilds?includeExcluded=true");
        if (!response.IsSuccessStatusCode) throw new Exception(await response.Content.ReadAsStringAsync());
        var content = response.Content.ReadFromJsonAsAsyncEnumerable<DiscoverableGuildModel>();
        await foreach (var guild in content) {
            var matchingGuild = _guilds.FirstOrDefault(g => g!.Id == guild!.Id);
            if (matchingGuild == null) continue;
            JsonObject update = new();

            if (matchingGuild.DiscoveryExcluded != guild.DiscoveryExcluded)
                update["discovery_excluded"] = matchingGuild.DiscoveryExcluded;

            if (matchingGuild.DiscoveryWeight != guild.DiscoveryWeight)
                update["discovery_weight"] = matchingGuild.DiscoveryWeight;

            if (update.Count == 0) continue;
            await hc.PatchAsJsonAsync(Config.AdminUrl + $"/_spacebar/admin/discovery/guilds/{guild.Id}", update);
        }
    }

}