mirror of
				https://github.com/Kareadita/Kavita.git
				synced 2025-11-04 03:27:05 -05:00 
			
		
		
		
	* Implemented the first version of dynamic filtering which is all Extension based. * Implemented basic generic property filter for expanded metadata filtering. * Fixed up the code to allow for nested properties and fixed up the Contains to work only for IList's * Started refactoring for the new approach * More progress, need to rethink a few filters like read progress to be % based and people needs to be more explicit. * Refactored most of the existing filtering operations into dedicate extensions for the appropriate comparisons. People still need to be reworked to be more dynamic. * Fixed a bug with continue point where it fails on chapters or volumes tagged with a range * Wired up a basic api path to start building groups. No and/or support yet. * Started on the UI * Made a bit of progress on the UI as I'm putting the pieces together about how to design it. * Refactored names to make it more consistent. New thinking is we will have one row that will take a filter statement and manipulate it. It will emit said statement and a builder will turn into the higher level statement. * Started working on updating code to use new inject() method. * Fixed the code to switch the comparisons. * Added dynamic input structure in and moved add/remove to the builder. * Fixed an enum bug * Hooked in basic dropdown support that is dynamic to the field. Only language is missing as that needs a DTO change (but don't want to break API) * Fixed a bug where dropdown options wouldn't re-populate when switching fields that are both dropdowns * Started adding metadata builder * Fixed when typing on filter row the focus resetting * Refactored to add an additional component which handles the compounding of filter rows. * Started hooking up v2 dto in the UI to send to the backend. * Started working on building group UI for and/or support. * Lots of backend code fixes to ensure OR and AND statements combine correctly. * More trying to figure out how to write the UI code * Started debugging to remember what I was last doing. * Lots of progress towards building out the UI recursively * I got the dto to build and propagate up the chain * Started hooking up to the actual api to fetch the data. * Basic wire up to the backend is working. * HasName is now complete * Refactored SortOptions code into an extension and streamlined LimitTo to the correct place. * Fixed a bug where Library Filters from the Group weren't actually being taken into account. * Refactored a lot of code so builder will now export the full dto. * Cleaned up the data flow from metadata filter to library detail * Got the dropdown to load preset values on first load, but now it triggers twice. * Changed so when you add a new filter, it does it at top and fixed remove * Fixed the remove button being on the wrong row * Cleaned up the arrays to make it easier to manage * Cleaned up some of the backend to ensure it doesn't throw an incorrect exception * I'm starting to tread water, taking a break * Fixed a merge issue * Cleaned up Docker checks. * Default IpAddresses to empty string. * Refactored IsDocker to be completely static * Figured out the issue with the dropdown not working. * Almost got it, but the event isn't being called. * I think i might try something else. This doesn't seem to be working. * On the new implementation, implemented remove group. * Use enums to reduce copy/paste * the new system is working pretty well, ill go with it and move on. Can alwasy refactor. * Code is totally broken, but working the cache resume code with some hiccups. * I need to take a break * Stashing my broken code. I have an idea on how to serialize to the URL, but I need to rearchitect a lot. * Reverted last commit * remove domain * Fixed up some hardcoded caching. I'm giving up on this implementation and going to a simpler version * Refactored the backend to just allow flat filtering. * Started refactoring the components to make it flat filtering only. * Finished refactoring so that the base preset case will render. * Implemented basic query functionality on desktop. Clear needs some work and url code. * Some cleanup * Working on filtering url encode/decode * Interacting with filters now saves to url and can be reloaded from the url. Named filters is not hooked up. * Fixed a double load on the library detail page. * Moved the library filtering code out of the FilterBuilder as it needs to be handled differently. * Fixed up how we handle library statements in the filter. * Fixed up how links that perform a filter work. * Refactored a bunch of linking to a search page. * LimitTo works, my css however does not. * Switched some code to use localized strings. * Cleaned up some css * Hooked up Languages and put some additional code in so that Languages will return invalid Language codes back. * Removed a duplicate language signature. * Hooked up ability to preload collection tag. * Want To Read is converted * Converted lots of code to new filtering system. Need to do Bookmarks. * Fixed a potential bug with default filter creation. * Hooked up the ability to disable certain filter fields from appearing. * Added mobile drawer code and a hook for Robbie to take a look for some css. * Converted the APIs for dashboard along with other safety fixes to ensure bad data doesn't break any of the filtering apis * Added the backend code to handle summary query * Converted Want to Read api properly now. * Fixed the HasReadingProgress query * Hooked back the Reading Progress for legacy APIs * Fixed some bad localization strings * Wrote the filtering code for all-bookmarks. * OPDS is now using the new filter * Fixed OPDS reading lists and covers not sending their images. * Fixed up the OPDS feed and fixed a bug where libraries also weren't sending their images over OPDS * All but dropdown options have been validated and tested. * Fixed up some default cases for setting up the filter. * Sorted filter fields and re-keyed to be better suited based on user's needs. Fixed a bug where OPDS Series (from library view) wasn't showing the summary. Moved the (Format) from the title to the description to make the UX much better for OPDS. MOved * don't send empty summaries in the new summary formatting * Fixed up some default cases for setting up the filter. * Fixed the reset button * Fixed infinite scroller not having correct scope key * Added localization to the new components and removed old debug code * Styling fixes * Fixed deep linking across the app. Made it so you can click Characters from Reading list and open a filtered search. * A bit of styling for mobile * Don't show language if it's not properly set --------- Co-authored-by: Robbie Davis <robbie@therobbiedavis.com>
		
			
				
	
	
		
			876 lines
		
	
	
		
			37 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			876 lines
		
	
	
		
			37 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System;
 | 
						|
using System.Collections.Generic;
 | 
						|
using System.IO;
 | 
						|
using System.Linq;
 | 
						|
using System.Threading.Tasks;
 | 
						|
using API.Constants;
 | 
						|
using API.Data;
 | 
						|
using API.Data.Repositories;
 | 
						|
using API.DTOs;
 | 
						|
using API.DTOs.Filtering;
 | 
						|
using API.DTOs.Filtering.v2;
 | 
						|
using API.DTOs.Reader;
 | 
						|
using API.Entities;
 | 
						|
using API.Entities.Enums;
 | 
						|
using API.Extensions;
 | 
						|
using API.Services;
 | 
						|
using API.Services.Plus;
 | 
						|
using API.SignalR;
 | 
						|
using Hangfire;
 | 
						|
using Kavita.Common;
 | 
						|
using Microsoft.AspNetCore.Authorization;
 | 
						|
using Microsoft.AspNetCore.Mvc;
 | 
						|
using Microsoft.Extensions.Logging;
 | 
						|
using Microsoft.IdentityModel.Tokens;
 | 
						|
using MimeTypes;
 | 
						|
 | 
						|
namespace API.Controllers;
 | 
						|
 | 
						|
/// <summary>
 | 
						|
/// For all things regarding reading, mainly focusing on non-Book related entities
 | 
						|
/// </summary>
 | 
						|
public class ReaderController : BaseApiController
 | 
						|
{
 | 
						|
    private readonly ICacheService _cacheService;
 | 
						|
    private readonly IUnitOfWork _unitOfWork;
 | 
						|
    private readonly ILogger<ReaderController> _logger;
 | 
						|
    private readonly IReaderService _readerService;
 | 
						|
    private readonly IBookmarkService _bookmarkService;
 | 
						|
    private readonly IAccountService _accountService;
 | 
						|
    private readonly IEventHub _eventHub;
 | 
						|
    private readonly IScrobblingService _scrobblingService;
 | 
						|
    private readonly ILocalizationService _localizationService;
 | 
						|
 | 
						|
    /// <inheritdoc />
 | 
						|
    public ReaderController(ICacheService cacheService,
 | 
						|
        IUnitOfWork unitOfWork, ILogger<ReaderController> logger,
 | 
						|
        IReaderService readerService, IBookmarkService bookmarkService,
 | 
						|
        IAccountService accountService, IEventHub eventHub,
 | 
						|
        IScrobblingService scrobblingService,
 | 
						|
        ILocalizationService localizationService)
 | 
						|
    {
 | 
						|
        _cacheService = cacheService;
 | 
						|
        _unitOfWork = unitOfWork;
 | 
						|
        _logger = logger;
 | 
						|
        _readerService = readerService;
 | 
						|
        _bookmarkService = bookmarkService;
 | 
						|
        _accountService = accountService;
 | 
						|
        _eventHub = eventHub;
 | 
						|
        _scrobblingService = scrobblingService;
 | 
						|
        _localizationService = localizationService;
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Returns the PDF for the chapterId.
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="chapterId"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpGet("pdf")]
 | 
						|
    [ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = new []{"chapterId", "apiKey"})]
 | 
						|
    public async Task<ActionResult> GetPdf(int chapterId, string apiKey)
 | 
						|
    {
 | 
						|
        if (await _unitOfWork.UserRepository.GetUserIdByApiKeyAsync(apiKey) == 0) return BadRequest();
 | 
						|
        var chapter = await _cacheService.Ensure(chapterId);
 | 
						|
        if (chapter == null) return NoContent();
 | 
						|
 | 
						|
        // Validate the user has access to the PDF
 | 
						|
        var series = await _unitOfWork.SeriesRepository.GetSeriesForChapter(chapter.Id,
 | 
						|
            await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername()));
 | 
						|
        if (series == null) return BadRequest(await _localizationService.Translate(User.GetUserId(), "invalid-access"));
 | 
						|
 | 
						|
        try
 | 
						|
        {
 | 
						|
 | 
						|
            var path = _cacheService.GetCachedFile(chapter);
 | 
						|
            if (string.IsNullOrEmpty(path) || !System.IO.File.Exists(path)) return BadRequest(await _localizationService.Translate(User.GetUserId(), "pdf-doesnt-exist"));
 | 
						|
 | 
						|
            return PhysicalFile(path, MimeTypeMap.GetMimeType(Path.GetExtension(path)), Path.GetFileName(path), true);
 | 
						|
        }
 | 
						|
        catch (Exception)
 | 
						|
        {
 | 
						|
            _cacheService.CleanupChapters(new []{ chapterId });
 | 
						|
            throw;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Returns an image for a given chapter. Will perform bounding checks
 | 
						|
    /// </summary>
 | 
						|
    /// <remarks>This will cache the chapter images for reading</remarks>
 | 
						|
    /// <param name="chapterId">Chapter Id</param>
 | 
						|
    /// <param name="page">Page in question</param>
 | 
						|
    /// <param name="apiKey">User's API Key for authentication</param>
 | 
						|
    /// <param name="extractPdf">Should Kavita extract pdf into images. Defaults to false.</param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpGet("image")]
 | 
						|
    [ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = new []{"chapterId","page", "extractPdf", "apiKey"})]
 | 
						|
    [AllowAnonymous]
 | 
						|
    public async Task<ActionResult> GetImage(int chapterId, int page, string apiKey, bool extractPdf = false)
 | 
						|
    {
 | 
						|
        if (page < 0) page = 0;
 | 
						|
        var userId = await _unitOfWork.UserRepository.GetUserIdByApiKeyAsync(apiKey);
 | 
						|
        if (userId == 0) return BadRequest();
 | 
						|
        var chapter = await _cacheService.Ensure(chapterId, extractPdf);
 | 
						|
        if (chapter == null) return NoContent();
 | 
						|
 | 
						|
        try
 | 
						|
        {
 | 
						|
            var path = _cacheService.GetCachedPagePath(chapter.Id, page);
 | 
						|
            if (string.IsNullOrEmpty(path) || !System.IO.File.Exists(path))
 | 
						|
                return BadRequest(await _localizationService.Translate(userId, "no-image-for-page", page));
 | 
						|
            var format = Path.GetExtension(path);
 | 
						|
 | 
						|
            return PhysicalFile(path, MimeTypeMap.GetMimeType(format), Path.GetFileName(path), true);
 | 
						|
        }
 | 
						|
        catch (Exception)
 | 
						|
        {
 | 
						|
            _cacheService.CleanupChapters(new []{ chapterId });
 | 
						|
            throw;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Returns a thumbnail for the given page number
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="chapterId"></param>
 | 
						|
    /// <param name="pageNum"></param>
 | 
						|
    /// <param name="apiKey"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpGet("thumbnail")]
 | 
						|
    [ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = new []{"chapterId", "pageNum", "apiKey"})]
 | 
						|
    [AllowAnonymous]
 | 
						|
    public async Task<ActionResult> GetThumbnail(int chapterId, int pageNum, string apiKey)
 | 
						|
    {
 | 
						|
        var userId = await _unitOfWork.UserRepository.GetUserIdByApiKeyAsync(apiKey);
 | 
						|
        if (userId == 0) return BadRequest();
 | 
						|
        var chapter = await _cacheService.Ensure(chapterId, true);
 | 
						|
        if (chapter == null) return NoContent();
 | 
						|
        var images = _cacheService.GetCachedPages(chapterId);
 | 
						|
 | 
						|
        var path = await _readerService.GetThumbnail(chapter, pageNum, images);
 | 
						|
        var format = Path.GetExtension(path);
 | 
						|
        return PhysicalFile(path, MimeTypeMap.GetMimeType(format), Path.GetFileName(path), true);
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Returns an image for a given bookmark series. Side effect: This will cache the bookmark images for reading.
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="seriesId"></param>
 | 
						|
    /// <param name="apiKey">Api key for the user the bookmarks are on</param>
 | 
						|
    /// <param name="page"></param>
 | 
						|
    /// <remarks>We must use api key as bookmarks could be leaked to other users via the API</remarks>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpGet("bookmark-image")]
 | 
						|
    [ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = new []{"seriesId", "page", "apiKey"})]
 | 
						|
    [AllowAnonymous]
 | 
						|
    public async Task<ActionResult> GetBookmarkImage(int seriesId, string apiKey, int page)
 | 
						|
    {
 | 
						|
        if (page < 0) page = 0;
 | 
						|
        var userId = await _unitOfWork.UserRepository.GetUserIdByApiKeyAsync(apiKey);
 | 
						|
        if (userId == 0) return Unauthorized();
 | 
						|
 | 
						|
        var totalPages = await _cacheService.CacheBookmarkForSeries(userId, seriesId);
 | 
						|
        if (page > totalPages)
 | 
						|
        {
 | 
						|
            page = totalPages;
 | 
						|
        }
 | 
						|
 | 
						|
        try
 | 
						|
        {
 | 
						|
            var path = _cacheService.GetCachedBookmarkPagePath(seriesId, page);
 | 
						|
            if (string.IsNullOrEmpty(path) || !System.IO.File.Exists(path)) return BadRequest(await _localizationService.Translate(userId, "no-image-for-page", page));
 | 
						|
            var format = Path.GetExtension(path);
 | 
						|
 | 
						|
            return PhysicalFile(path, MimeTypeMap.GetMimeType(format), Path.GetFileName(path));
 | 
						|
        }
 | 
						|
        catch (Exception)
 | 
						|
        {
 | 
						|
            _cacheService.CleanupBookmarks(new []{ seriesId });
 | 
						|
            throw;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Returns the file dimensions for all pages in a chapter. If the underlying chapter is PDF, use extractPDF to unpack as images.
 | 
						|
    /// </summary>
 | 
						|
    /// <remarks>This has a side effect of caching the images.
 | 
						|
    /// This will only be populated on archive filetypes and not in bookmark mode</remarks>
 | 
						|
    /// <param name="chapterId"></param>
 | 
						|
    /// <param name="extractPdf"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpGet("file-dimensions")]
 | 
						|
    [ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = new []{"chapterId", "extractPdf"})]
 | 
						|
    public async Task<ActionResult<IEnumerable<FileDimensionDto>>> GetFileDimensions(int chapterId, bool extractPdf = false)
 | 
						|
    {
 | 
						|
        if (chapterId <= 0) return ArraySegment<FileDimensionDto>.Empty;
 | 
						|
        var chapter = await _cacheService.Ensure(chapterId, extractPdf);
 | 
						|
        if (chapter == null) return NoContent();
 | 
						|
        return Ok(_cacheService.GetCachedFileDimensions(_cacheService.GetCachePath(chapterId)));
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Returns various information about a Chapter. Side effect: This will cache the chapter images for reading.
 | 
						|
    /// </summary>
 | 
						|
    /// <remarks>This is generally the first call when attempting to read to allow pre-generation of assets needed for reading</remarks>
 | 
						|
    /// <param name="chapterId"></param>
 | 
						|
    /// <param name="extractPdf">Should Kavita extract pdf into images. Defaults to false.</param>
 | 
						|
    /// <param name="includeDimensions">Include file dimensions. Only useful for image based reading</param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpGet("chapter-info")]
 | 
						|
    [ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = new []{"chapterId", "extractPdf", "includeDimensions"})]
 | 
						|
    public async Task<ActionResult<ChapterInfoDto>> GetChapterInfo(int chapterId, bool extractPdf = false, bool includeDimensions = false)
 | 
						|
    {
 | 
						|
        if (chapterId <= 0) return Ok(null); // This can happen occasionally from UI, we should just ignore
 | 
						|
        var chapter = await _cacheService.Ensure(chapterId, extractPdf);
 | 
						|
        if (chapter == null) return NoContent();
 | 
						|
 | 
						|
        var dto = await _unitOfWork.ChapterRepository.GetChapterInfoDtoAsync(chapterId);
 | 
						|
        if (dto == null) return BadRequest(await _localizationService.Translate(User.GetUserId(), "perform-scan"));
 | 
						|
        var mangaFile = chapter.Files.First();
 | 
						|
 | 
						|
        var info = new ChapterInfoDto()
 | 
						|
        {
 | 
						|
            ChapterNumber = dto.ChapterNumber,
 | 
						|
            VolumeNumber = dto.VolumeNumber,
 | 
						|
            VolumeId = dto.VolumeId,
 | 
						|
            FileName = Path.GetFileName(mangaFile.FilePath),
 | 
						|
            SeriesName = dto.SeriesName,
 | 
						|
            SeriesFormat = dto.SeriesFormat,
 | 
						|
            SeriesId = dto.SeriesId,
 | 
						|
            LibraryId = dto.LibraryId,
 | 
						|
            IsSpecial = dto.IsSpecial,
 | 
						|
            Pages = dto.Pages,
 | 
						|
            ChapterTitle = dto.ChapterTitle ?? string.Empty,
 | 
						|
            Subtitle = string.Empty,
 | 
						|
            Title = dto.SeriesName,
 | 
						|
        };
 | 
						|
 | 
						|
        if (includeDimensions)
 | 
						|
        {
 | 
						|
            info.PageDimensions = _cacheService.GetCachedFileDimensions(_cacheService.GetCachePath(chapterId));
 | 
						|
            info.DoublePairs = _readerService.GetPairs(info.PageDimensions);
 | 
						|
        }
 | 
						|
 | 
						|
        if (info.ChapterTitle is {Length: > 0}) {
 | 
						|
            info.Title += " - " + info.ChapterTitle;
 | 
						|
        }
 | 
						|
 | 
						|
        if (info.IsSpecial && dto.VolumeNumber.Equals(Services.Tasks.Scanner.Parser.Parser.DefaultVolume))
 | 
						|
        {
 | 
						|
            info.Subtitle = info.FileName;
 | 
						|
        } else if (!info.IsSpecial && info.VolumeNumber.Equals(Services.Tasks.Scanner.Parser.Parser.DefaultVolume))
 | 
						|
        {
 | 
						|
            info.Subtitle = ReaderService.FormatChapterName(info.LibraryType, true, true) + info.ChapterNumber;
 | 
						|
        }
 | 
						|
        else
 | 
						|
        {
 | 
						|
            //info.Subtitle = await _localizationService.Translate(User.GetUserId(), "volume-num", info.VolumeNumber);
 | 
						|
            info.Subtitle = $"Volume {info.VolumeNumber}";
 | 
						|
            if (!info.ChapterNumber.Equals(Services.Tasks.Scanner.Parser.Parser.DefaultChapter))
 | 
						|
            {
 | 
						|
                info.Subtitle += " " + ReaderService.FormatChapterName(info.LibraryType, true, true) +
 | 
						|
                                 info.ChapterNumber;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        return Ok(info);
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Returns various information about all bookmark files for a Series. Side effect: This will cache the bookmark images for reading.
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="seriesId">Series Id for all bookmarks</param>
 | 
						|
    /// <param name="includeDimensions">Include file dimensions (extra I/O). Defaults to true.</param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpGet("bookmark-info")]
 | 
						|
    [ResponseCache(CacheProfileName = ResponseCacheProfiles.Hour, VaryByQueryKeys = new []{"seriesId", "includeDimensions"})]
 | 
						|
    public async Task<ActionResult<BookmarkInfoDto>> GetBookmarkInfo(int seriesId, bool includeDimensions = true)
 | 
						|
    {
 | 
						|
        var totalPages = await _cacheService.CacheBookmarkForSeries(User.GetUserId(), seriesId);
 | 
						|
        var series = await _unitOfWork.SeriesRepository.GetSeriesByIdAsync(seriesId, SeriesIncludes.None);
 | 
						|
 | 
						|
        var info = new BookmarkInfoDto()
 | 
						|
        {
 | 
						|
            SeriesName = series!.Name,
 | 
						|
            SeriesFormat = series.Format,
 | 
						|
            SeriesId = series.Id,
 | 
						|
            LibraryId = series.LibraryId,
 | 
						|
            Pages = totalPages,
 | 
						|
        };
 | 
						|
 | 
						|
        if (includeDimensions)
 | 
						|
        {
 | 
						|
            info.PageDimensions = _cacheService.GetCachedFileDimensions(_cacheService.GetBookmarkCachePath(seriesId));
 | 
						|
            info.DoublePairs = _readerService.GetPairs(info.PageDimensions);
 | 
						|
        }
 | 
						|
 | 
						|
        return Ok(info);
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Marks a Series as read. All volumes and chapters will be marked as read during this process.
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="markReadDto"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpPost("mark-read")]
 | 
						|
    public async Task<ActionResult> MarkRead(MarkReadDto markReadDto)
 | 
						|
    {
 | 
						|
        var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Progress);
 | 
						|
        if (user == null) return Unauthorized();
 | 
						|
        try
 | 
						|
        {
 | 
						|
            await _readerService.MarkSeriesAsRead(user, markReadDto.SeriesId);
 | 
						|
        }
 | 
						|
        catch (KavitaException ex)
 | 
						|
        {
 | 
						|
            return BadRequest(await _localizationService.Translate(User.GetUserId(), ex.Message));
 | 
						|
        }
 | 
						|
 | 
						|
        if (!await _unitOfWork.CommitAsync()) return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-read-progress"));
 | 
						|
 | 
						|
        BackgroundJob.Enqueue(() => _scrobblingService.ScrobbleReadingUpdate(user.Id, markReadDto.SeriesId));
 | 
						|
        BackgroundJob.Enqueue(() => _unitOfWork.SeriesRepository.ClearOnDeckRemoval(markReadDto.SeriesId, user.Id));
 | 
						|
        return Ok();
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Marks a Series as Unread. All volumes and chapters will be marked as unread during this process.
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="markReadDto"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpPost("mark-unread")]
 | 
						|
    public async Task<ActionResult> MarkUnread(MarkReadDto markReadDto)
 | 
						|
    {
 | 
						|
        var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Progress);
 | 
						|
        if (user == null) return Unauthorized();
 | 
						|
        await _readerService.MarkSeriesAsUnread(user, markReadDto.SeriesId);
 | 
						|
 | 
						|
        if (!await _unitOfWork.CommitAsync()) return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-read-progress"));
 | 
						|
 | 
						|
        BackgroundJob.Enqueue(() => _scrobblingService.ScrobbleReadingUpdate(user.Id, markReadDto.SeriesId));
 | 
						|
        return Ok();
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Marks all chapters within a volume as unread
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="markVolumeReadDto"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpPost("mark-volume-unread")]
 | 
						|
    public async Task<ActionResult> MarkVolumeAsUnread(MarkVolumeReadDto markVolumeReadDto)
 | 
						|
    {
 | 
						|
        var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Progress);
 | 
						|
        if (user == null) return Unauthorized();
 | 
						|
 | 
						|
        var chapters = await _unitOfWork.ChapterRepository.GetChaptersAsync(markVolumeReadDto.VolumeId);
 | 
						|
        await _readerService.MarkChaptersAsUnread(user, markVolumeReadDto.SeriesId, chapters);
 | 
						|
 | 
						|
        if (await _unitOfWork.CommitAsync())
 | 
						|
        {
 | 
						|
            BackgroundJob.Enqueue(() => _scrobblingService.ScrobbleReadingUpdate(user.Id, markVolumeReadDto.SeriesId));
 | 
						|
            return Ok();
 | 
						|
        }
 | 
						|
 | 
						|
        return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-read-progress"));
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Marks all chapters within a volume as Read
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="markVolumeReadDto"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpPost("mark-volume-read")]
 | 
						|
    public async Task<ActionResult> MarkVolumeAsRead(MarkVolumeReadDto markVolumeReadDto)
 | 
						|
    {
 | 
						|
        var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Progress);
 | 
						|
 | 
						|
        var chapters = await _unitOfWork.ChapterRepository.GetChaptersAsync(markVolumeReadDto.VolumeId);
 | 
						|
        if (user == null) return Unauthorized();
 | 
						|
        try
 | 
						|
        {
 | 
						|
            await _readerService.MarkChaptersAsRead(user, markVolumeReadDto.SeriesId, chapters);
 | 
						|
        }
 | 
						|
        catch (KavitaException ex)
 | 
						|
        {
 | 
						|
            return BadRequest(await _localizationService.Translate(User.GetUserId(), ex.Message));
 | 
						|
        }
 | 
						|
        await _eventHub.SendMessageAsync(MessageFactory.UserProgressUpdate,
 | 
						|
            MessageFactory.UserProgressUpdateEvent(user.Id, user.UserName!, markVolumeReadDto.SeriesId,
 | 
						|
                markVolumeReadDto.VolumeId, 0, chapters.Sum(c => c.Pages)));
 | 
						|
 | 
						|
        if (!await _unitOfWork.CommitAsync()) return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-read-progress"));
 | 
						|
 | 
						|
        BackgroundJob.Enqueue(() => _scrobblingService.ScrobbleReadingUpdate(user.Id, markVolumeReadDto.SeriesId));
 | 
						|
        BackgroundJob.Enqueue(() => _unitOfWork.SeriesRepository.ClearOnDeckRemoval(markVolumeReadDto.SeriesId, user.Id));
 | 
						|
        return Ok();
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Marks all chapters within a list of volumes as Read. All volumes must belong to the same Series.
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="dto"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpPost("mark-multiple-read")]
 | 
						|
    public async Task<ActionResult> MarkMultipleAsRead(MarkVolumesReadDto dto)
 | 
						|
    {
 | 
						|
        var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Progress);
 | 
						|
        if (user == null) return Unauthorized();
 | 
						|
        user.Progresses ??= new List<AppUserProgress>();
 | 
						|
 | 
						|
        var chapterIds = await _unitOfWork.VolumeRepository.GetChapterIdsByVolumeIds(dto.VolumeIds);
 | 
						|
        foreach (var chapterId in dto.ChapterIds)
 | 
						|
        {
 | 
						|
            chapterIds.Add(chapterId);
 | 
						|
        }
 | 
						|
        var chapters = await _unitOfWork.ChapterRepository.GetChaptersByIdsAsync(chapterIds);
 | 
						|
        await _readerService.MarkChaptersAsRead(user, dto.SeriesId, chapters.ToList());
 | 
						|
 | 
						|
        if (!await _unitOfWork.CommitAsync()) return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-read-progress"));
 | 
						|
        BackgroundJob.Enqueue(() => _scrobblingService.ScrobbleReadingUpdate(user.Id, dto.SeriesId));
 | 
						|
        BackgroundJob.Enqueue(() => _unitOfWork.SeriesRepository.ClearOnDeckRemoval(dto.SeriesId, user.Id));
 | 
						|
        return Ok();
 | 
						|
 | 
						|
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Marks all chapters within a list of volumes as Unread. All volumes must belong to the same Series.
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="dto"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpPost("mark-multiple-unread")]
 | 
						|
    public async Task<ActionResult> MarkMultipleAsUnread(MarkVolumesReadDto dto)
 | 
						|
    {
 | 
						|
        var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Progress);
 | 
						|
        if (user == null) return Unauthorized();
 | 
						|
        user.Progresses ??= new List<AppUserProgress>();
 | 
						|
 | 
						|
        var chapterIds = await _unitOfWork.VolumeRepository.GetChapterIdsByVolumeIds(dto.VolumeIds);
 | 
						|
        foreach (var chapterId in dto.ChapterIds)
 | 
						|
        {
 | 
						|
            chapterIds.Add(chapterId);
 | 
						|
        }
 | 
						|
        var chapters = await _unitOfWork.ChapterRepository.GetChaptersByIdsAsync(chapterIds);
 | 
						|
        await _readerService.MarkChaptersAsUnread(user, dto.SeriesId, chapters.ToList());
 | 
						|
 | 
						|
        if (await _unitOfWork.CommitAsync())
 | 
						|
        {
 | 
						|
            BackgroundJob.Enqueue(() => _scrobblingService.ScrobbleReadingUpdate(user.Id, dto.SeriesId));
 | 
						|
            return Ok();
 | 
						|
        }
 | 
						|
 | 
						|
        return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-read-progress"));
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Marks all chapters within a list of series as Read.
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="dto"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpPost("mark-multiple-series-read")]
 | 
						|
    public async Task<ActionResult> MarkMultipleSeriesAsRead(MarkMultipleSeriesAsReadDto dto)
 | 
						|
    {
 | 
						|
        var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Progress);
 | 
						|
        if (user == null) return Unauthorized();
 | 
						|
        user.Progresses ??= new List<AppUserProgress>();
 | 
						|
 | 
						|
        var volumes = await _unitOfWork.VolumeRepository.GetVolumesForSeriesAsync(dto.SeriesIds.ToArray(), true);
 | 
						|
        foreach (var volume in volumes)
 | 
						|
        {
 | 
						|
            await _readerService.MarkChaptersAsRead(user, volume.SeriesId, volume.Chapters);
 | 
						|
        }
 | 
						|
 | 
						|
        if (!await _unitOfWork.CommitAsync()) return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-read-progress"));
 | 
						|
 | 
						|
        foreach (var sId in dto.SeriesIds)
 | 
						|
        {
 | 
						|
            BackgroundJob.Enqueue(() => _scrobblingService.ScrobbleReadingUpdate(user.Id, sId));
 | 
						|
            BackgroundJob.Enqueue(() => _unitOfWork.SeriesRepository.ClearOnDeckRemoval(sId, user.Id));
 | 
						|
        }
 | 
						|
        return Ok();
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Marks all chapters within a list of series as Unread.
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="dto"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpPost("mark-multiple-series-unread")]
 | 
						|
    public async Task<ActionResult> MarkMultipleSeriesAsUnread(MarkMultipleSeriesAsReadDto dto)
 | 
						|
    {
 | 
						|
        var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Progress);
 | 
						|
        if (user == null) return Unauthorized();
 | 
						|
        user.Progresses ??= new List<AppUserProgress>();
 | 
						|
 | 
						|
        var volumes = await _unitOfWork.VolumeRepository.GetVolumesForSeriesAsync(dto.SeriesIds.ToArray(), true);
 | 
						|
        foreach (var volume in volumes)
 | 
						|
        {
 | 
						|
            await _readerService.MarkChaptersAsUnread(user, volume.SeriesId, volume.Chapters);
 | 
						|
        }
 | 
						|
 | 
						|
        if (await _unitOfWork.CommitAsync())
 | 
						|
        {
 | 
						|
            foreach (var sId in dto.SeriesIds)
 | 
						|
            {
 | 
						|
                BackgroundJob.Enqueue(() => _scrobblingService.ScrobbleReadingUpdate(user.Id, sId));
 | 
						|
            }
 | 
						|
            return Ok();
 | 
						|
        }
 | 
						|
 | 
						|
        return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-read-progress"));
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Returns Progress (page number) for a chapter for the logged in user
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="chapterId"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpGet("get-progress")]
 | 
						|
    public async Task<ActionResult<ProgressDto>> GetProgress(int chapterId)
 | 
						|
    {
 | 
						|
        var progress = await _unitOfWork.AppUserProgressRepository.GetUserProgressDtoAsync(chapterId, User.GetUserId());
 | 
						|
        if (progress == null) return Ok(new ProgressDto()
 | 
						|
        {
 | 
						|
            PageNum = 0,
 | 
						|
            ChapterId = chapterId,
 | 
						|
            VolumeId = 0,
 | 
						|
            SeriesId = 0
 | 
						|
        });
 | 
						|
        return Ok(progress);
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Save page against Chapter for logged in user
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="progressDto"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpPost("progress")]
 | 
						|
    public async Task<ActionResult> SaveProgress(ProgressDto progressDto)
 | 
						|
    {
 | 
						|
        var userId = User.GetUserId();
 | 
						|
        if (!await _readerService.SaveReadingProgress(progressDto, userId))
 | 
						|
            return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-read-progress"));
 | 
						|
 | 
						|
 | 
						|
        return Ok(true);
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Continue point is the chapter which you should start reading again from. If there is no progress on a series, then the first chapter will be returned (non-special unless only specials).
 | 
						|
    /// Otherwise, loop through the chapters and volumes in order to find the next chapter which has progress.
 | 
						|
    /// </summary>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpGet("continue-point")]
 | 
						|
    public async Task<ActionResult<ChapterDto>> GetContinuePoint(int seriesId)
 | 
						|
    {
 | 
						|
        return Ok(await _readerService.GetContinuePoint(seriesId, User.GetUserId()));
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Returns if the user has reading progress on the Series
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="seriesId"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpGet("has-progress")]
 | 
						|
    public async Task<ActionResult<bool>> HasProgress(int seriesId)
 | 
						|
    {
 | 
						|
        return Ok(await _unitOfWork.AppUserProgressRepository.HasAnyProgressOnSeriesAsync(seriesId, User.GetUserId()));
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Returns a list of bookmarked pages for a given Chapter
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="chapterId"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpGet("chapter-bookmarks")]
 | 
						|
    public async Task<ActionResult<IEnumerable<BookmarkDto>>> GetBookmarks(int chapterId)
 | 
						|
    {
 | 
						|
        return Ok(await _unitOfWork.UserRepository.GetBookmarkDtosForChapter(User.GetUserId(), chapterId));
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Returns a list of all bookmarked pages for a User
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="filterDto">Only supports SeriesNameQuery</param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpPost("all-bookmarks")]
 | 
						|
    public async Task<ActionResult<IEnumerable<BookmarkDto>>> GetAllBookmarks(FilterV2Dto filterDto)
 | 
						|
    {
 | 
						|
        return Ok(await _unitOfWork.UserRepository.GetAllBookmarkDtos(User.GetUserId(), filterDto));
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Removes all bookmarks for all chapters linked to a Series
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="dto"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpPost("remove-bookmarks")]
 | 
						|
    public async Task<ActionResult> RemoveBookmarks(RemoveBookmarkForSeriesDto dto)
 | 
						|
    {
 | 
						|
        var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Bookmarks);
 | 
						|
        if (user == null) return Unauthorized();
 | 
						|
        if (user.Bookmarks == null) return Ok(await _localizationService.Translate(User.GetUserId(), "nothing-to-do"));
 | 
						|
 | 
						|
        try
 | 
						|
        {
 | 
						|
            var bookmarksToRemove = user.Bookmarks.Where(bmk => bmk.SeriesId == dto.SeriesId).ToList();
 | 
						|
            user.Bookmarks = user.Bookmarks.Where(bmk => bmk.SeriesId != dto.SeriesId).ToList();
 | 
						|
            _unitOfWork.UserRepository.Update(user);
 | 
						|
 | 
						|
            if (!_unitOfWork.HasChanges() || await _unitOfWork.CommitAsync())
 | 
						|
            {
 | 
						|
                try
 | 
						|
                {
 | 
						|
                    await _bookmarkService.DeleteBookmarkFiles(bookmarksToRemove);
 | 
						|
                }
 | 
						|
                catch (Exception ex)
 | 
						|
                {
 | 
						|
                    _logger.LogError(ex, "There was an issue cleaning up old bookmarks");
 | 
						|
                }
 | 
						|
                return Ok();
 | 
						|
            }
 | 
						|
        }
 | 
						|
        catch (Exception ex)
 | 
						|
        {
 | 
						|
            _logger.LogError(ex, "There was an exception when trying to clear bookmarks");
 | 
						|
            await _unitOfWork.RollbackAsync();
 | 
						|
        }
 | 
						|
 | 
						|
        return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-clear-bookmarks"));
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Removes all bookmarks for all chapters linked to a Series
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="dto"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpPost("bulk-remove-bookmarks")]
 | 
						|
    public async Task<ActionResult> BulkRemoveBookmarks(BulkRemoveBookmarkForSeriesDto dto)
 | 
						|
    {
 | 
						|
        var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Bookmarks);
 | 
						|
        if (user == null) return Unauthorized();
 | 
						|
        if (user.Bookmarks == null) return Ok(await _localizationService.Translate(User.GetUserId(), "nothing-to-do"));
 | 
						|
 | 
						|
        try
 | 
						|
        {
 | 
						|
            foreach (var seriesId in dto.SeriesIds)
 | 
						|
            {
 | 
						|
                var bookmarksToRemove = user.Bookmarks.Where(bmk => bmk.SeriesId == seriesId).ToList();
 | 
						|
                user.Bookmarks = user.Bookmarks.Where(bmk => bmk.SeriesId != seriesId).ToList();
 | 
						|
                _unitOfWork.UserRepository.Update(user);
 | 
						|
                await _bookmarkService.DeleteBookmarkFiles(bookmarksToRemove);
 | 
						|
            }
 | 
						|
 | 
						|
 | 
						|
            if (!_unitOfWork.HasChanges() || await _unitOfWork.CommitAsync())
 | 
						|
            {
 | 
						|
                return Ok();
 | 
						|
            }
 | 
						|
        }
 | 
						|
        catch (Exception ex)
 | 
						|
        {
 | 
						|
            _logger.LogError(ex, "There was an exception when trying to clear bookmarks");
 | 
						|
            await _unitOfWork.RollbackAsync();
 | 
						|
        }
 | 
						|
 | 
						|
        return BadRequest(await _localizationService.Translate(User.GetUserId(), "generic-clear-bookmarks"));
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Returns all bookmarked pages for a given volume
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="volumeId"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpGet("volume-bookmarks")]
 | 
						|
    public async Task<ActionResult<IEnumerable<BookmarkDto>>> GetBookmarksForVolume(int volumeId)
 | 
						|
    {
 | 
						|
        return Ok(await _unitOfWork.UserRepository.GetBookmarkDtosForVolume(User.GetUserId(), volumeId));
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Returns all bookmarked pages for a given series
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="seriesId"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpGet("series-bookmarks")]
 | 
						|
    public async Task<ActionResult<IEnumerable<BookmarkDto>>> GetBookmarksForSeries(int seriesId)
 | 
						|
    {
 | 
						|
        return Ok(await _unitOfWork.UserRepository.GetBookmarkDtosForSeries(User.GetUserId(), seriesId));
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Bookmarks a page against a Chapter
 | 
						|
    /// </summary>
 | 
						|
    /// <remarks>This has a side effect of caching the chapter files to disk</remarks>
 | 
						|
    /// <param name="bookmarkDto"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpPost("bookmark")]
 | 
						|
    public async Task<ActionResult> BookmarkPage(BookmarkDto bookmarkDto)
 | 
						|
    {
 | 
						|
        // Don't let user save past total pages.
 | 
						|
        var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Bookmarks);
 | 
						|
        if (user == null) return new UnauthorizedResult();
 | 
						|
 | 
						|
        if (!await _accountService.HasBookmarkPermission(user))
 | 
						|
            return BadRequest(await _localizationService.Translate(User.GetUserId(), "bookmark-permission"));
 | 
						|
 | 
						|
        var chapter = await _cacheService.Ensure(bookmarkDto.ChapterId);
 | 
						|
        if (chapter == null) return BadRequest(await _localizationService.Translate(User.GetUserId(), "cache-file-find"));
 | 
						|
 | 
						|
        bookmarkDto.Page = _readerService.CapPageToChapter(chapter, bookmarkDto.Page);
 | 
						|
        var path = _cacheService.GetCachedPagePath(chapter.Id, bookmarkDto.Page);
 | 
						|
 | 
						|
        if (!await _bookmarkService.BookmarkPage(user, bookmarkDto, path))
 | 
						|
            return BadRequest(await _localizationService.Translate(User.GetUserId(), "bookmark-save"));
 | 
						|
 | 
						|
        BackgroundJob.Enqueue(() => _cacheService.CleanupBookmarkCache(bookmarkDto.SeriesId));
 | 
						|
        return Ok();
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Removes a bookmarked page for a Chapter
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="bookmarkDto"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpPost("unbookmark")]
 | 
						|
    public async Task<ActionResult> UnBookmarkPage(BookmarkDto bookmarkDto)
 | 
						|
    {
 | 
						|
        var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Bookmarks);
 | 
						|
        if (user == null) return new UnauthorizedResult();
 | 
						|
        if (user.Bookmarks.IsNullOrEmpty()) return Ok();
 | 
						|
 | 
						|
        if (!await _accountService.HasBookmarkPermission(user))
 | 
						|
            return BadRequest(await _localizationService.Translate(User.GetUserId(), "bookmark-permission"));
 | 
						|
 | 
						|
        if (!await _bookmarkService.RemoveBookmarkPage(user, bookmarkDto))
 | 
						|
            return BadRequest(await _localizationService.Translate(User.GetUserId(), "bookmark-save"));
 | 
						|
        BackgroundJob.Enqueue(() => _cacheService.CleanupBookmarkCache(bookmarkDto.SeriesId));
 | 
						|
        return Ok();
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Returns the next logical chapter from the series.
 | 
						|
    /// </summary>
 | 
						|
    /// <example>
 | 
						|
    /// V1 → V2 → V3 chapter 0 → V3 chapter 10 → SP 01 → SP 02
 | 
						|
    /// </example>
 | 
						|
    /// <param name="seriesId"></param>
 | 
						|
    /// <param name="volumeId"></param>
 | 
						|
    /// <param name="currentChapterId"></param>
 | 
						|
    /// <returns>chapter id for next manga</returns>
 | 
						|
    [ResponseCache(CacheProfileName = "Hour", VaryByQueryKeys = new [] { "seriesId", "volumeId", "currentChapterId"})]
 | 
						|
    [HttpGet("next-chapter")]
 | 
						|
    public async Task<ActionResult<int>> GetNextChapter(int seriesId, int volumeId, int currentChapterId)
 | 
						|
    {
 | 
						|
        return await _readerService.GetNextChapterIdAsync(seriesId, volumeId, currentChapterId, User.GetUserId());
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Returns the previous logical chapter from the series.
 | 
						|
    /// </summary>
 | 
						|
    /// <example>
 | 
						|
    /// V1 ← V2 ← V3 chapter 0 ← V3 chapter 10 ← SP 01 ← SP 02
 | 
						|
    /// </example>
 | 
						|
    /// <param name="seriesId"></param>
 | 
						|
    /// <param name="volumeId"></param>
 | 
						|
    /// <param name="currentChapterId"></param>
 | 
						|
    /// <returns>chapter id for next manga</returns>
 | 
						|
    [ResponseCache(CacheProfileName = "Hour", VaryByQueryKeys = new [] { "seriesId", "volumeId", "currentChapterId"})]
 | 
						|
    [HttpGet("prev-chapter")]
 | 
						|
    public async Task<ActionResult<int>> GetPreviousChapter(int seriesId, int volumeId, int currentChapterId)
 | 
						|
    {
 | 
						|
        return await _readerService.GetPrevChapterIdAsync(seriesId, volumeId, currentChapterId, User.GetUserId());
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// For the current user, returns an estimate on how long it would take to finish reading the series.
 | 
						|
    /// </summary>
 | 
						|
    /// <remarks>For Epubs, this does not check words inside a chapter due to overhead so may not work in all cases.</remarks>
 | 
						|
    /// <param name="seriesId"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpGet("time-left")]
 | 
						|
    [ResponseCache(CacheProfileName = "Hour", VaryByQueryKeys = new [] { "seriesId"})]
 | 
						|
    public async Task<ActionResult<HourEstimateRangeDto>> GetEstimateToCompletion(int seriesId)
 | 
						|
    {
 | 
						|
        var userId = User.GetUserId();
 | 
						|
        var series = await _unitOfWork.SeriesRepository.GetSeriesDtoByIdAsync(seriesId, userId);
 | 
						|
 | 
						|
        // Get all sum of all chapters with progress that is complete then subtract from series. Multiply by modifiers
 | 
						|
        var progress = await _unitOfWork.AppUserProgressRepository.GetUserProgressForSeriesAsync(seriesId, userId);
 | 
						|
        if (series.Format == MangaFormat.Epub)
 | 
						|
        {
 | 
						|
            var chapters =
 | 
						|
                await _unitOfWork.ChapterRepository.GetChaptersByIdsAsync(progress.Select(p => p.ChapterId).ToList());
 | 
						|
            // Word count
 | 
						|
            var progressCount = chapters.Sum(c => c.WordCount);
 | 
						|
            var wordsLeft = series.WordCount - progressCount;
 | 
						|
            return _readerService.GetTimeEstimate(wordsLeft, 0, true);
 | 
						|
        }
 | 
						|
 | 
						|
        var progressPageCount = progress.Sum(p => p.PagesRead);
 | 
						|
        var pagesLeft = series.Pages - progressPageCount;
 | 
						|
        return _readerService.GetTimeEstimate(0, pagesLeft, false);
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Returns the user's personal table of contents for the given chapter
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="chapterId"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpGet("ptoc")]
 | 
						|
    public ActionResult<IEnumerable<PersonalToCDto>> GetPersonalToC(int chapterId)
 | 
						|
    {
 | 
						|
        return Ok(_unitOfWork.UserTableOfContentRepository.GetPersonalToC(User.GetUserId(), chapterId));
 | 
						|
    }
 | 
						|
 | 
						|
    [HttpDelete("ptoc")]
 | 
						|
    public async Task<ActionResult> DeletePersonalToc([FromQuery] int chapterId, [FromQuery] int pageNum, [FromQuery] string title)
 | 
						|
    {
 | 
						|
        var userId = User.GetUserId();
 | 
						|
        if (string.IsNullOrWhiteSpace(title)) return BadRequest(await _localizationService.Translate(userId, "name-required"));
 | 
						|
        if (pageNum < 0) return BadRequest(await _localizationService.Translate(userId, "valid-number"));
 | 
						|
        var toc = await _unitOfWork.UserTableOfContentRepository.Get(userId, chapterId, pageNum, title);
 | 
						|
        if (toc == null) return Ok();
 | 
						|
        _unitOfWork.UserTableOfContentRepository.Remove(toc);
 | 
						|
        await _unitOfWork.CommitAsync();
 | 
						|
        return Ok();
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Create a new personal table of content entry for a given chapter
 | 
						|
    /// </summary>
 | 
						|
    /// <remarks>The title and page number must be unique to that book</remarks>
 | 
						|
    /// <param name="dto"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    [HttpPost("create-ptoc")]
 | 
						|
    public async Task<ActionResult> CreatePersonalToC(CreatePersonalToCDto dto)
 | 
						|
    {
 | 
						|
        // Validate there isn't already an existing page title combo?
 | 
						|
        var userId = User.GetUserId();
 | 
						|
        if (string.IsNullOrWhiteSpace(dto.Title)) return BadRequest(await _localizationService.Translate(userId, "name-required"));
 | 
						|
        if (dto.PageNumber < 0) return BadRequest(await _localizationService.Translate(userId, "valid-number"));
 | 
						|
        if (await _unitOfWork.UserTableOfContentRepository.IsUnique(userId, dto.ChapterId, dto.PageNumber,
 | 
						|
                dto.Title.Trim()))
 | 
						|
        {
 | 
						|
            return BadRequest(await _localizationService.Translate(userId, "duplicate-bookmark"));
 | 
						|
        }
 | 
						|
 | 
						|
        _unitOfWork.UserTableOfContentRepository.Attach(new AppUserTableOfContent()
 | 
						|
        {
 | 
						|
            Title = dto.Title.Trim(),
 | 
						|
            ChapterId = dto.ChapterId,
 | 
						|
            PageNumber = dto.PageNumber,
 | 
						|
            SeriesId = dto.SeriesId,
 | 
						|
            LibraryId = dto.LibraryId,
 | 
						|
            BookScrollId = dto.BookScrollId,
 | 
						|
            AppUserId = userId
 | 
						|
        });
 | 
						|
        await _unitOfWork.CommitAsync();
 | 
						|
        return Ok();
 | 
						|
    }
 | 
						|
}
 |