[CHANGE] Client handling

This commit is contained in:
max
2025-10-19 23:21:07 +02:00
parent e4596df392
commit 34e029ec79
4 changed files with 60 additions and 16 deletions

View File

@@ -50,7 +50,6 @@ public partial class Accounts : ComponentBase
await _table.ReloadServerData();
}
Snackbar.Add($"Client {clientChannel.Channel?.Handle ?? clientChannel.YouTubeClient.Id} saved!", Severity.Success);
ClientService.AddClient(clientChannel.YouTubeClient);
}
else
{

View File

@@ -0,0 +1,16 @@
using Manager.App.Models.Library;
namespace Manager.App.Models.System;
public class YouTubeClientItem : AccountListView
{
public YouTubeClientItem(AccountListView accountListView)
{
Id = accountListView.Id;
Name = accountListView.Name;
Handle = accountListView.Handle;
HasCookies = accountListView.HasCookies;
AvatarFileId = accountListView.AvatarFileId;
}
public bool IsLoaded { get; set; }
}

View File

@@ -1,6 +1,7 @@
using System.Net;
using DotBased.Logging;
using DotBased.Monads;
using Manager.App.Models.System;
using Manager.Data.Entities.LibraryContext;
using Manager.YouTube;
@@ -39,17 +40,40 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
}
}
public async Task<Result> AddClientByIdAsync(string id, CancellationToken stoppingToken = default)
public async Task<ListResult<YouTubeClientItem>> GetClientsAsync(string search, int offset = 0, int limit = 10, CancellationToken cancellationToken = default)
{
if (_libraryService == null)
{
return ResultError.Fail("Library service is not initialized!.");
}
var clientResult = await _libraryService.GetChannelByIdAsync(id, stoppingToken);
var accountsResult = await _libraryService.GetAccountsAsync(search, offset, limit, cancellationToken);
if (!accountsResult.IsSuccess)
{
return accountsResult.Error ?? ResultError.Fail("Failed to get accounts!");
}
var comparedClients = accountsResult.Value.Select(x => new YouTubeClientItem(x)
{ Id = x.Id, IsLoaded = _loadedClients.Contains(x.Id) }).ToList();
return new ListResultReturn<YouTubeClientItem>(comparedClients, accountsResult.Total);
}
public async Task<Result<YouTubeClient>> LoadClientByIdAsync(string id, bool forceLoad = false, CancellationToken cancellationToken = default)
{
if (_libraryService == null)
{
return ResultError.Fail("Library service is not initialized!.");
}
if (!forceLoad && _loadedClients.TryGetValue(id, out var client))
{
return client;
}
var clientResult = await _libraryService.GetChannelByIdAsync(id, cancellationToken);
if (!clientResult.IsSuccess)
{
return clientResult;
return clientResult.Error ?? ResultError.Fail("Failed to load channel from database!");
}
var clientAcc = clientResult.Value.ClientAccount;
@@ -79,18 +103,8 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
return ytClientResult;
}
AddClient(ytClientResult.Value);
return Result.Success();
}
public void AddClient(YouTubeClient client)
{
if (_loadedClients.Contains(client))
{
return;
}
_loadedClients.Add(client);
_loadedClients.Add(ytClientResult.Value);
return ytClientResult.Value;
}
public async Task<Result> SaveClientAsync(YouTubeClient client, CancellationToken cancellationToken = default)
@@ -106,6 +120,8 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
return ResultError.Fail("Client does not have an ID, cannot save to library database!");
}
_loadedClients.Add(client);
List<HttpCookieEntity> httpCookies = [];
httpCookies.AddRange(client.CookieContainer.GetAllCookies().Where(c => c.Expires != DateTime.MinValue)
.ToList()

View File

@@ -8,4 +8,17 @@ public class YouTubeClientCollection : KeyedCollection<string, YouTubeClient>
{
return item.Id;
}
public new void Add(YouTubeClient client)
{
if (Items.Contains(client))
{
var index = Items.IndexOf(client);
Items[index] = client;
}
else
{
Items.Add(client);
}
}
}