blob: e9a59f3b29fd85a7fea808fc676221df6f5a9c04 (
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
|
@page "/Users/Delete/{Id}"
@using System.Net.Http.Headers
@using System.Text.Json
@using System.Text.Json.Nodes
@using ArcaneLibs
@using ArcaneLibs.Extensions
@using Spacebar.Models.AdminApi
@using Spacebar.AdminApi.TestClient.Services
@inject Config Config
<h3>UsersDelete - @Id</h3>
Deleted @ChannelDeleteProgress.Sum(x=>x.Value.Deleted) messages so far!
@foreach (var (channel, progress) in ChannelDeleteProgress.Where(x=>x.Value.Deleted != x.Value.Total).OrderByDescending(x=>x.Value.Progress)) {
<div>@channel: @progress.Total total, @progress.Deleted deleted</div>
<progress max="@progress.Total" value="@progress.Deleted"></progress>
}
@if (Done) {
<h1>Done!</h1>
}
@code {
[Parameter]
public required string Id { get; set; }
private Dictionary<string, DeleteProgress> ChannelDeleteProgress { get; set; } = new();
private bool Done { get; set; }
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/Users/{Id}/delete?messageDeleteChunkSize=100");
if (!response.IsSuccessStatusCode) throw new Exception(await response.Content.ReadAsStringAsync());
var content = response.Content.ReadFromJsonAsAsyncEnumerable<AsyncActionResult>();
await foreach (var actionResult in content) {
Console.WriteLine(actionResult.ToJson(indent: false));
switch (actionResult.MessageType) {
case "STATS": {
var data = JsonSerializer.Deserialize<JsonObject>(actionResult.Data.ToJson());
ChannelDeleteProgress = data!["messages_per_channel"]!
.Deserialize<Dictionary<string, int>>()!
.ToDictionary(x=>x.Key, x=>new DeleteProgress { Total = x.Value });
break;
}
case "BULK_DELETED": {
var data = JsonSerializer.Deserialize<JsonObject>(actionResult.Data.ToJson());
ChannelDeleteProgress[data!["channel_id"]!.ToString()].Deleted += data!["deleted"]!.GetValue<int>();
break;
}
default: {
Console.WriteLine($"Unknown message type: {actionResult.MessageType}");
break;
}
}
StateHasChanged();
await Task.Delay(1);
}
Done = true;
StateHasChanged();
}
private class DeleteProgress {
public int Total { get; set; }
public int Deleted { get; set; } = 0;
public float Progress => (float)Deleted / Total;
}
}
|