From bbda791316b7c240870b9cab4b4d662b52bbb5f3 Mon Sep 17 00:00:00 2001 From: Casper Sparre Date: Fri, 20 Feb 2026 22:32:51 +0100 Subject: [PATCH] Implemented overrides for get Active/Expired subscriptions --- Cajetan.OF-DL/Properties/GlobalUsings.cs | 2 +- Cajetan.OF-DL/Services/CajetanApiService.cs | 85 ++++++++++++++++++++- 2 files changed, 84 insertions(+), 3 deletions(-) diff --git a/Cajetan.OF-DL/Properties/GlobalUsings.cs b/Cajetan.OF-DL/Properties/GlobalUsings.cs index ef39d9b..4a11ae2 100644 --- a/Cajetan.OF-DL/Properties/GlobalUsings.cs +++ b/Cajetan.OF-DL/Properties/GlobalUsings.cs @@ -13,6 +13,6 @@ global using Serilog.Context; global using Spectre.Console; global using MessageDtos = OF_DL.Models.Dtos.Messages; global using MessageEntities = OF_DL.Models.Entities.Messages; -global using SubscriptionDtos = OF_DL.Models.Dtos.Subscriptions; +global using SubscriptionsDtos = OF_DL.Models.Dtos.Subscriptions; global using UserDtos = OF_DL.Models.Dtos.Users; global using UserEntities = OF_DL.Models.Entities.Users; diff --git a/Cajetan.OF-DL/Services/CajetanApiService.cs b/Cajetan.OF-DL/Services/CajetanApiService.cs index d3511e8..2ae13f5 100644 --- a/Cajetan.OF-DL/Services/CajetanApiService.cs +++ b/Cajetan.OF-DL/Services/CajetanApiService.cs @@ -15,6 +15,18 @@ public class CajetanApiService(IAuthService authService, IConfigService configSe return userInfo; } + public new Task?> GetActiveSubscriptions(string endpoint, bool includeRestricted) + { + Log.Debug("Calling GetActiveSubscriptions"); + return GetAllSubscriptions(endpoint, includeRestricted, "active"); + } + + public new Task?> GetExpiredSubscriptions(string endpoint, bool includeRestricted) + { + Log.Debug("Calling GetExpiredSubscriptions"); + return GetAllSubscriptions(endpoint, includeRestricted, "expired"); + } + public async Task GetDetailedUserInfoAsync(string endpoint) { Log.Debug($"Calling GetDetailedUserInfo: {endpoint}"); @@ -116,12 +128,12 @@ public class CajetanApiService(IAuthService authService, IConfigService configSe if (string.IsNullOrWhiteSpace(body)) break; - SubscriptionDtos.SubscriptionsDto? subscriptions = DeserializeJson(body, s_mJsonSerializerSettings); + SubscriptionsDtos.SubscriptionsDto? subscriptions = DeserializeJson(body, s_mJsonSerializerSettings); if (subscriptions?.List is null) break; - foreach (SubscriptionDtos.ListItemDto item in subscriptions.List) + foreach (SubscriptionsDtos.ListItemDto item in subscriptions.List) { if (string.IsNullOrWhiteSpace(item?.Username)) continue; @@ -232,6 +244,75 @@ public class CajetanApiService(IAuthService authService, IConfigService configSe } } + private async Task?> GetAllSubscriptions(string endpoint, bool includeRestricted, string type) + { + if (!HasSignedRequestAuth()) + return null; + + try + { + Dictionary users = new(StringComparer.OrdinalIgnoreCase); + + Log.Debug("Calling GetAllSubscrptions"); + + const int limit = 30; + int offset = 0; + + Dictionary getParams = new() + { + ["type"] = type, + ["format"] = "infinite", + ["limit"] = limit.ToString(), + }; + + while (true) + { + getParams["offset"] = offset.ToString(); + + string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, new HttpClient()); + + if (string.IsNullOrWhiteSpace(body)) + break; + + SubscriptionsDtos.SubscriptionsDto? subscriptionsDto = DeserializeJson(body, s_mJsonSerializerSettings); + + if (subscriptionsDto?.List is null) + break; + + foreach (SubscriptionsDtos.ListItemDto item in subscriptionsDto.List) + { + if (string.IsNullOrWhiteSpace(item.Username)) + { + Log.Warning("Found '{Type:l}' subscription user with empty username (id: {Id})", type, item.Id); + 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); + } + + if (subscriptionsDto.HasMore is false) + break; + + offset += limit; + } + + return users; + } + catch (Exception ex) + { + ExceptionLoggerHelper.LogException(ex); + } + + return null; + } + private async Task GetChatsAsync(string endpoint, bool onlyUnread) { Log.Debug($"Calling GetChats - {endpoint}");