Compare commits

...

2 Commits

5 changed files with 99 additions and 13 deletions

View File

@ -31,6 +31,7 @@
<ItemGroup>
<PackageReference Include="Serilog.Sinks.Seq" Version="7.0.1" />
<PackageReference Include="Serilog.Sinks.Debug" Version="3.0.0" />
<PackageReference Include="Akka" Version="1.5.60"/>
<PackageReference Include="BouncyCastle.NetCore" Version="2.2.1"/>
<PackageReference Include="HtmlAgilityPack" Version="1.12.4"/>

View File

@ -20,7 +20,7 @@ static async Task<ServiceCollection> ConfigureServices(string[] args)
{
// Set up dependency injection with LoggingService and ConfigService
ServiceCollection services = new();
services.AddSingleton<ILoggingService, SeqLoggingService>();
services.AddSingleton<ILoggingService, CajetanLoggingService>();
services.AddSingleton<IConfigService, ConfigService>();
services.AddSingleton(new ExitHelper(new SpectreDownloadEventHandler()));
ServiceProvider tempServiceProvider = services.BuildServiceProvider();

View File

@ -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;

View File

@ -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}");

View File

@ -1,13 +1,11 @@
using OF_DL.Enumerations;
using Serilog;
using Serilog.Core;
using Serilog.Events;
namespace OF_DL.Services;
public class SeqLoggingService : ILoggingService
public class CajetanLoggingService : ILoggingService
{
public SeqLoggingService()
public CajetanLoggingService()
{
LevelSwitch = new LoggingLevelSwitch();
InitializeLoggerWithSeq();
@ -35,15 +33,21 @@ public class SeqLoggingService : ILoggingService
private void InitializeLoggerWithSeq()
{
Log.Logger = new LoggerConfiguration()
LevelSwitch.MinimumLevel = LogEventLevel.Warning;
LoggerConfiguration loggerConfig = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithProperty("Application", "OF_DL")
.Enrich.WithProperty("StartTime", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} ")
.Enrich.WithProperty("MachineName", Environment.MachineName)
.MinimumLevel.ControlledBy(LevelSwitch)
.WriteTo.File("logs/OFDL.txt", rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Error)
.WriteTo.Seq("https://seq.cajetan.dk")
.CreateLogger();
.MinimumLevel.Verbose()
.WriteTo.File("logs/OFDL.txt", rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Error, levelSwitch: LevelSwitch)
.WriteTo.Seq("https://seq.cajetan.dk", controlLevelSwitch: LevelSwitch);
if (System.Diagnostics.Debugger.IsAttached)
loggerConfig.WriteTo.Debug(restrictedToMinimumLevel: LogEventLevel.Debug);
Log.Logger = loggerConfig.CreateLogger();
Log.Debug("Logging service initialized");
}