Implemented Output Blocked

This commit is contained in:
Casper Sparre 2026-02-19 21:28:31 +01:00
parent 1cf3431832
commit bad6cc740f
2 changed files with 107 additions and 0 deletions

View File

@ -52,6 +52,91 @@ public class CajetanApiService(IAuthService authService, IConfigService configSe
return null;
}
public async Task<Dictionary<string, long>> GetUsersWithProgressAsync(string typeDisplay, string endpoint, string? typeParam, bool offsetByCount)
{
Dictionary<string, long> usersOfType = await _eventHandler.WithStatusAsync(
statusMessage: $"Getting {typeDisplay} Users",
work: FetchAsync
);
return usersOfType;
async Task<Dictionary<string, long>> FetchAsync(IStatusReporter statusReporter)
{
Dictionary<string, long> users = [];
int limit = 50;
int offset = 0;
bool includeRestricted = true;
Dictionary<string, string> getParams = new()
{
["format"] = "infinite",
["limit"] = limit.ToString(),
["offset"] = offset.ToString()
};
if (!string.IsNullOrWhiteSpace(typeParam))
getParams["type"] = typeParam;
try
{
Log.Debug("Calling GetUsersWithProgress");
HttpClient client = GetHttpClient();
bool isLastLoop = false;
while (true)
{
string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, client);
if (string.IsNullOrWhiteSpace(body))
break;
SubscriptionDtos.SubscriptionsDto? subscriptions = DeserializeJson<SubscriptionDtos.SubscriptionsDto>(body, s_mJsonSerializerSettings);
if (subscriptions?.List is null)
break;
foreach (SubscriptionDtos.ListItemDto item in subscriptions.List)
{
if (string.IsNullOrWhiteSpace(item?.Username))
continue;
if (users.ContainsKey(item.Username))
continue;
bool isRestricted = item.IsRestricted ?? false;
bool isRestrictedButAllowed = isRestricted && includeRestricted;
if (!isRestricted || isRestrictedButAllowed)
users.Add(item.Username, item.Id);
}
statusReporter.ReportStatus($"[blue]Getting {typeDisplay} Users\n[/] [blue]Found {users.Count}[/]");
if (isLastLoop)
break;
if (!subscriptions.HasMore || subscriptions.List.Count == 0)
isLastLoop = true;
offset += offsetByCount
? subscriptions.List.Count
: limit;
getParams["offset"] = offset.ToString();
}
}
catch (Exception ex)
{
ExceptionLoggerHelper.LogException(ex);
}
return users;
}
}
public new async Task<MessageEntities.MessageCollection> GetMessages(string endpoint, string folder, IStatusReporter statusReporter)
{
(bool couldExtract, long userId) = ExtractUserId(endpoint);

View File

@ -225,7 +225,29 @@ internal class Worker(IServiceProvider serviceProvider)
private async Task OutputBlockedUsersAsync()
{
const string OUTPUT_FILE_BLOCKED = "blocked-users.json";
const string OUTPUT_FILE_EXPIRED = "expired-users.json";
await GetUsersAsync("Blocked", "/users/blocked", OUTPUT_FILE_BLOCKED);
await GetUsersAsync("Expired", "/subscriptions/subscribes", OUTPUT_FILE_EXPIRED, typeParam: "expired", offsetByCount: false);
async Task GetUsersAsync(string typeDisplay, string uri, string outputFile, string? typeParam = null, bool offsetByCount = true)
{
Dictionary<string, long> users = await _apiService.GetUsersWithProgressAsync(typeDisplay, uri, typeParam, offsetByCount);
Console.WriteLine();
if (users is null || users.Count == 0)
{
AnsiConsole.Markup($"[green]No {typeDisplay} Users found.\n[/]");
}
else
{
AnsiConsole.Markup($"[green]Found {users.Count} {typeDisplay} Users, saving to '{outputFile}'\n[/]");
string json = JsonConvert.SerializeObject(users, Formatting.Indented);
await File.WriteAllTextAsync(outputFile, json);
}
}
}
private async Task UpdateUserInfoAsync()