fix: Resolve Chapter Download Queue and Removal Issues (Closes #1937)

This commit is contained in:
Rajarshee Chatterjee
2026-08-01 17:10:29 +05:30
parent c0877a9b06
commit c3260e8e01
15 changed files with 242 additions and 24 deletions
+19 -9
View File
@@ -245,7 +245,6 @@ export const increaseTimeSpent = async (
});
};
// TODO: Remove the need for the chapters array, as it could lead to not deleting the downloaded files but just marking them as not downloaded
/*
Deletes all downloaded chapters from the database
*/
@@ -255,19 +254,28 @@ export const deleteDownloads = async (
if (!chapters?.length) {
return;
}
chapters.forEach(chapter => {
deleteDownloadedFiles(chapter.pluginId, chapter.novelId, chapter.id);
});
await Promise.all(
chapters.map(chapter =>
deleteDownloadedFiles(chapter.pluginId, chapter.novelId, chapter.id),
),
);
const chapterIds = chapters.map(chapter => chapter.id);
await dbManager.write(async tx => {
await tx.update(chapterSchema).set({ isDownloaded: false }).run();
await tx
.update(chapterSchema)
.set({ isDownloaded: false })
.where(inArray(chapterSchema.id, chapterIds))
.run();
});
};
export const deleteReadChaptersFromDb = async (): Promise<void> => {
const chapters = await getReadDownloadedChapters();
chapters?.forEach(chapter => {
deleteDownloadedFiles(chapter.pluginId, chapter.novelId, chapter.id);
});
await Promise.all(
chapters.map(chapter =>
deleteDownloadedFiles(chapter.pluginId, chapter.novelId, chapter.id),
),
);
const chapterIds = chapters?.map(chapter => chapter.id);
if (chapterIds?.length) {
await dbManager.write(async tx => {
@@ -460,7 +468,8 @@ export const getAllUndownloadedChapters = async (
eq(chapterSchema.novelId, novelId),
eq(chapterSchema.isDownloaded, false),
),
);
)
.orderBy(asc(castInt(chapterSchema.page)), asc(chapterSchema.position));
/**
* @deprecated, use getNovelChapters with whereConditions instead
*/
@@ -477,6 +486,7 @@ export const getAllUndownloadedAndUnreadChapters = async (
eq(chapterSchema.unread, true),
),
)
.orderBy(asc(castInt(chapterSchema.page)), asc(chapterSchema.position))
.all();
export const getChapter = async (chapterId: number) =>
@@ -509,9 +509,14 @@ describe('ChapterQueries', () => {
await insertTestChapter(testDb, novelId, {
isDownloaded: true,
});
const untouchedChapterId = await insertTestChapter(testDb, novelId, {
isDownloaded: true,
});
const chapters = await getNovelChapters(novelId);
const downloadedChapters = chapters.filter(c => c.isDownloaded);
const downloadedChapters = chapters.filter(
c => c.isDownloaded && c.id !== untouchedChapterId,
);
await deleteDownloads(
downloadedChapters.map(c => ({
id: c.id,
@@ -521,7 +526,14 @@ describe('ChapterQueries', () => {
);
const updatedChapters = await getNovelChapters(novelId);
expect(updatedChapters.every(c => c.isDownloaded === false)).toBe(true);
expect(
updatedChapters
.filter(c => c.id !== untouchedChapterId)
.every(c => c.isDownloaded === false),
).toBe(true);
expect(
updatedChapters.find(c => c.id === untouchedChapterId)?.isDownloaded,
).toBe(true);
});
});
@@ -729,6 +741,18 @@ describe('ChapterQueries', () => {
expect(result).toHaveLength(2);
expect(result.every(c => c.isDownloaded === false)).toBe(true);
});
it('returns chapters in reading order', async () => {
const testDb = getTestDb();
const novelId = await insertTestNovel(testDb, { inLibrary: true });
await insertTestChapter(testDb, novelId, { position: 3 });
await insertTestChapter(testDb, novelId, { position: 1 });
await insertTestChapter(testDb, novelId, { position: 2 });
const result = await getAllUndownloadedChapters(novelId);
expect(result.map(chapter => chapter.position)).toEqual([1, 2, 3]);
});
});
describe('getAllUndownloadedAndUnreadChapters', () => {
@@ -754,6 +778,17 @@ describe('ChapterQueries', () => {
expect(result[0].isDownloaded).toBe(false);
expect(result[0].unread).toBe(true);
});
it('returns unread chapters in reading order', async () => {
const testDb = getTestDb();
const novelId = await insertTestNovel(testDb, { inLibrary: true });
await insertTestChapter(testDb, novelId, { position: 5, unread: true });
await insertTestChapter(testDb, novelId, { position: 2, unread: true });
const result = await getAllUndownloadedAndUnreadChapters(novelId);
expect(result.map(chapter => chapter.position)).toEqual([2, 5]);
});
});
describe('getChapter', () => {