mirror of
https://github.com/Kareadita/Kavita.git
synced 2026-06-06 06:45:18 -04:00
Progress Overhaul + Profile Page and a LOT more! (#4262)
Co-authored-by: Amelia <77553571+Fesaa@users.noreply.github.com> Co-authored-by: Robbie Davis <robbie@therobbiedavis.com>
This commit is contained in:
@@ -7,6 +7,7 @@ using API.Constants;
|
||||
using API.Data;
|
||||
using API.Data.Repositories;
|
||||
using API.DTOs;
|
||||
using API.DTOs.Account;
|
||||
using API.DTOs.JumpBar;
|
||||
using API.DTOs.System;
|
||||
using API.Entities;
|
||||
@@ -68,13 +69,13 @@ public class LibraryController : BaseApiController
|
||||
/// </summary>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[Authorize(Policy = PolicyGroups.AdminPolicy)]
|
||||
[HttpPost("create")]
|
||||
public async Task<ActionResult> AddLibrary(UpdateLibraryDto dto)
|
||||
{
|
||||
if (await _unitOfWork.LibraryRepository.LibraryExists(dto.Name))
|
||||
{
|
||||
return BadRequest(await _localizationService.Translate(User.GetUserId(), "library-name-exists"));
|
||||
return BadRequest(await _localizationService.Translate(UserId, "library-name-exists"));
|
||||
}
|
||||
|
||||
var library = new LibraryBuilder(dto.Name, dto.Type)
|
||||
@@ -98,6 +99,7 @@ public class LibraryController : BaseApiController
|
||||
.ToList();
|
||||
library.RemovePrefixForSortName = dto.RemovePrefixForSortName;
|
||||
library.DefaultLanguage = dto.DefaultLanguage;
|
||||
library.InheritWebLinksFromFirstChapter = dto.InheritWebLinksFromFirstChapter;
|
||||
|
||||
// Override Scrobbling for Comic libraries since there are no providers to scrobble to
|
||||
if (library.Type == LibraryType.Comic)
|
||||
@@ -115,7 +117,7 @@ public class LibraryController : BaseApiController
|
||||
admin.Libraries.Add(library);
|
||||
}
|
||||
|
||||
if (!await _unitOfWork.CommitAsync()) return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-library"));
|
||||
if (!await _unitOfWork.CommitAsync()) return BadRequest(await _localizationService.Translate(UserId, "generic-library"));
|
||||
_logger.LogInformation("Created a new library: {LibraryName}", library.Name);
|
||||
|
||||
// Restart Folder watching if on
|
||||
@@ -126,7 +128,7 @@ public class LibraryController : BaseApiController
|
||||
}
|
||||
|
||||
// Assign all the necessary users with this library side nav
|
||||
var userIds = admins.Select(u => u.Id).Append(User.GetUserId()).ToList();
|
||||
var userIds = admins.Select(u => u.Id).Append(UserId).ToList();
|
||||
var userNeedingNewLibrary = (await _unitOfWork.UserRepository.GetAllUsersAsync(AppUserIncludes.SideNavStreams))
|
||||
.Where(u => userIds.Contains(u.Id))
|
||||
.ToList();
|
||||
@@ -137,7 +139,7 @@ public class LibraryController : BaseApiController
|
||||
_unitOfWork.UserRepository.Update(user);
|
||||
}
|
||||
|
||||
if (!await _unitOfWork.CommitAsync()) return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-library"));
|
||||
if (!await _unitOfWork.CommitAsync()) return BadRequest(await _localizationService.Translate(UserId, "generic-library"));
|
||||
|
||||
await _libraryCacheProvider.RemoveByPrefixAsync(CacheKey);
|
||||
|
||||
@@ -150,7 +152,7 @@ public class LibraryController : BaseApiController
|
||||
await _eventHub.SendMessageAsync(MessageFactory.LibraryModified,
|
||||
MessageFactory.LibraryModifiedEvent(library.Id, "create"), false);
|
||||
await _eventHub.SendMessageAsync(MessageFactory.SideNavUpdate,
|
||||
MessageFactory.SideNavUpdateEvent(User.GetUserId()), false);
|
||||
MessageFactory.SideNavUpdateEvent(UserId), false);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
@@ -160,7 +162,7 @@ public class LibraryController : BaseApiController
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[Authorize(Policy = PolicyGroups.AdminPolicy)]
|
||||
[HttpGet("list")]
|
||||
public ActionResult<IEnumerable<DirectoryDto>> GetDirectories(string? path)
|
||||
{
|
||||
@@ -182,7 +184,7 @@ public class LibraryController : BaseApiController
|
||||
/// For each root, checks if there are any supported files at root to warn the user during library creation about an invalid setup
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[Authorize(Policy = PolicyGroups.AdminPolicy)]
|
||||
[HttpPost("has-files-at-root")]
|
||||
public ActionResult<IList<string>> AnyFilesAtRoot(CheckForFilesInFolderRootsDto dto)
|
||||
{
|
||||
@@ -199,24 +201,16 @@ public class LibraryController : BaseApiController
|
||||
/// Return a specific library
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[Authorize(Policy = PolicyGroups.AdminPolicy)]
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<LibraryDto?>> GetLibrary(int libraryId)
|
||||
{
|
||||
var username = User.GetUsername();
|
||||
var username = Username!;
|
||||
if (string.IsNullOrEmpty(username)) return Unauthorized();
|
||||
|
||||
var cacheKey = CacheKey + username;
|
||||
var result = await _libraryCacheProvider.GetAsync<IEnumerable<LibraryDto>>(cacheKey);
|
||||
if (result.HasValue)
|
||||
{
|
||||
return Ok(result.Value.FirstOrDefault(l => l.Id == libraryId));
|
||||
}
|
||||
var libraries = await GetLibrariesForUser(username);
|
||||
|
||||
var ret = _unitOfWork.LibraryRepository.GetLibraryDtosForUsernameAsync(username).ToList();
|
||||
await _libraryCacheProvider.SetAsync(CacheKey, ret, TimeSpan.FromHours(24));
|
||||
|
||||
return Ok(ret.Find(l => l.Id == libraryId));
|
||||
return Ok(libraries.FirstOrDefault(l => l.Id == libraryId));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -226,17 +220,48 @@ public class LibraryController : BaseApiController
|
||||
[HttpGet("libraries")]
|
||||
public async Task<ActionResult<IEnumerable<LibraryDto>>> GetLibraries()
|
||||
{
|
||||
var username = User.GetUsername();
|
||||
var username = Username!;
|
||||
if (string.IsNullOrEmpty(username)) return Unauthorized();
|
||||
|
||||
return Ok(await GetLibrariesForUser(username));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets libraries for the given user that you also have access to
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("user-libraries")]
|
||||
public async Task<ActionResult<IEnumerable<LibraryDto>>> GetLibrariesForUser(int userId)
|
||||
{
|
||||
var ownUserName = Username!;
|
||||
if (string.IsNullOrEmpty(ownUserName)) return Unauthorized();
|
||||
|
||||
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(userId);
|
||||
if (user == null || string.IsNullOrEmpty(user.UserName)) return BadRequest();
|
||||
|
||||
var ownLibraries = await GetLibrariesForUser(ownUserName);
|
||||
var otherLibraries = await GetLibrariesForUser(user.UserName);
|
||||
|
||||
var sharedLibraries = otherLibraries.IntersectBy(ownLibraries.Select(l => l.Id), l => l.Id).ToList();
|
||||
|
||||
return Ok(sharedLibraries);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all libraries a giver username has access to. And cache them for 24h
|
||||
/// </summary>
|
||||
/// <param name="username"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<IList<LibraryDto>> GetLibrariesForUser(string username)
|
||||
{
|
||||
var cacheKey = CacheKey + username;
|
||||
var result = await _libraryCacheProvider.GetAsync<IEnumerable<LibraryDto>>(cacheKey);
|
||||
if (result.HasValue) return Ok(result.Value);
|
||||
var result = await _libraryCacheProvider.GetAsync<IList<LibraryDto>>(cacheKey);
|
||||
if (result.HasValue) return result.Value;
|
||||
|
||||
var ret = _unitOfWork.LibraryRepository.GetLibraryDtosForUsernameAsync(username);
|
||||
var ret = await _unitOfWork.LibraryRepository.GetLibraryDtosForUsernameAsync(username);
|
||||
await _libraryCacheProvider.SetAsync(CacheKey, ret, TimeSpan.FromHours(24));
|
||||
|
||||
return Ok(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -247,8 +272,8 @@ public class LibraryController : BaseApiController
|
||||
[HttpGet("jump-bar")]
|
||||
public async Task<ActionResult<IEnumerable<JumpKeyDto>>> GetJumpBar(int libraryId)
|
||||
{
|
||||
if (!await _unitOfWork.UserRepository.HasAccessToLibrary(libraryId, User.GetUserId()))
|
||||
return BadRequest(await _localizationService.Translate(User.GetUserId(), "no-library-access"));
|
||||
if (!await _unitOfWork.UserRepository.HasAccessToLibrary(libraryId, UserId))
|
||||
return BadRequest(await _localizationService.Translate(UserId, "no-library-access"));
|
||||
|
||||
return Ok(_unitOfWork.LibraryRepository.GetJumpBarAsync(libraryId));
|
||||
}
|
||||
@@ -258,12 +283,12 @@ public class LibraryController : BaseApiController
|
||||
/// </summary>
|
||||
/// <param name="updateLibraryForUserDto"></param>
|
||||
/// <returns></returns>
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[Authorize(Policy = PolicyGroups.AdminPolicy)]
|
||||
[HttpPost("grant-access")]
|
||||
public async Task<ActionResult<MemberDto>> UpdateUserLibraries(UpdateLibraryForUserDto updateLibraryForUserDto)
|
||||
{
|
||||
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(updateLibraryForUserDto.Username, AppUserIncludes.SideNavStreams);
|
||||
if (user == null) return BadRequest(await _localizationService.Translate(User.GetUserId(), "user-doesnt-exist"));
|
||||
if (user == null) return BadRequest(await _localizationService.Translate(UserId, "user-doesnt-exist"));
|
||||
|
||||
var libraryString = string.Join(',', updateLibraryForUserDto.SelectedLibraries.Select(x => x.Name));
|
||||
_logger.LogInformation("Granting user {UserName} access to: {Libraries}", updateLibraryForUserDto.Username, libraryString);
|
||||
@@ -305,7 +330,7 @@ public class LibraryController : BaseApiController
|
||||
}
|
||||
|
||||
|
||||
return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-library"));
|
||||
return BadRequest(await _localizationService.Translate(UserId, "generic-library"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -314,11 +339,11 @@ public class LibraryController : BaseApiController
|
||||
/// <param name="libraryId"></param>
|
||||
/// <param name="force">If true, will ignore any optimizations to avoid file I/O and will treat similar to a first scan</param>
|
||||
/// <returns></returns>
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[Authorize(Policy = PolicyGroups.AdminPolicy)]
|
||||
[HttpPost("scan")]
|
||||
public async Task<ActionResult> Scan(int libraryId, bool force = false)
|
||||
{
|
||||
if (libraryId <= 0) return BadRequest(await _localizationService.Translate(User.GetUserId(), "greater-0", "libraryId"));
|
||||
if (libraryId <= 0) return BadRequest(await _localizationService.Translate(UserId, "greater-0", "libraryId"));
|
||||
await _taskScheduler.ScanLibrary(libraryId, force);
|
||||
return Ok();
|
||||
}
|
||||
@@ -327,7 +352,7 @@ public class LibraryController : BaseApiController
|
||||
/// Enqueues a bunch of library scans
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[Authorize(Policy = PolicyGroups.AdminPolicy)]
|
||||
[HttpPost("scan-multiple")]
|
||||
public async Task<ActionResult> ScanMultiple(BulkActionDto dto)
|
||||
{
|
||||
@@ -344,7 +369,7 @@ public class LibraryController : BaseApiController
|
||||
/// </summary>
|
||||
/// <param name="force">If true, will ignore any optimizations to avoid file I/O and will treat similar to a first scan</param>
|
||||
/// <returns></returns>
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[Authorize(Policy = PolicyGroups.AdminPolicy)]
|
||||
[HttpPost("scan-all")]
|
||||
public ActionResult ScanAll(bool force = false)
|
||||
{
|
||||
@@ -352,7 +377,7 @@ public class LibraryController : BaseApiController
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[Authorize(Policy = PolicyGroups.AdminPolicy)]
|
||||
[HttpPost("refresh-metadata")]
|
||||
public ActionResult RefreshMetadata(int libraryId, bool force = true, bool forceColorscape = true)
|
||||
{
|
||||
@@ -360,7 +385,7 @@ public class LibraryController : BaseApiController
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[Authorize(Policy = PolicyGroups.AdminPolicy)]
|
||||
[HttpPost("refresh-metadata-multiple")]
|
||||
public ActionResult RefreshMetadataMultiple(BulkActionDto dto, bool forceColorscape = true)
|
||||
{
|
||||
@@ -377,7 +402,7 @@ public class LibraryController : BaseApiController
|
||||
/// </summary>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[Authorize(Policy = PolicyGroups.AdminPolicy)]
|
||||
[HttpPost("copy-settings-from")]
|
||||
public async Task<ActionResult> CopySettingsFromLibraryToLibraries(CopySettingsFromLibraryDto dto)
|
||||
{
|
||||
@@ -423,7 +448,7 @@ public class LibraryController : BaseApiController
|
||||
[HttpPost("scan-folder")]
|
||||
public async Task<ActionResult> ScanFolder(ScanFolderDto dto)
|
||||
{
|
||||
var userId = await _unitOfWork.UserRepository.GetUserIdByApiKeyAsync(dto.ApiKey);
|
||||
var userId = await _unitOfWork.UserRepository.GetUserIdByAuthKeyAsync(dto.ApiKey);
|
||||
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(userId);
|
||||
if (user == null) return Unauthorized();
|
||||
|
||||
@@ -453,15 +478,15 @@ public class LibraryController : BaseApiController
|
||||
/// <remarks>This does not touch any files</remarks>
|
||||
/// <param name="libraryId"></param>
|
||||
/// <returns></returns>
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[Authorize(Policy = PolicyGroups.AdminPolicy)]
|
||||
[HttpDelete("delete")]
|
||||
public async Task<ActionResult<bool>> DeleteLibrary(int libraryId)
|
||||
{
|
||||
_logger.LogInformation("Library {LibraryId} is being deleted by {UserName}", libraryId, User.GetUsername());
|
||||
_logger.LogInformation("Library {LibraryId} is being deleted by {UserName}", libraryId, Username!);
|
||||
|
||||
try
|
||||
{
|
||||
return Ok(await DeleteLibrary(libraryId, User.GetUserId()));
|
||||
return Ok(await DeleteLibrary(libraryId, UserId));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -475,18 +500,18 @@ public class LibraryController : BaseApiController
|
||||
/// <remarks>This does not touch any files</remarks>
|
||||
/// <param name="libraryIds"></param>
|
||||
/// <returns></returns>
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[Authorize(Policy = PolicyGroups.AdminPolicy)]
|
||||
[HttpDelete("delete-multiple")]
|
||||
public async Task<ActionResult<bool>> DeleteMultipleLibraries([FromQuery] List<int> libraryIds)
|
||||
{
|
||||
var username = User.GetUsername();
|
||||
var username = Username!;
|
||||
_logger.LogInformation("Libraries {LibraryIds} are being deleted by {UserName}", libraryIds, username);
|
||||
|
||||
foreach (var libraryId in libraryIds)
|
||||
{
|
||||
try
|
||||
{
|
||||
await DeleteLibrary(libraryId, User.GetUserId());
|
||||
await DeleteLibrary(libraryId, UserId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -581,7 +606,7 @@ public class LibraryController : BaseApiController
|
||||
/// </summary>
|
||||
/// <param name="name">If empty or null, will return true as that is invalid</param>
|
||||
/// <returns></returns>
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[Authorize(Policy = PolicyGroups.AdminPolicy)]
|
||||
[HttpGet("name-exists")]
|
||||
public async Task<ActionResult<bool>> IsLibraryNameValid(string name)
|
||||
{
|
||||
@@ -595,11 +620,11 @@ public class LibraryController : BaseApiController
|
||||
/// <remarks>Any folder or type change will invoke a scan.</remarks>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[Authorize(Policy = PolicyGroups.AdminPolicy)]
|
||||
[HttpPost("update")]
|
||||
public async Task<ActionResult> UpdateLibrary(UpdateLibraryDto dto)
|
||||
{
|
||||
var userId = User.GetUserId();
|
||||
var userId = UserId;
|
||||
var library = await _unitOfWork.LibraryRepository.GetLibraryForIdAsync(dto.Id, LibraryIncludes.Folders | LibraryIncludes.FileTypes | LibraryIncludes.ExcludePatterns);
|
||||
if (library == null) return BadRequest(await _localizationService.Translate(userId, "library-doesnt-exist"));
|
||||
|
||||
@@ -642,6 +667,8 @@ public class LibraryController : BaseApiController
|
||||
|
||||
private void UpdateLibrarySettings(UpdateLibraryDto dto, Library library, bool updateType = true)
|
||||
{
|
||||
// Reminder: Add new fields to the Create Library Endpoint!
|
||||
|
||||
if (updateType)
|
||||
{
|
||||
library.Type = dto.Type;
|
||||
|
||||
Reference in New Issue
Block a user