mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-05-24 00:52:23 -04:00
* Fixed a bug where volumes that are a range fail to generate series detail * Moved tags closer to genre instead of between different people * Optimized the query for On Deck * Adjusted mime types to map to cbX types instead of their generic compression methods. * Added wiki documentation into invite user flow and register admin user to help users understand email isn't required and they can host their own service. * Refactored the document height to be set and removed on nav service, so the book reader and manga reader aren't broken. * Refactored On Deck to first be completely streamed to UI, without having to do any processing in memory. Rewrote the query so that we sort by progress then chapter added. Progress is 30 days inclusive, chapter added is 7 days. * Fixed an issue where epub date parsing would sometimes fail when it's only a year or not a year at all * Fixed a bug where incognito mode would report progress * Fixed a bug where bulk selection in storyline tab wouldn't properly run the action on the correct chapters (if selecting from volume -> chapter). * Removed a - 1 from total page from card progress bar as the original bug was fixed some time ago * Fixed a bug where the logic for filtering out a progress event for current logged in user didn't check properly when user is logged out. * When a file doesn't exist and we are trying to read, throw a kavita exception to the UI layer and log. * Removed unneeded variable and added some jsdoc
442 lines
20 KiB
C#
442 lines
20 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using API.Data;
|
|
using API.Data.Repositories;
|
|
using API.DTOs;
|
|
using API.DTOs.Filtering;
|
|
using API.DTOs.SeriesDetail;
|
|
using API.Entities;
|
|
using API.Entities.Enums;
|
|
using API.Entities.Metadata;
|
|
using API.Extensions;
|
|
using API.Helpers;
|
|
using API.Services;
|
|
using Kavita.Common;
|
|
using Kavita.Common.Extensions;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace API.Controllers
|
|
{
|
|
public class SeriesController : BaseApiController
|
|
{
|
|
private readonly ILogger<SeriesController> _logger;
|
|
private readonly ITaskScheduler _taskScheduler;
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
private readonly ISeriesService _seriesService;
|
|
|
|
|
|
public SeriesController(ILogger<SeriesController> logger, ITaskScheduler taskScheduler, IUnitOfWork unitOfWork, ISeriesService seriesService)
|
|
{
|
|
_logger = logger;
|
|
_taskScheduler = taskScheduler;
|
|
_unitOfWork = unitOfWork;
|
|
_seriesService = seriesService;
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<ActionResult<IEnumerable<Series>>> GetSeriesForLibrary(int libraryId, [FromQuery] UserParams userParams, [FromBody] FilterDto filterDto)
|
|
{
|
|
var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername());
|
|
var series =
|
|
await _unitOfWork.SeriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId, userId, userParams, filterDto);
|
|
|
|
// Apply progress/rating information (I can't work out how to do this in initial query)
|
|
if (series == null) return BadRequest("Could not get series for library");
|
|
|
|
await _unitOfWork.SeriesRepository.AddSeriesModifiers(userId, series);
|
|
|
|
Response.AddPaginationHeader(series.CurrentPage, series.PageSize, series.TotalCount, series.TotalPages);
|
|
|
|
return Ok(series);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fetches a Series for a given Id
|
|
/// </summary>
|
|
/// <param name="seriesId">Series Id to fetch details for</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="KavitaException">Throws an exception if the series Id does exist</exception>
|
|
[HttpGet("{seriesId:int}")]
|
|
public async Task<ActionResult<SeriesDto>> GetSeries(int seriesId)
|
|
{
|
|
var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername());
|
|
try
|
|
{
|
|
return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoByIdAsync(seriesId, userId));
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_logger.LogError(e, "There was an issue fetching {SeriesId}", seriesId);
|
|
throw new KavitaException("This series does not exist");
|
|
}
|
|
|
|
}
|
|
|
|
[Authorize(Policy = "RequireAdminRole")]
|
|
[HttpDelete("{seriesId}")]
|
|
public async Task<ActionResult<bool>> DeleteSeries(int seriesId)
|
|
{
|
|
var username = User.GetUsername();
|
|
_logger.LogInformation("Series {SeriesId} is being deleted by {UserName}", seriesId, username);
|
|
|
|
return Ok(await _seriesService.DeleteMultipleSeries(new[] {seriesId}));
|
|
}
|
|
|
|
[Authorize(Policy = "RequireAdminRole")]
|
|
[HttpPost("delete-multiple")]
|
|
public async Task<ActionResult> DeleteMultipleSeries(DeleteSeriesDto dto)
|
|
{
|
|
var username = User.GetUsername();
|
|
_logger.LogInformation("Series {SeriesId} is being deleted by {UserName}", dto.SeriesIds, username);
|
|
|
|
if (await _seriesService.DeleteMultipleSeries(dto.SeriesIds)) return Ok();
|
|
|
|
return BadRequest("There was an issue deleting the series requested");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns All volumes for a series with progress information and Chapters
|
|
/// </summary>
|
|
/// <param name="seriesId"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("volumes")]
|
|
public async Task<ActionResult<IEnumerable<VolumeDto>>> GetVolumes(int seriesId)
|
|
{
|
|
var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername());
|
|
return Ok(await _unitOfWork.VolumeRepository.GetVolumesDtoAsync(seriesId, userId));
|
|
}
|
|
|
|
[HttpGet("volume")]
|
|
public async Task<ActionResult<VolumeDto>> GetVolume(int volumeId)
|
|
{
|
|
var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername());
|
|
return Ok(await _unitOfWork.VolumeRepository.GetVolumeDtoAsync(volumeId, userId));
|
|
}
|
|
|
|
[HttpGet("chapter")]
|
|
public async Task<ActionResult<ChapterDto>> GetChapter(int chapterId)
|
|
{
|
|
return Ok(await _unitOfWork.ChapterRepository.GetChapterDtoAsync(chapterId));
|
|
}
|
|
|
|
[HttpGet("chapter-metadata")]
|
|
public async Task<ActionResult<ChapterDto>> GetChapterMetadata(int chapterId)
|
|
{
|
|
return Ok(await _unitOfWork.ChapterRepository.GetChapterMetadataDtoAsync(chapterId));
|
|
}
|
|
|
|
|
|
[HttpPost("update-rating")]
|
|
public async Task<ActionResult> UpdateSeriesRating(UpdateSeriesRatingDto updateSeriesRatingDto)
|
|
{
|
|
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Ratings);
|
|
if (!await _seriesService.UpdateRating(user, updateSeriesRatingDto)) return BadRequest("There was a critical error.");
|
|
return Ok();
|
|
}
|
|
|
|
[HttpPost("update")]
|
|
public async Task<ActionResult> UpdateSeries(UpdateSeriesDto updateSeries)
|
|
{
|
|
_logger.LogInformation("{UserName} is updating Series {SeriesName}", User.GetUsername(), updateSeries.Name);
|
|
|
|
var series = await _unitOfWork.SeriesRepository.GetSeriesByIdAsync(updateSeries.Id);
|
|
|
|
if (series == null) return BadRequest("Series does not exist");
|
|
|
|
var seriesExists =
|
|
await _unitOfWork.SeriesRepository.DoesSeriesNameExistInLibrary(updateSeries.Name.Trim(), series.LibraryId,
|
|
series.Format);
|
|
if (series.Name != updateSeries.Name && seriesExists)
|
|
{
|
|
return BadRequest("A series already exists in this library with this name. Series Names must be unique to a library.");
|
|
}
|
|
|
|
series.Name = updateSeries.Name.Trim();
|
|
if (!string.IsNullOrEmpty(updateSeries.SortName.Trim()))
|
|
{
|
|
series.SortName = updateSeries.SortName.Trim();
|
|
}
|
|
|
|
series.LocalizedName = updateSeries.LocalizedName.Trim();
|
|
|
|
series.NameLocked = updateSeries.NameLocked;
|
|
series.SortNameLocked = updateSeries.SortNameLocked;
|
|
series.LocalizedNameLocked = updateSeries.LocalizedNameLocked;
|
|
|
|
|
|
var needsRefreshMetadata = false;
|
|
// This is when you hit Reset
|
|
if (series.CoverImageLocked && !updateSeries.CoverImageLocked)
|
|
{
|
|
// Trigger a refresh when we are moving from a locked image to a non-locked
|
|
needsRefreshMetadata = true;
|
|
series.CoverImage = string.Empty;
|
|
series.CoverImageLocked = updateSeries.CoverImageLocked;
|
|
}
|
|
|
|
_unitOfWork.SeriesRepository.Update(series);
|
|
|
|
if (await _unitOfWork.CommitAsync())
|
|
{
|
|
if (needsRefreshMetadata)
|
|
{
|
|
_taskScheduler.RefreshSeriesMetadata(series.LibraryId, series.Id);
|
|
}
|
|
return Ok();
|
|
}
|
|
|
|
return BadRequest("There was an error with updating the series");
|
|
}
|
|
|
|
[HttpPost("recently-added")]
|
|
public async Task<ActionResult<IEnumerable<SeriesDto>>> GetRecentlyAdded(FilterDto filterDto, [FromQuery] UserParams userParams, [FromQuery] int libraryId = 0)
|
|
{
|
|
var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername());
|
|
var series =
|
|
await _unitOfWork.SeriesRepository.GetRecentlyAdded(libraryId, userId, userParams, filterDto);
|
|
|
|
// Apply progress/rating information (I can't work out how to do this in initial query)
|
|
if (series == null) return BadRequest("Could not get series");
|
|
|
|
await _unitOfWork.SeriesRepository.AddSeriesModifiers(userId, series);
|
|
|
|
Response.AddPaginationHeader(series.CurrentPage, series.PageSize, series.TotalCount, series.TotalPages);
|
|
|
|
return Ok(series);
|
|
}
|
|
|
|
[HttpPost("recently-updated-series")]
|
|
public async Task<ActionResult<IEnumerable<RecentlyAddedItemDto>>> GetRecentlyAddedChapters()
|
|
{
|
|
var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername());
|
|
return Ok(await _unitOfWork.SeriesRepository.GetRecentlyUpdatedSeries(userId));
|
|
}
|
|
|
|
[HttpPost("all")]
|
|
public async Task<ActionResult<IEnumerable<SeriesDto>>> GetAllSeries(FilterDto filterDto, [FromQuery] UserParams userParams, [FromQuery] int libraryId = 0)
|
|
{
|
|
var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername());
|
|
var series =
|
|
await _unitOfWork.SeriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId, userId, userParams, filterDto);
|
|
|
|
// Apply progress/rating information (I can't work out how to do this in initial query)
|
|
if (series == null) return BadRequest("Could not get series");
|
|
|
|
await _unitOfWork.SeriesRepository.AddSeriesModifiers(userId, series);
|
|
|
|
Response.AddPaginationHeader(series.CurrentPage, series.PageSize, series.TotalCount, series.TotalPages);
|
|
|
|
return Ok(series);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fetches series that are on deck aka have progress on them.
|
|
/// </summary>
|
|
/// <param name="filterDto"></param>
|
|
/// <param name="userParams"></param>
|
|
/// <param name="libraryId">Default of 0 meaning all libraries</param>
|
|
/// <returns></returns>
|
|
[HttpPost("on-deck")]
|
|
public async Task<ActionResult<IEnumerable<SeriesDto>>> GetOnDeck(FilterDto filterDto, [FromQuery] UserParams userParams, [FromQuery] int libraryId = 0)
|
|
{
|
|
var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername());
|
|
var pagedList = await _unitOfWork.SeriesRepository.GetOnDeck(userId, libraryId, userParams, filterDto);
|
|
|
|
await _unitOfWork.SeriesRepository.AddSeriesModifiers(userId, pagedList);
|
|
|
|
Response.AddPaginationHeader(pagedList.CurrentPage, pagedList.PageSize, pagedList.TotalCount, pagedList.TotalPages);
|
|
|
|
return Ok(pagedList);
|
|
}
|
|
|
|
[Authorize(Policy = "RequireAdminRole")]
|
|
[HttpPost("refresh-metadata")]
|
|
public ActionResult RefreshSeriesMetadata(RefreshSeriesDto refreshSeriesDto)
|
|
{
|
|
_taskScheduler.RefreshSeriesMetadata(refreshSeriesDto.LibraryId, refreshSeriesDto.SeriesId, true);
|
|
return Ok();
|
|
}
|
|
|
|
[Authorize(Policy = "RequireAdminRole")]
|
|
[HttpPost("scan")]
|
|
public ActionResult ScanSeries(RefreshSeriesDto refreshSeriesDto)
|
|
{
|
|
_taskScheduler.ScanSeries(refreshSeriesDto.LibraryId, refreshSeriesDto.SeriesId);
|
|
return Ok();
|
|
}
|
|
|
|
[HttpGet("metadata")]
|
|
public async Task<ActionResult<SeriesMetadataDto>> GetSeriesMetadata(int seriesId)
|
|
{
|
|
var metadata = await _unitOfWork.SeriesRepository.GetSeriesMetadata(seriesId);
|
|
return Ok(metadata);
|
|
}
|
|
|
|
[HttpPost("metadata")]
|
|
public async Task<ActionResult> UpdateSeriesMetadata(UpdateSeriesMetadataDto updateSeriesMetadataDto)
|
|
{
|
|
if (await _seriesService.UpdateSeriesMetadata(updateSeriesMetadataDto))
|
|
{
|
|
return Ok("Successfully updated");
|
|
}
|
|
|
|
return BadRequest("Could not update metadata");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns all Series grouped by the passed Collection Id with Pagination.
|
|
/// </summary>
|
|
/// <param name="collectionId">Collection Id to pull series from</param>
|
|
/// <param name="userParams">Pagination information</param>
|
|
/// <returns></returns>
|
|
[HttpGet("series-by-collection")]
|
|
public async Task<ActionResult<IEnumerable<SeriesDto>>> GetSeriesByCollectionTag(int collectionId, [FromQuery] UserParams userParams)
|
|
{
|
|
var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername());
|
|
var series =
|
|
await _unitOfWork.SeriesRepository.GetSeriesDtoForCollectionAsync(collectionId, userId, userParams);
|
|
|
|
// Apply progress/rating information (I can't work out how to do this in initial query)
|
|
if (series == null) return BadRequest("Could not get series for collection");
|
|
|
|
await _unitOfWork.SeriesRepository.AddSeriesModifiers(userId, series);
|
|
|
|
Response.AddPaginationHeader(series.CurrentPage, series.PageSize, series.TotalCount, series.TotalPages);
|
|
|
|
return Ok(series);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fetches Series for a set of Ids. This will check User for permission access and filter out any Ids that don't exist or
|
|
/// the user does not have access to.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost("series-by-ids")]
|
|
public async Task<ActionResult<IEnumerable<SeriesDto>>> GetAllSeriesById(SeriesByIdsDto dto)
|
|
{
|
|
if (dto.SeriesIds == null) return BadRequest("Must pass seriesIds");
|
|
var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername());
|
|
return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoForIdsAsync(dto.SeriesIds, userId));
|
|
}
|
|
|
|
[HttpGet("age-rating")]
|
|
public ActionResult<string> GetAgeRating(int ageRating)
|
|
{
|
|
var val = (AgeRating) ageRating;
|
|
|
|
return Ok(val.ToDescription());
|
|
}
|
|
|
|
[HttpGet("series-detail")]
|
|
public async Task<ActionResult<SeriesDetailDto>> GetSeriesDetailBreakdown(int seriesId)
|
|
{
|
|
var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername());
|
|
return await _seriesService.GetSeriesDetail(seriesId, userId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the series for the MangaFile id. If the user does not have access (shouldn't happen by the UI),
|
|
/// then null is returned
|
|
/// </summary>
|
|
/// <param name="mangaFileId"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("series-for-mangafile")]
|
|
public async Task<ActionResult<SeriesDto>> GetSeriesForMangaFile(int mangaFileId)
|
|
{
|
|
var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername());
|
|
return Ok(await _unitOfWork.SeriesRepository.GetSeriesForMangaFile(mangaFileId, userId));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the series for the Chapter id. If the user does not have access (shouldn't happen by the UI),
|
|
/// then null is returned
|
|
/// </summary>
|
|
/// <param name="chapterId"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("series-for-chapter")]
|
|
public async Task<ActionResult<SeriesDto>> GetSeriesForChapter(int chapterId)
|
|
{
|
|
var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername());
|
|
return Ok(await _unitOfWork.SeriesRepository.GetSeriesForChapter(chapterId, userId));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fetches the related series for a given series
|
|
/// </summary>
|
|
/// <param name="seriesId"></param>
|
|
/// <param name="relation">Type of Relationship to pull back</param>
|
|
/// <returns></returns>
|
|
[HttpGet("related")]
|
|
public async Task<ActionResult<IEnumerable<SeriesDto>>> GetRelatedSeries(int seriesId, RelationKind relation)
|
|
{
|
|
// Send back a custom DTO with each type or maybe sorted in some way
|
|
var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername());
|
|
return Ok(await _unitOfWork.SeriesRepository.GetSeriesForRelationKind(userId, seriesId, relation));
|
|
}
|
|
|
|
[HttpGet("all-related")]
|
|
public async Task<ActionResult<RelatedSeriesDto>> GetAllRelatedSeries(int seriesId)
|
|
{
|
|
// Send back a custom DTO with each type or maybe sorted in some way
|
|
var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername());
|
|
return Ok(await _unitOfWork.SeriesRepository.GetRelatedSeries(userId, seriesId));
|
|
}
|
|
|
|
[Authorize(Policy="RequireAdminRole")]
|
|
[HttpPost("update-related")]
|
|
public async Task<ActionResult> UpdateRelatedSeries(UpdateRelatedSeriesDto dto)
|
|
{
|
|
var series = await _unitOfWork.SeriesRepository.GetSeriesByIdAsync(dto.SeriesId, SeriesIncludes.Related);
|
|
|
|
UpdateRelationForKind(dto.Adaptations, series.Relations.Where(r => r.RelationKind == RelationKind.Adaptation).ToList(), series, RelationKind.Adaptation);
|
|
UpdateRelationForKind(dto.Characters, series.Relations.Where(r => r.RelationKind == RelationKind.Character).ToList(), series, RelationKind.Character);
|
|
UpdateRelationForKind(dto.Contains, series.Relations.Where(r => r.RelationKind == RelationKind.Contains).ToList(), series, RelationKind.Contains);
|
|
UpdateRelationForKind(dto.Others, series.Relations.Where(r => r.RelationKind == RelationKind.Other).ToList(), series, RelationKind.Other);
|
|
UpdateRelationForKind(dto.SideStories, series.Relations.Where(r => r.RelationKind == RelationKind.SideStory).ToList(), series, RelationKind.SideStory);
|
|
UpdateRelationForKind(dto.SpinOffs, series.Relations.Where(r => r.RelationKind == RelationKind.SpinOff).ToList(), series, RelationKind.SpinOff);
|
|
UpdateRelationForKind(dto.AlternativeSettings, series.Relations.Where(r => r.RelationKind == RelationKind.AlternativeSetting).ToList(), series, RelationKind.AlternativeSetting);
|
|
UpdateRelationForKind(dto.AlternativeVersions, series.Relations.Where(r => r.RelationKind == RelationKind.AlternativeVersion).ToList(), series, RelationKind.AlternativeVersion);
|
|
UpdateRelationForKind(dto.Doujinshis, series.Relations.Where(r => r.RelationKind == RelationKind.Doujinshi).ToList(), series, RelationKind.Doujinshi);
|
|
UpdateRelationForKind(dto.Prequels, series.Relations.Where(r => r.RelationKind == RelationKind.Prequel).ToList(), series, RelationKind.Prequel);
|
|
UpdateRelationForKind(dto.Sequels, series.Relations.Where(r => r.RelationKind == RelationKind.Sequel).ToList(), series, RelationKind.Sequel);
|
|
|
|
if (!_unitOfWork.HasChanges()) return Ok();
|
|
if (await _unitOfWork.CommitAsync()) return Ok();
|
|
|
|
|
|
return BadRequest("There was an issue updating relationships");
|
|
}
|
|
|
|
private void UpdateRelationForKind(IList<int> dtoTargetSeriesIds, IEnumerable<SeriesRelation> adaptations, Series series, RelationKind kind)
|
|
{
|
|
foreach (var adaptation in adaptations.Where(adaptation => !dtoTargetSeriesIds.Contains(adaptation.TargetSeriesId)))
|
|
{
|
|
// If the seriesId isn't in dto, it means we've removed or reclassified
|
|
series.Relations.Remove(adaptation);
|
|
}
|
|
|
|
// At this point, we only have things to add
|
|
foreach (var targetSeriesId in dtoTargetSeriesIds)
|
|
{
|
|
// This ensures we don't allow any duplicates to be added
|
|
if (series.Relations.SingleOrDefault(r =>
|
|
r.RelationKind == kind && r.TargetSeriesId == targetSeriesId) !=
|
|
null) continue;
|
|
|
|
series.Relations.Add(new SeriesRelation()
|
|
{
|
|
Series = series,
|
|
SeriesId = series.Id,
|
|
TargetSeriesId = targetSeriesId,
|
|
RelationKind = kind
|
|
});
|
|
_unitOfWork.SeriesRepository.Update(series);
|
|
}
|
|
}
|
|
}
|
|
}
|