forked from sim0n00ps/OF-DL
Implemented overrides for get Active/Expired subscriptions
This commit is contained in:
parent
17f08513b3
commit
bbda791316
@ -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;
|
||||
|
||||
@ -15,6 +15,18 @@ public class CajetanApiService(IAuthService authService, IConfigService configSe
|
||||
return userInfo;
|
||||
}
|
||||
|
||||
public new Task<Dictionary<string, long>?> GetActiveSubscriptions(string endpoint, bool includeRestricted)
|
||||
{
|
||||
Log.Debug("Calling GetActiveSubscriptions");
|
||||
return GetAllSubscriptions(endpoint, includeRestricted, "active");
|
||||
}
|
||||
|
||||
public new Task<Dictionary<string, long>?> GetExpiredSubscriptions(string endpoint, bool includeRestricted)
|
||||
{
|
||||
Log.Debug("Calling GetExpiredSubscriptions");
|
||||
return GetAllSubscriptions(endpoint, includeRestricted, "expired");
|
||||
}
|
||||
|
||||
public async Task<UserEntities.UserInfo?> 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<SubscriptionDtos.SubscriptionsDto>(body, s_mJsonSerializerSettings);
|
||||
SubscriptionsDtos.SubscriptionsDto? subscriptions = DeserializeJson<SubscriptionsDtos.SubscriptionsDto>(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<Dictionary<string, long>?> GetAllSubscriptions(string endpoint, bool includeRestricted, string type)
|
||||
{
|
||||
if (!HasSignedRequestAuth())
|
||||
return null;
|
||||
|
||||
try
|
||||
{
|
||||
Dictionary<string, long> users = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
Log.Debug("Calling GetAllSubscrptions");
|
||||
|
||||
const int limit = 30;
|
||||
int offset = 0;
|
||||
|
||||
Dictionary<string, string> 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<SubscriptionsDtos.SubscriptionsDto>(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<MessageDtos.ChatsDto> GetChatsAsync(string endpoint, bool onlyUnread)
|
||||
{
|
||||
Log.Debug($"Calling GetChats - {endpoint}");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user