mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-06-03 05:34:21 -04:00
* Refactored ResponseCache profiles into consts * Refactored code to use an extension method for getting user library ids. * Started server statistics, added a charting library, and added a table sort column (not finished) * Refactored code and have a fully working example of sortable headers. Still doesn't work with default sorting state, will work on that later. * Implemented file size, but it's too expensive, so commented out. * Added a migration to provide extension and length/size information in the DB to allow for faster stat apis. * Added the ability to force a library scan from library settings. * Refactored some apis to provide more of a file breakdown rather than just file size. * Working on visualization of file breakdown * Fixed the file breakdown visual * Fixed up 2 visualizations * Added back an api for member names, started work on top reads * Hooked up the other library types and username/days. * Preparing to remove top reads and refactor into Top users * Added LibraryId to AppUserProgress to help with complex lookups. * Added the new libraryId hook into some stats methods * Updated api methods to use libraryId for progress * More places where LibraryId is needed * Added some high level server stats * Got a ton done on server stats * Updated default theme (dark) to be the default root variables. This will allow user themes to override just what they want, rather than maintain their own css variables. * Implemented a monster query for top users by reading time. It's very slow and can be cleaned up likely. * Hooked up top reads. Code needs a big refactor. Handing off for Robbie treatment and I'll switch to User stats. * Implemented last 5 recently read series (broken) and added some basic css * Fixed recently read query * Cleanup the css a bit, Robbie we need you * More css love * Cleaned up DTOs that aren't needed anymore * Fixed top readers query * When calculating top readers, don't include read events where nothing is read (0 pages) * Hooked up the date into GetTopUsers * Hooked top readers up with days and refactored and cleaned up componets not used * Fixed up query * Started on a day by day breakdown, but going to take a break from stats. * Added a temp task to run some migration manually for stats to work * Ensure OPDS-PS uses new libraryId for progress reporting * Fixed a code smell * Adding some styling * adding more styles * Removed some debug stuff from user stats * Bump qs from 6.5.2 to 6.5.3 in /UI/Web Bumps [qs](https://github.com/ljharb/qs) from 6.5.2 to 6.5.3. - [Release notes](https://github.com/ljharb/qs/releases) - [Changelog](https://github.com/ljharb/qs/blob/main/CHANGELOG.md) - [Commits](https://github.com/ljharb/qs/compare/v6.5.2...v6.5.3) --- updated-dependencies: - dependency-name: qs dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * Tweaked some code for bad data cases * Refactored a chapter lookup to remove un-needed Volume join in 5 places across the code. * API push Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Robbie Davis <robbie@therobbiedavis.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
215 lines
8.2 KiB
C#
215 lines
8.2 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using API.Comparators;
|
|
using API.Data;
|
|
using API.Data.Repositories;
|
|
using API.DTOs.ReadingLists;
|
|
using API.Entities;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace API.Services;
|
|
|
|
public interface IReadingListService
|
|
{
|
|
Task<bool> RemoveFullyReadItems(int readingListId, AppUser user);
|
|
Task<bool> UpdateReadingListItemPosition(UpdateReadingListPosition dto);
|
|
Task<bool> DeleteReadingListItem(UpdateReadingListPosition dto);
|
|
Task<AppUser?> UserHasReadingListAccess(int readingListId, string username);
|
|
Task<bool> DeleteReadingList(int readingListId, AppUser user);
|
|
Task CalculateReadingListAgeRating(ReadingList readingList);
|
|
Task<bool> AddChaptersToReadingList(int seriesId, IList<int> chapterIds,
|
|
ReadingList readingList);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Methods responsible for management of Reading Lists
|
|
/// </summary>
|
|
/// <remarks>If called from API layer, expected for <see cref="UserHasReadingListAccess"/> to be called beforehand</remarks>
|
|
public class ReadingListService : IReadingListService
|
|
{
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
private readonly ILogger<ReadingListService> _logger;
|
|
private readonly ChapterSortComparerZeroFirst _chapterSortComparerForInChapterSorting = new ChapterSortComparerZeroFirst();
|
|
|
|
public ReadingListService(IUnitOfWork unitOfWork, ILogger<ReadingListService> logger)
|
|
{
|
|
_unitOfWork = unitOfWork;
|
|
_logger = logger;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Removes all entries that are fully read from the reading list. This commits
|
|
/// </summary>
|
|
/// <remarks>If called from API layer, expected for <see cref="UserHasReadingListAccess"/> to be called beforehand</remarks>
|
|
/// <param name="readingListId">Reading List Id</param>
|
|
/// <param name="user">User</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> RemoveFullyReadItems(int readingListId, AppUser user)
|
|
{
|
|
var items = await _unitOfWork.ReadingListRepository.GetReadingListItemDtosByIdAsync(readingListId, user.Id);
|
|
items = await _unitOfWork.ReadingListRepository.AddReadingProgressModifiers(user.Id, items.ToList());
|
|
|
|
// Collect all Ids to remove
|
|
var itemIdsToRemove = items.Where(item => item.PagesRead == item.PagesTotal).Select(item => item.Id);
|
|
|
|
try
|
|
{
|
|
var listItems =
|
|
(await _unitOfWork.ReadingListRepository.GetReadingListItemsByIdAsync(readingListId)).Where(r =>
|
|
itemIdsToRemove.Contains(r.Id));
|
|
_unitOfWork.ReadingListRepository.BulkRemove(listItems);
|
|
|
|
var readingList = await _unitOfWork.ReadingListRepository.GetReadingListByIdAsync(readingListId);
|
|
await CalculateReadingListAgeRating(readingList);
|
|
|
|
if (!_unitOfWork.HasChanges()) return true;
|
|
|
|
return await _unitOfWork.CommitAsync();
|
|
}
|
|
catch
|
|
{
|
|
await _unitOfWork.RollbackAsync();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates a reading list item from one position to another. This will cause items at that position to be pushed one index.
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> UpdateReadingListItemPosition(UpdateReadingListPosition dto)
|
|
{
|
|
var items = (await _unitOfWork.ReadingListRepository.GetReadingListItemsByIdAsync(dto.ReadingListId)).ToList();
|
|
var item = items.Find(r => r.Id == dto.ReadingListItemId);
|
|
items.Remove(item);
|
|
items.Insert(dto.ToPosition, item);
|
|
|
|
for (var i = 0; i < items.Count; i++)
|
|
{
|
|
items[i].Order = i;
|
|
}
|
|
|
|
if (!_unitOfWork.HasChanges()) return true;
|
|
|
|
return await _unitOfWork.CommitAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes a certain reading list item from a reading list
|
|
/// </summary>
|
|
/// <param name="dto">Only ReadingListId and ReadingListItemId are used</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DeleteReadingListItem(UpdateReadingListPosition dto)
|
|
{
|
|
var readingList = await _unitOfWork.ReadingListRepository.GetReadingListByIdAsync(dto.ReadingListId);
|
|
readingList.Items = readingList.Items.Where(r => r.Id != dto.ReadingListItemId).ToList();
|
|
|
|
var index = 0;
|
|
foreach (var readingListItem in readingList.Items)
|
|
{
|
|
readingListItem.Order = index;
|
|
index++;
|
|
}
|
|
|
|
await CalculateReadingListAgeRating(readingList);
|
|
|
|
if (!_unitOfWork.HasChanges()) return true;
|
|
|
|
return await _unitOfWork.CommitAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calculates the highest Age Rating from each Reading List Item
|
|
/// </summary>
|
|
/// <param name="readingList"></param>
|
|
public async Task CalculateReadingListAgeRating(ReadingList readingList)
|
|
{
|
|
await CalculateReadingListAgeRating(readingList, readingList.Items.Select(i => i.SeriesId));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calculates the highest Age Rating from each Reading List Item
|
|
/// </summary>
|
|
/// <remarks>This method is used when the ReadingList doesn't have items yet</remarks>
|
|
/// <param name="readingList"></param>
|
|
/// <param name="seriesIds">The series ids of all the reading list items</param>
|
|
private async Task CalculateReadingListAgeRating(ReadingList readingList, IEnumerable<int> seriesIds)
|
|
{
|
|
var ageRating = await _unitOfWork.SeriesRepository.GetMaxAgeRatingFromSeriesAsync(seriesIds);
|
|
readingList.AgeRating = ageRating;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates the user has access to the reading list to perform actions on it
|
|
/// </summary>
|
|
/// <param name="readingListId"></param>
|
|
/// <param name="username"></param>
|
|
/// <returns></returns>
|
|
public async Task<AppUser?> UserHasReadingListAccess(int readingListId, string username)
|
|
{
|
|
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(username,
|
|
AppUserIncludes.ReadingListsWithItems);
|
|
if (user.ReadingLists.SingleOrDefault(rl => rl.Id == readingListId) == null && !await _unitOfWork.UserRepository.IsUserAdminAsync(user))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return user;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes the Reading List from kavita
|
|
/// </summary>
|
|
/// <param name="readingListId"></param>
|
|
/// <param name="user">User should have ReadingLists populated</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DeleteReadingList(int readingListId, AppUser user)
|
|
{
|
|
var readingList = await _unitOfWork.ReadingListRepository.GetReadingListByIdAsync(readingListId);
|
|
user.ReadingLists.Remove(readingList);
|
|
|
|
if (!_unitOfWork.HasChanges()) return true;
|
|
|
|
return await _unitOfWork.CommitAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a list of Chapters as reading list items to the passed reading list.
|
|
/// </summary>
|
|
/// <param name="seriesId"></param>
|
|
/// <param name="chapterIds"></param>
|
|
/// <param name="readingList"></param>
|
|
/// <returns>True if new chapters were added</returns>
|
|
public async Task<bool> AddChaptersToReadingList(int seriesId, IList<int> chapterIds, ReadingList readingList)
|
|
{
|
|
readingList.Items ??= new List<ReadingListItem>();
|
|
var lastOrder = 0;
|
|
if (readingList.Items.Any())
|
|
{
|
|
lastOrder = readingList.Items.DefaultIfEmpty().Max(rli => rli.Order);
|
|
}
|
|
|
|
var existingChapterExists = readingList.Items.Select(rli => rli.ChapterId).ToHashSet();
|
|
var chaptersForSeries = (await _unitOfWork.ChapterRepository.GetChaptersByIdsAsync(chapterIds, ChapterIncludes.Volumes))
|
|
.OrderBy(c => Tasks.Scanner.Parser.Parser.MinNumberFromRange(c.Volume.Name))
|
|
.ThenBy(x => double.Parse(x.Number), _chapterSortComparerForInChapterSorting)
|
|
.ToList();
|
|
|
|
var index = lastOrder + 1;
|
|
foreach (var chapter in chaptersForSeries.Where(chapter => !existingChapterExists.Contains(chapter.Id)))
|
|
{
|
|
readingList.Items.Add(DbFactory.ReadingListItem(index, seriesId, chapter.VolumeId, chapter.Id));
|
|
index += 1;
|
|
}
|
|
|
|
await CalculateReadingListAgeRating(readingList, new []{ seriesId });
|
|
|
|
return index > lastOrder + 1;
|
|
}
|
|
}
|