[CHANGE] Fixed storing account/channel
This commit is contained in:
@@ -29,9 +29,9 @@
|
||||
<MudTh>Has login</MudTh>
|
||||
</HeaderContent>
|
||||
<RowTemplate>
|
||||
<MudTd>@context.Name</MudTd>
|
||||
<MudTd>@context.Channel?.Name</MudTd>
|
||||
<MudTd>@context.Id</MudTd>
|
||||
<MudTd>@(context.ClientAccount != null)</MudTd>
|
||||
<MudTd>@(context.HttpCookies.Any())</MudTd>
|
||||
</RowTemplate>
|
||||
<NoRecordsContent>
|
||||
<MudText>No channels found</MudText>
|
||||
|
||||
@@ -9,12 +9,12 @@ namespace Manager.App.Components.Pages;
|
||||
public partial class Channels : ComponentBase
|
||||
{
|
||||
private readonly DialogOptions _dialogOptions = new() { BackdropClick = false, CloseButton = true, FullWidth = true, MaxWidth = MaxWidth.ExtraLarge };
|
||||
private MudTable<ChannelEntity>? _table;
|
||||
private MudTable<ClientAccountEntity>? _table;
|
||||
|
||||
private async Task<TableData<ChannelEntity>> ServerReload(TableState state, CancellationToken token)
|
||||
private async Task<TableData<ClientAccountEntity>> ServerReload(TableState state, CancellationToken token)
|
||||
{
|
||||
var results = await LibraryService.GetChannelsAsync(state.PageSize, state.Page * state.PageSize, token);
|
||||
return !results.IsSuccess ? new TableData<ChannelEntity>() : new TableData<ChannelEntity> { Items = results.Value, TotalItems = results.Total };
|
||||
var results = await LibraryService.GetAccountsAsync(null, state.Page * state.PageSize, state.PageSize, token);
|
||||
return !results.IsSuccess ? new TableData<ClientAccountEntity>() : new TableData<ClientAccountEntity> { Items = results.Value, TotalItems = results.Total };
|
||||
}
|
||||
|
||||
private async Task OnAddAccountDialogAsync()
|
||||
|
||||
@@ -11,9 +11,8 @@ public interface ILibraryService
|
||||
public Task<Result> FetchChannelImagesAsync(InnertubeChannel innertubeChannel);
|
||||
public Task<Result> SaveClientAsync(ClientAccountEntity client, CancellationToken cancellationToken = default);
|
||||
public Task<Result<ChannelEntity>> GetChannelByIdAsync(string id, CancellationToken cancellationToken = default);
|
||||
|
||||
public Task<Result> SaveChannelAsync(InnertubeChannel innertubeChannel, CancellationToken cancellationToken = default);
|
||||
public Task<Result<LibraryInformation>> GetLibraryInfoAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
public Task<ListResult<ChannelEntity>> GetChannelsAsync(int total = 20, int offset = 0, CancellationToken cancellationToken = default);
|
||||
public Task<ListResult<ClientAccountEntity>> GetAccountsAsync(string? search, int offset = 0, int total = 20, CancellationToken cancellationToken = default);
|
||||
public Task<ListResult<ChannelEntity>> GetChannelsAsync(int offset = 0, int total = 20, CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -61,7 +61,7 @@ public class LibraryService : ILibraryService
|
||||
{
|
||||
foreach (var image in images)
|
||||
{
|
||||
if (context.Files.Any(f => image.Url.Equals(f.OriginalUrl, StringComparison.OrdinalIgnoreCase)))
|
||||
if (context.Files.Any(f => image.Url.Equals(f.OriginalUrl)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -108,7 +108,7 @@ public class LibraryService : ILibraryService
|
||||
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var updateEntity = false;
|
||||
var dbClient = context.ClientAccounts.FirstOrDefault(c => c.Id == client.Id);
|
||||
var dbClient = context.ClientAccounts.Include(ca => ca.HttpCookies).FirstOrDefault(c => c.Id == client.Id);
|
||||
if (dbClient == null)
|
||||
{
|
||||
dbClient = client;
|
||||
@@ -245,13 +245,55 @@ public class LibraryService : ILibraryService
|
||||
return HandleException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ListResult<ClientAccountEntity>> GetAccountsAsync(string? search, int offset = 0, int total = 20, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (total == 0)
|
||||
{
|
||||
total = 20;
|
||||
}
|
||||
|
||||
public async Task<ListResult<ChannelEntity>> GetChannelsAsync(int total = 20, int offset = 0, CancellationToken cancellationToken = default)
|
||||
try
|
||||
{
|
||||
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||
var accounts = context.ClientAccounts
|
||||
.Include(ca => ca.Channel)
|
||||
.Include(ca => ca.HttpCookies)
|
||||
.OrderByDescending(ca => ca.Id);
|
||||
var totalAccounts = accounts.Count();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(search) && totalAccounts != 0)
|
||||
{
|
||||
var normalizedSearch = $"%{search.ToLower()}%";
|
||||
var searched = accounts
|
||||
.Where(ca =>
|
||||
EF.Functions.Like(
|
||||
(
|
||||
ca.Id.ToString() + " " +
|
||||
(ca.Channel != null ? ca.Channel.Name : "") + " " +
|
||||
(ca.Channel != null ? ca.Channel.Handle : "")
|
||||
).ToLower(),
|
||||
normalizedSearch
|
||||
)
|
||||
);
|
||||
totalAccounts = searched.Count();
|
||||
accounts = searched.OrderByDescending(ca => ca.Id);
|
||||
}
|
||||
|
||||
return new ListResultReturn<ClientAccountEntity>(totalAccounts == 0 ? [] : accounts.Skip(offset).Take(total).ToList(), totalAccounts);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return ResultError.Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ListResult<ChannelEntity>> GetChannelsAsync(int offset = 0, int total = 20, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||
var orderedAccounts = context.Channels.Include(x => x.ClientAccount).Where(x => x.ClientAccount != null).OrderBy(x => x.Id);
|
||||
var orderedAccounts = context.Channels.Include(x => x.ClientAccount).OrderBy(x => x.Id);
|
||||
return new ListResultReturn<ChannelEntity>(orderedAccounts.Skip(offset).Take(total).ToList(),orderedAccounts.Count());
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
@@ -105,8 +105,23 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
|
||||
LogEvent("Failed to store client no ID!", LogSeverity.Warning);
|
||||
return ResultError.Fail("Client does not have an ID, cannot save to library database!");
|
||||
}
|
||||
|
||||
List<HttpCookieEntity> httpCookies = [];
|
||||
httpCookies.AddRange(client.CookieContainer.GetAllCookies()
|
||||
.ToList()
|
||||
.Select(cookie => new HttpCookieEntity
|
||||
{
|
||||
ClientId = client.Id,
|
||||
Name = cookie.Name,
|
||||
Value = cookie.Value,
|
||||
Domain = cookie.Domain,
|
||||
Path = cookie.Path,
|
||||
Secure = cookie.Secure,
|
||||
HttpOnly = cookie.HttpOnly,
|
||||
ExpiresUtc = cookie.Expires
|
||||
}));
|
||||
|
||||
var saveResult = await _libraryService.SaveClientAsync(new ClientAccountEntity { Id = client.Id, UserAgent = client.UserAgent }, cancellationToken);
|
||||
var saveResult = await _libraryService.SaveClientAsync(new ClientAccountEntity { Id = client.Id, UserAgent = client.UserAgent, HttpCookies = httpCookies }, cancellationToken);
|
||||
return saveResult;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user