@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
GuildDiscovery
Save
@foreach (var guild in _guilds) {
if (guild == null) continue;

@if (guild.Banner is not null) {

}
@guild.Name
@guild.Description
@guild.PresenceCount Online
@guild.MemberCount Members
}
@code {
private List _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();
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();
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);
}
}
}