From 3e74ae7b73d3d43c873008d1a16eec1516a5741f Mon Sep 17 00:00:00 2001 From: Casper Sparre Date: Sat, 21 Feb 2026 00:28:54 +0100 Subject: [PATCH] Updated to read both username and userid from list users --- .../Models/Dtos/Lists/ListUsersDto.cs | 8 +++ Cajetan.OF-DL/Properties/GlobalUsings.cs | 1 + Cajetan.OF-DL/Services/CajetanApiService.cs | 58 +++++++++++++++++++ Cajetan.OF-DL/Services/ICajetanApiService.cs | 2 + Cajetan.OF-DL/Worker.cs | 11 ++-- 5 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 Cajetan.OF-DL/Models/Dtos/Lists/ListUsersDto.cs diff --git a/Cajetan.OF-DL/Models/Dtos/Lists/ListUsersDto.cs b/Cajetan.OF-DL/Models/Dtos/Lists/ListUsersDto.cs new file mode 100644 index 0000000..e88bc22 --- /dev/null +++ b/Cajetan.OF-DL/Models/Dtos/Lists/ListUsersDto.cs @@ -0,0 +1,8 @@ +namespace OF_DL.Models.Dtos.Lists; + +public class ListUsersDto +{ + [JsonProperty("list")] public List List { get; set; } = []; + [JsonProperty("hasMore")] public bool? HasMore { get; set; } + [JsonProperty("nextOffset")] public int NextOffset { get; set; } +} diff --git a/Cajetan.OF-DL/Properties/GlobalUsings.cs b/Cajetan.OF-DL/Properties/GlobalUsings.cs index 4a11ae2..1e5f526 100644 --- a/Cajetan.OF-DL/Properties/GlobalUsings.cs +++ b/Cajetan.OF-DL/Properties/GlobalUsings.cs @@ -11,6 +11,7 @@ global using OF_DL.Services; global using Serilog; global using Serilog.Context; global using Spectre.Console; +global using ListsDtos = OF_DL.Models.Dtos.Lists; global using MessageDtos = OF_DL.Models.Dtos.Messages; global using MessageEntities = OF_DL.Models.Entities.Messages; global using SubscriptionsDtos = OF_DL.Models.Dtos.Subscriptions; diff --git a/Cajetan.OF-DL/Services/CajetanApiService.cs b/Cajetan.OF-DL/Services/CajetanApiService.cs index 9285851..832caeb 100644 --- a/Cajetan.OF-DL/Services/CajetanApiService.cs +++ b/Cajetan.OF-DL/Services/CajetanApiService.cs @@ -27,6 +27,64 @@ public class CajetanApiService(IAuthService authService, IConfigService configSe return GetAllSubscriptions(endpoint, includeRestricted, "expired"); } + public new async Task?> GetListUsers(string endpoint) + { + if (!HasSignedRequestAuth()) + return null; + + try + { + Dictionary users = new(StringComparer.OrdinalIgnoreCase); + + Log.Debug($"Calling GetListUsers - {endpoint}"); + + const int limit = 50; + int offset = 0; + + Dictionary getParams = new() + { + ["format"] = "infinite", + ["limit"] = limit.ToString() + }; + + while (true) + { + getParams["offset"] = offset.ToString(); + + string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, new HttpClient()); + + if (string.IsNullOrWhiteSpace(body)) + break; + + ListsDtos.ListUsersDto? listUsers = DeserializeJson(body, s_mJsonSerializerSettings); + + if (listUsers?.List is null) + break; + + foreach (ListsDtos.UsersListDto item in listUsers.List) + { + if (item.Id is null) + continue; + + users.TryAdd(item.Username, item.Id.Value); + } + + if (listUsers.HasMore is false) + break; + + offset = listUsers.NextOffset; + } + + return users; + } + catch (Exception ex) + { + ExceptionLoggerHelper.LogException(ex); + } + + return null; + } + public async Task GetDetailedUserInfoAsync(string endpoint) { Log.Debug($"Calling GetDetailedUserInfo: {endpoint}"); diff --git a/Cajetan.OF-DL/Services/ICajetanApiService.cs b/Cajetan.OF-DL/Services/ICajetanApiService.cs index de9d831..12360c3 100644 --- a/Cajetan.OF-DL/Services/ICajetanApiService.cs +++ b/Cajetan.OF-DL/Services/ICajetanApiService.cs @@ -2,6 +2,8 @@ namespace OF_DL.Services; public interface ICajetanApiService : IApiService { + new Task?> GetListUsers(string endpoint); + Task GetDetailedUserInfoAsync(string endpoint); Task> GetUsersWithProgressAsync(string typeDisplay, string endpoint, string? typeParam, bool offsetByCount); Task> GetUsersWithUnreadMessagesAsync(); diff --git a/Cajetan.OF-DL/Worker.cs b/Cajetan.OF-DL/Worker.cs index 313d717..b999568 100644 --- a/Cajetan.OF-DL/Worker.cs +++ b/Cajetan.OF-DL/Worker.cs @@ -332,17 +332,14 @@ internal class Worker(IServiceProvider serviceProvider) Log.Information("Getting Users from list '{ListName:l}' (Include Restricted: {IncludeRestrictedSubscriptions})", name, currentConfig.IncludeRestrictedSubscriptions); AnsiConsole.MarkupLine($"[green]Getting Users from list '{name}' (Include Restricted: {currentConfig.IncludeRestrictedSubscriptions})[/]"); - List listUsernames = await _apiService.GetListUsers($"/lists/{listId}/users") ?? []; + Dictionary listUsernames = await _apiService.GetListUsers($"/lists/{listId}/users") ?? []; - foreach (string u in listUsernames) + foreach ((string username, long userId) in listUsernames) { - if (usersFromLists.ContainsKey(u)) + if (usersFromLists.ContainsKey(username)) continue; - if (!allUsersAndLists.Users.TryGetValue(u, out long userId)) - continue; - - usersFromLists[u] = userId; + usersFromLists[username] = userId; } }