From 23e639a4e99819d796cf41d1b1017d4d80cab843 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Tue, 23 Apr 2024 13:46:59 +0200 Subject: [PATCH] Ignore duplicated chapters for download limits In case the next 5 chapters should get downloaded and they are all the same chapter from different scanlators, then the download actually only included 1 chapter. To fix this duplicated chapters are counted as 1 chapter --- src/lib/data/Chapters.ts | 12 ++++++++++++ src/lib/data/Mangas.ts | 18 +++++++++++++++++- src/lib/graphql/generated/graphql.ts | 2 +- src/lib/graphql/queries/ChapterQuery.ts | 2 ++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/lib/data/Chapters.ts b/src/lib/data/Chapters.ts index c9297b22..f6ab754d 100644 --- a/src/lib/data/Chapters.ts +++ b/src/lib/data/Chapters.ts @@ -305,6 +305,18 @@ export class Chapters { } } + /** + * Returns the provided "uniqueChapters" plus their duplicates found in "allChapters" + */ + static addDuplicates( + uniqueChapters: T[], + allChapters: T[], + ): T[] { + const chapterNumberToChapters = Object.groupBy(allChapters, ({ chapterNumber }) => chapterNumber); + + return uniqueChapters.map(({ chapterNumber }) => chapterNumberToChapters[chapterNumber]!).flat(); + } + static removeDuplicates(currentChapter: T, chapters: T[]): T[] { const chapterNumberToChapters = Object.groupBy(chapters, ({ chapterNumber }) => chapterNumber); diff --git a/src/lib/data/Mangas.ts b/src/lib/data/Mangas.ts index 114b5e60..78fc1753 100644 --- a/src/lib/data/Mangas.ts +++ b/src/lib/data/Mangas.ts @@ -280,7 +280,23 @@ export class Mangas { ) satisfies MangaIdToDownloadSize[]; const chapterIdsToDownload = mangaIdToActualDownloadSize - .map(([mangaId, actualSize]) => mangaIdToChaptersToConsider[Number(mangaId)]!.slice(0, actualSize)) + .map(([mangaId, actualSize]) => { + const mangaChapters = mangaIdToChaptersToConsider[Number(mangaId)]!; + + if (!mangaChapters.length) { + return []; + } + + const shouldDownloadAll = actualSize === undefined; + if (shouldDownloadAll) { + return mangaChapters; + } + + const uniqueMangaChapters = Chapters.removeDuplicates(mangaChapters[0], mangaChapters); + const uniqueMangaChaptersToDownload = uniqueMangaChapters.slice(0, actualSize); + + return Chapters.addDuplicates(uniqueMangaChaptersToDownload, mangaChapters); + }) .flat(); if (!chapterIdsToDownload.length) { diff --git a/src/lib/graphql/generated/graphql.ts b/src/lib/graphql/generated/graphql.ts index 2c04e41d..063d5175 100644 --- a/src/lib/graphql/generated/graphql.ts +++ b/src/lib/graphql/generated/graphql.ts @@ -3120,7 +3120,7 @@ export type GetMangasChapterIdsWithStateQueryVariables = Exact<{ }>; -export type GetMangasChapterIdsWithStateQuery = { __typename?: 'Query', chapters: { __typename?: 'ChapterNodeList', nodes: Array<{ __typename?: 'ChapterType', id: number, isDownloaded: boolean, isRead: boolean, isBookmarked: boolean, mangaId: number }> } }; +export type GetMangasChapterIdsWithStateQuery = { __typename?: 'Query', chapters: { __typename?: 'ChapterNodeList', nodes: Array<{ __typename?: 'ChapterType', id: number, isDownloaded: boolean, isRead: boolean, isBookmarked: boolean, mangaId: number, scanlator?: string | null, chapterNumber: number }> } }; export type GetDownloadStatusQueryVariables = Exact<{ [key: string]: never; }>; diff --git a/src/lib/graphql/queries/ChapterQuery.ts b/src/lib/graphql/queries/ChapterQuery.ts index c73898d8..01f242cc 100644 --- a/src/lib/graphql/queries/ChapterQuery.ts +++ b/src/lib/graphql/queries/ChapterQuery.ts @@ -73,6 +73,8 @@ export const GET_MANGAS_CHAPTER_IDS_WITH_STATE = gql` isRead isBookmarked mangaId + scanlator + chapterNumber } } }