[CHANGE] Reworked db with interceptors

This commit is contained in:
max
2025-09-10 23:49:41 +02:00
parent b1e5b0dc68
commit 0f83cf1ddc
23 changed files with 293 additions and 79 deletions

View File

@@ -2,7 +2,7 @@
<html lang="en">
<head>
<title>Application</title>
<title>YouTube Manager server</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<base href="/"/>
@@ -11,7 +11,6 @@
<link rel="icon" type="image/png" href="favicon.png"/>
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet"/>
<link href="_content/MudBlazor/MudBlazor.min.css?v=@Metadata.Version" rel="stylesheet"/>
<HeadOutlet/>
</head>
<body>

View File

@@ -107,7 +107,11 @@ public partial class EventConsole : ComponentBase
_batchLock.Release();
}
_serviceEvents.AddRange(batch);
foreach (var serviceEvent in _batchBuffer.Where(serviceEvent => !_serviceEvents.Contains(serviceEvent)))
{
_serviceEvents.Add(serviceEvent);
}
_lastBatchUpdate = DateTime.UtcNow;
if (_virtualize != null)

View File

@@ -6,6 +6,8 @@
@inject ILibraryService LibraryService
@inject IDialogService DialogService
@inject IOptions<LibrarySettings> LibraryOptions
@inject ClientService ClientService
@inject ISnackbar Snackbar
<PageTitle>Channels</PageTitle>

View File

@@ -28,21 +28,21 @@ public partial class Channels : ComponentBase
return;
}
var client = (ClientPrep)result.Data;
if (client == null)
var clientPrep = (ClientPrep)result.Data;
if (clientPrep?.YouTubeClient == null)
{
return;
}
/*var savedResult = await ClientManager.SaveClientAsync(client);
var savedResult = await ClientService.SaveClientAsync(clientPrep.YouTubeClient, clientPrep.Channel);
if (!savedResult.IsSuccess)
{
Snackbar.Add($"Failed to store client: {savedResult.Error?.Description ?? "Unknown!"}", Severity.Error);
}
else
{
Snackbar.Add($"Client {client.External.Channel?.Handle ?? client.Id} saved!", Severity.Success);
}*/
Snackbar.Add($"Client {clientPrep.Channel?.Handle ?? clientPrep.YouTubeClient.Id} saved!", Severity.Success);
}
await InvokeAsync(StateHasChanged);
}

View File

@@ -1,6 +1,6 @@
@page "/Development"
@using Manager.App.Components.Application.Dev
<title>Development page</title>
<PageTitle>Development page</PageTitle>
<MudTabs Outlined Position="Position.Left" PanelClass="pa-4" ApplyEffectsToContainer Style="height: 100%">
<MudTabPanel Text="Authentication">

View File

@@ -5,7 +5,7 @@
@inject BackgroundServiceRegistry ServiceRegistry
<title>Services</title>
<PageTitle>Services</PageTitle>
<MudDataGrid T="ExtendedBackgroundService" Items="@_backgroundServices" Filterable QuickFilter="@QuickFilter">
<ToolBarContent>
@@ -39,4 +39,4 @@
</MudDataGrid>
<EventConsole AsyncEnumerable="@GetEventAsyncEnumerable()" InitialEvents="@GetInitialEvents()"
Elevation="0" Class="mt-3" Style="flex: 1; display: flex; flex-direction: column; min-height: 0;"/>
Elevation="0" Class="mt-3" Style="flex: 1; display: flex; flex-direction: column; min-height: 350px;"/>

View File

@@ -3,4 +3,5 @@
<RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)"/>
<FocusOnNavigate RouteData="routeData" Selector="h1"/>
</Found>
</Router>
</Router>
<HeadOutlet />

View File

@@ -1,34 +1,32 @@
using System.Net;
using DotBased.Logging;
using DotBased.Monads;
using Manager.App.Models.Library;
using Manager.Data.Entities.LibraryContext;
using Manager.YouTube;
using Manager.YouTube.Models.Innertube;
namespace Manager.App.Services.System;
public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientService> logger)
: ExtendedBackgroundService("ClientService", "Managing YouTube clients", logger, TimeSpan.FromMilliseconds(100))
: ExtendedBackgroundService(nameof(ClientService), "Managing YouTube clients", logger, TimeSpan.FromMinutes(10))
{
private readonly List<YouTubeClient> _clients = [];
private CancellationToken _cancellationToken;
private ILibraryService? _libraryService;
protected override async Task InitializeAsync(CancellationToken stoppingToken)
protected override Task InitializeAsync(CancellationToken stoppingToken)
{
_cancellationToken = stoppingToken;
stoppingToken.Register(CancellationRequested);
using var scope = scopeFactory.CreateScope();
_libraryService = scope.ServiceProvider.GetRequiredService<ILibraryService>();
LogEvent("Initializing service...");
//Pause();
return Task.CompletedTask;
}
protected override async Task ExecuteServiceAsync(CancellationToken stoppingToken)
protected override Task ExecuteServiceAsync(CancellationToken stoppingToken)
{
LogEvent("Sending event...");
LogEvent("Sending warning event...", LogSeverity.Warning);
LogEvent("Sending error event...", LogSeverity.Error);
return Task.CompletedTask;
}
private void CancellationRequested()
@@ -36,42 +34,54 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
// Clear up
}
public async Task<Result<ClientPrep>> PrepareClient()
public async Task<Result> SaveClientAsync(YouTubeClient client, Channel? channelInfo = null, CancellationToken cancellationToken = default)
{
return ResultError.Fail("Not implemented!");
}
if (_libraryService == null)
{
return ResultError.Fail("Library service is not initialized!.");
}
/*public async Task<Result> SaveClientAsync(YouTubeClient client, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(client.Id))
{
LogEvent("Failed to store client no ID!", LogSeverity.Warning);
return ResultError.Fail("Client does not have an ID, cannot save to library database!");
}
var channelResult = await libraryService.GetChannelByIdAsync(client.Id, cancellationToken);
var channelResult = await _libraryService.GetChannelByIdAsync(client.Id, cancellationToken);
ChannelEntity? channel;
if (channelResult.IsSuccess)
try
{
channel = channelResult.Value;
UpdateChannelEntity(channel, client);
if (channelResult.IsSuccess)
{
channel = channelResult.Value;
UpdateChannelEntity(client, channel, channelInfo);
}
else
{
channel = CreateNewChannelFromClient(client, channelInfo);
}
}
else
catch (Exception e)
{
channel = CreateNewChannelFromClient(client);
LogEvent("Failed to save client: " + e.Message, LogSeverity.Warning);
return ResultError.Error(e);
}
var saveResult = await libraryService.SaveChannelAsync(channel, cancellationToken);
var saveResult = await _libraryService.SaveChannelAsync(channel, cancellationToken);
return saveResult;
}*/
}
/*private void UpdateChannelEntity(ChannelEntity channel, YouTubeClient client)
private void UpdateChannelEntity(YouTubeClient client, ChannelEntity entity, Channel? channelInfo)
{
channel.Name = client.External.Channel?.ChannelName;
channel.Handle = client.External.Channel?.Handle;
channel.Description = client.External.Channel?.Description;
var clientAcc = channel.ClientAccount;
if (channelInfo != null)
{
entity.Name = channelInfo.ChannelName;
entity.Handle = channelInfo.Handle;
entity.Description = channelInfo.Description;
}
var clientAcc = entity.ClientAccount;
if (clientAcc != null)
{
clientAcc.UserAgent = clientAcc.UserAgent;
@@ -99,8 +109,13 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
}
}
private ChannelEntity CreateNewChannelFromClient(YouTubeClient client)
private ChannelEntity CreateNewChannelFromClient(YouTubeClient client, Channel? channelInfo)
{
if (channelInfo == null)
{
throw new ArgumentNullException(nameof(channelInfo), "Channel information required to store new client/account.");
}
var cookies = new List<HttpCookieEntity>();
foreach (var cookieObj in client.CookieContainer.GetAllCookies())
{
@@ -133,22 +148,21 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
var channel = new ChannelEntity
{
Id = client.Id,
Name = client.External.Channel?.ChannelName,
Handle = client.External.Channel?.Handle,
Description = client.External.Channel?.Description,
Name = channelInfo.ChannelName,
Handle = channelInfo.Handle,
Description = channelInfo.Description,
ClientAccount = clientAcc
};
return channel;
}*/
}
public async Task<Result<YouTubeClient>> LoadClientByIdAsync(string id)
/*public async Task<Result<YouTubeClient>> LoadClientByIdAsync(string id)
{
if (string.IsNullOrWhiteSpace(id))
{
return ResultError.Fail("Client ID is empty!");
}
return ResultError.Fail("Not implemented");
}
}*/
}