fix(admin): fixed various issues lmao idk it was a ballache and is probably quite shit. sorry to future me
This commit is contained in:
parent
56ea7fb7f0
commit
2cbbc00489
29 changed files with 382 additions and 131 deletions
|
|
@ -107,16 +107,23 @@ public class AuthService : IAuthService
|
|||
return new UserLoginDTO(newLogin);
|
||||
}
|
||||
|
||||
public async Task UpdateLoginOwnPassword(string username, string newPassword, string oldPassword)
|
||||
public async Task UpdateLoginOwnPassword(string sessionToken, string newPassword, string oldPassword)
|
||||
{
|
||||
var login = await _loginRepo.GetUserLogin(username);
|
||||
var session = await _sessionRepo.GetUserSession(sessionToken);
|
||||
|
||||
if (session == null)
|
||||
{
|
||||
throw new LoginFailedException();
|
||||
}
|
||||
|
||||
var login = session.Login;
|
||||
|
||||
if (!BCrypt.Net.BCrypt.Verify(oldPassword, login.HashedPassword))
|
||||
{
|
||||
throw new LoginFailedException();
|
||||
}
|
||||
|
||||
await UpdateLoginPassword(username, newPassword);
|
||||
await UpdateLoginPassword(login.Username, newPassword);
|
||||
}
|
||||
|
||||
public async Task UpdateLoginPassword(string username, string newPassword)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using JellyGlass.Exceptions;
|
||||
using JellyGlass.Models;
|
||||
using JellyGlass.Repositories;
|
||||
|
||||
namespace JellyGlass.Services;
|
||||
|
|
@ -6,7 +7,7 @@ namespace JellyGlass.Services;
|
|||
public class ClientService : IClientService
|
||||
{
|
||||
private IServerRepository _repository;
|
||||
private static JellyfinApiClient[] _clients = [];
|
||||
private static List<JellyfinApiClient> _clients = new List<JellyfinApiClient>();
|
||||
private ILogger<ClientService> _logger;
|
||||
|
||||
public ClientService(IServerRepository repository, ILogger<ClientService> logger)
|
||||
|
|
@ -22,59 +23,70 @@ public class ClientService : IClientService
|
|||
await LoadClients();
|
||||
}
|
||||
|
||||
return _clients;
|
||||
return _clients.ToArray();
|
||||
}
|
||||
|
||||
public async Task<JellyfinApiClient> GetClientForServerId(string serverId)
|
||||
public async Task<JellyfinApiClient> GetClientFromUrl(string url)
|
||||
{
|
||||
if (!_clients.Any())
|
||||
{
|
||||
await LoadClients();
|
||||
}
|
||||
|
||||
foreach (var client in _clients)
|
||||
var client = _clients.FirstOrDefault(c => c.InstanceUrl == url);
|
||||
|
||||
if (client == null)
|
||||
{
|
||||
if (client.ID == serverId)
|
||||
{
|
||||
return client;
|
||||
}
|
||||
throw new Exception($"Could not find client with url {url}");
|
||||
}
|
||||
|
||||
throw new Exception($"Client with ID {serverId} not found");
|
||||
return client;
|
||||
}
|
||||
|
||||
public async Task LoadNewClient(Server server)
|
||||
{
|
||||
var client = new JellyfinApiClient(server.Url, server.ApiToken);
|
||||
|
||||
try
|
||||
{
|
||||
await client.GetServerInfo();
|
||||
}
|
||||
catch (JellyfinApiClientException e)
|
||||
{
|
||||
_logger.LogError($"Error authenticating to {server.Url}. Error: {e.Message} Client will not be used");
|
||||
throw;
|
||||
}
|
||||
|
||||
_clients.Add(client);
|
||||
}
|
||||
|
||||
public void UnloadClient(JellyfinApiClient client)
|
||||
{
|
||||
if (_clients.Contains(client))
|
||||
{
|
||||
_clients.Remove(client);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Haven't loaded Jellyfin api client with ID of {client.ID}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task LoadClients()
|
||||
{
|
||||
var servers = await _repository.GetServers();
|
||||
var clients = new List<JellyfinApiClient>();
|
||||
|
||||
foreach (var server in servers)
|
||||
{
|
||||
var client = new JellyfinApiClient(server.Url, server.ApiToken);
|
||||
|
||||
// try
|
||||
// {
|
||||
// await client.Authenticate();
|
||||
// }
|
||||
// catch (JellyfinApiClientException e)
|
||||
// {
|
||||
// _logger.LogError($"Error authenticating to {server.Url}. Error: {e.Message} Client will not be used");
|
||||
// continue;
|
||||
// }
|
||||
|
||||
try
|
||||
{
|
||||
await client.GetServerInfo(); //test the connection
|
||||
await LoadNewClient(server);
|
||||
}
|
||||
catch (JellyfinApiClientException e)
|
||||
catch (JellyfinApiClientException)
|
||||
{
|
||||
_logger.LogError($"Error authenticating to {server.Url}. Error: {e.Message} Client will not be used");
|
||||
continue;
|
||||
}
|
||||
|
||||
clients.Add(client);
|
||||
}
|
||||
|
||||
_clients = clients.ToArray();
|
||||
}
|
||||
}
|
||||
|
|
@ -10,7 +10,7 @@ public interface IAuthService
|
|||
public Task<UserLoginDTO[]> GetLogins();
|
||||
public Task<UserLoginDTO> GetLogin(string username);
|
||||
public Task<UserLoginDTO> CreateLogin(string username, string password);
|
||||
public Task UpdateLoginOwnPassword(string username, string newPassword, string oldPassword);
|
||||
public Task UpdateLoginOwnPassword(string sessionToken, string newPassword, string oldPassword);
|
||||
public Task UpdateLoginPassword(string username, string newPassword);
|
||||
public Task<UserLoginDTO> DeleteLogin(string username);
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
using JellyGlass.Models;
|
||||
using JellyGlass.Repositories;
|
||||
|
||||
namespace JellyGlass.Services;
|
||||
|
|
@ -5,6 +6,7 @@ namespace JellyGlass.Services;
|
|||
public interface IClientService
|
||||
{
|
||||
public Task<JellyfinApiClient[]> GetClients();
|
||||
// public JellyfinApiClient GetClientForServer(string url);
|
||||
public Task<JellyfinApiClient> GetClientForServerId(string serverId);
|
||||
public Task<JellyfinApiClient> GetClientFromUrl(string url);
|
||||
public Task LoadNewClient(Server server);
|
||||
public void UnloadClient(JellyfinApiClient client);
|
||||
}
|
||||
|
|
@ -5,9 +5,9 @@ namespace JellyGlass.Services;
|
|||
public interface ILibraryService
|
||||
{
|
||||
public Task<Library[]> GetLibraries();
|
||||
public Task<ItemDTO[]> GetItemsFromLibrary(string libraryName, string serverId);
|
||||
public Task<ItemDTO[]> GetItemsFromLibrary(string libraryName, string serverUrl);
|
||||
|
||||
public Task<Library[]> GetLibrariesFromServer(string serverId);
|
||||
public Task<Library[]> GetLibrariesFromServer(string serverUrl);
|
||||
|
||||
|
||||
// public Task<ItemDTO[]> GetChildrenFromItems(ItemDTO[] items);
|
||||
|
|
|
|||
|
|
@ -4,5 +4,5 @@ namespace JellyGlass.Services;
|
|||
|
||||
public interface ISearchService
|
||||
{
|
||||
public Task<ItemDTO[]> Search(string searchTerm, string serverId);
|
||||
public Task<ItemDTO[]> Search(string searchTerm, string serverUrl);
|
||||
}
|
||||
|
|
@ -7,8 +7,8 @@ namespace JellyGlass.Services;
|
|||
public interface IServerService
|
||||
{
|
||||
public Task<ServerDTO[]> GetServers();
|
||||
public Task<ServerDTO> GetServerByID(int id);
|
||||
public Task<ServerDTO> GetServerByUrl(string url);
|
||||
public Task<ServerDTO> CreateServer(string owner, string url, string apiToken);
|
||||
public Task<ServerDTO> UpdateServer(int id, string owner, string url, string apiToken);
|
||||
public Task<ServerDTO> DeleteServer(int id);
|
||||
public Task<ServerDTO> UpdateServer(string owner, string url, string apiToken);
|
||||
public Task<ServerDTO> DeleteServer(string url);
|
||||
}
|
||||
|
|
@ -44,13 +44,13 @@ public class LibraryService : ILibraryService
|
|||
return libraries.Values.ToArray();
|
||||
}
|
||||
|
||||
public async Task<Item> GetLibrary(string libraryName, string serverId)
|
||||
public async Task<Item> GetLibrary(string libraryName, string serverUrl)
|
||||
{
|
||||
var client = await _clientService.GetClientForServerId(serverId);
|
||||
var client = await _clientService.GetClientFromUrl(serverUrl); ;
|
||||
|
||||
if (client == null)
|
||||
{
|
||||
throw new Exception($"Could not find client with ID of {serverId}");
|
||||
throw new Exception($"Could not find client with url of {serverUrl}");
|
||||
}
|
||||
|
||||
var libraries = await client.GetInstanceLibraries();
|
||||
|
|
@ -66,11 +66,11 @@ public class LibraryService : ILibraryService
|
|||
throw new Exception("Couldn't find library");
|
||||
}
|
||||
|
||||
public async Task<ItemDTO[]> GetItemsFromLibrary(string libraryName, string serverId)
|
||||
public async Task<ItemDTO[]> GetItemsFromLibrary(string libraryName, string serverUrl)
|
||||
{
|
||||
var client = await _clientService.GetClientForServerId(serverId);
|
||||
var client = await _clientService.GetClientFromUrl(serverUrl);
|
||||
|
||||
var library = await GetLibrary(libraryName, serverId);
|
||||
var library = await GetLibrary(libraryName, serverUrl);
|
||||
|
||||
var items = await client.GetItemChildren(library.Id);
|
||||
|
||||
|
|
@ -84,9 +84,9 @@ public class LibraryService : ILibraryService
|
|||
return dtos.ToArray();
|
||||
}
|
||||
|
||||
public async Task<Library[]> GetLibrariesFromServer(string serverId)
|
||||
public async Task<Library[]> GetLibrariesFromServer(string serverUrl)
|
||||
{
|
||||
var client = await _clientService.GetClientForServerId(serverId);
|
||||
var client = await _clientService.GetClientFromUrl(serverUrl);
|
||||
|
||||
var libraries = new Dictionary<string, Library>();
|
||||
|
||||
|
|
|
|||
|
|
@ -14,9 +14,9 @@ public class SearchService : ISearchService
|
|||
_clientService = clientService;
|
||||
}
|
||||
|
||||
public async Task<ItemDTO[]> Search(string searchTerm, string serverId)
|
||||
public async Task<ItemDTO[]> Search(string searchTerm, string serverUrl)
|
||||
{
|
||||
var client = await _clientService.GetClientForServerId(serverId);
|
||||
var client = await _clientService.GetClientFromUrl(serverUrl);
|
||||
|
||||
var items = await client.GetItems(searchTerm: searchTerm);
|
||||
|
||||
|
|
@ -30,25 +30,25 @@ public class SearchService : ISearchService
|
|||
return dtos.ToArray();
|
||||
}
|
||||
|
||||
public async Task<ItemDTO[]> Search2(string searchTerm, string serverId)
|
||||
// public async Task<ItemDTO[]> Search2(string searchTerm, int serverId)
|
||||
// {
|
||||
// var libraries = await _libraryService.GetLibrariesFromServer(serverId);
|
||||
|
||||
// var foundItems = new List<ItemDTO>();
|
||||
|
||||
// foreach (var library in libraries)
|
||||
// {
|
||||
// var found = await SearchLibraryForTerm(searchTerm, serverId, library);
|
||||
|
||||
// foundItems.AddRange(found);
|
||||
// }
|
||||
|
||||
// return foundItems.ToArray();
|
||||
// }
|
||||
|
||||
private async Task<ItemDTO[]> SearchLibraryForTerm(string searchTerm, string serverUrl, Library library)
|
||||
{
|
||||
var libraries = await _libraryService.GetLibrariesFromServer(serverId);
|
||||
|
||||
var foundItems = new List<ItemDTO>();
|
||||
|
||||
foreach (var library in libraries)
|
||||
{
|
||||
var found = await SearchLibraryForTerm(searchTerm, serverId, library);
|
||||
|
||||
foundItems.AddRange(found);
|
||||
}
|
||||
|
||||
return foundItems.ToArray();
|
||||
}
|
||||
|
||||
private async Task<ItemDTO[]> SearchLibraryForTerm(string searchTerm, string serverId, Library library)
|
||||
{
|
||||
var items = await _libraryService.GetItemsFromLibrary(library.Name, serverId);
|
||||
var items = await _libraryService.GetItemsFromLibrary(library.Name, serverUrl);
|
||||
|
||||
var foundItems = new List<ItemDTO>();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
|
||||
|
||||
using JellyGlass.Exceptions;
|
||||
using JellyGlass.Models;
|
||||
using JellyGlass.Repositories;
|
||||
|
||||
|
|
@ -49,31 +50,68 @@ public class ServerService : IServerService
|
|||
return dtos.ToArray();
|
||||
}
|
||||
|
||||
public async Task<ServerDTO> GetServerByID(int id)
|
||||
public async Task<ServerDTO> GetServerByUrl(string url)
|
||||
{
|
||||
var server = await _repository.GetServerById(id);
|
||||
var server = await _repository.GetServerByUrl(url);
|
||||
|
||||
return new ServerDTO(server);
|
||||
return await GetServerDTO(server);
|
||||
}
|
||||
|
||||
public async Task<ServerDTO> CreateServer(string owner, string url, string apiToken)
|
||||
{
|
||||
var newServer = await _repository.CreateServer(owner, url, apiToken);
|
||||
var dto = new ServerDTO(newServer);
|
||||
|
||||
return new ServerDTO(newServer);
|
||||
try
|
||||
{
|
||||
await _service.LoadNewClient(newServer);
|
||||
}
|
||||
catch (JellyfinApiClientException)
|
||||
{
|
||||
dto.Errored = true;
|
||||
}
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
public async Task<ServerDTO> UpdateServer(int id, string owner, string url, string apiToken)
|
||||
public async Task<ServerDTO> UpdateServer(string owner, string url, string apiToken)
|
||||
{
|
||||
var updatedServer = await _repository.UpdateServer(id, owner, url, apiToken);
|
||||
var updatedServer = await _repository.UpdateServer(owner, url, apiToken);
|
||||
|
||||
return new ServerDTO(updatedServer);
|
||||
return await GetServerDTO(updatedServer);
|
||||
}
|
||||
|
||||
public async Task<ServerDTO> DeleteServer(int id)
|
||||
public async Task<ServerDTO> DeleteServer(string url)
|
||||
{
|
||||
var deletedServer = await _repository.DeleteServer(id);
|
||||
var deletedServer = await _repository.DeleteServer(url);
|
||||
|
||||
try
|
||||
{
|
||||
var client = await _service.GetClientFromUrl(deletedServer.Url);
|
||||
_service.UnloadClient(client);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
//if it's not loaded, we don't care as we want to unload it anyway
|
||||
}
|
||||
|
||||
return new ServerDTO(deletedServer);
|
||||
}
|
||||
|
||||
private async Task<ServerDTO> GetServerDTO(Server server)
|
||||
{
|
||||
var dto = new ServerDTO(server);
|
||||
|
||||
try
|
||||
{
|
||||
var client = await _service.GetClientFromUrl(server.Url);
|
||||
dto.JellyfinServerID = client.ID;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
dto.Errored = true;
|
||||
}
|
||||
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue