diff --git a/src/runners/komga/api/library.ts b/src/runners/komga/api/library.ts index 6d523c8..7a2e1e8 100644 --- a/src/runners/komga/api/library.ts +++ b/src/runners/komga/api/library.ts @@ -5,6 +5,7 @@ import { LibraryDto, PageBookDto, PageSeriesDto, + ReadListDto, SeriesDto, } from "../types"; import { getHost } from "./auth"; @@ -157,6 +158,31 @@ export const getBooksForSeriesAsChapters = async (series: string) => { return items; }; +/** + * Gets all books from a user's readlist. + */ +export const getBooksForReadList = async ( + readlist_id: string | null, + sort: string, + status?: "UNREAD" | "READ" | "IN_PROGRESS" +) => { + const { content: data } = await request({ + url: await genURL(`/api/v1/readlists`), + params: { + sort, + read_status: status, + ...(readlist_id && { readlist_id }), + }, + }); + + const host = await getHost(); + const highlights: Highlight[] = (data ?? []).map((book) => + bookToHighlight(book, host) + ); + + return highlights; +}; + export const getBook = async (id: string) => { return request({ url: await genURL(`/api/v1/books/${id}`), @@ -203,3 +229,9 @@ export const getBooks2 = async ( return data ?? []; }; + +export const getReadList = async (id: string) => { + return request({ + url: await genURL(`/api/v1/readlists/${id}`), + }); +}; diff --git a/src/runners/komga/impl/directoryHandler.ts b/src/runners/komga/impl/directoryHandler.ts index fbacc98..806dd11 100644 --- a/src/runners/komga/impl/directoryHandler.ts +++ b/src/runners/komga/impl/directoryHandler.ts @@ -16,6 +16,7 @@ import { getBooks2, getBooksForSeries, getHost, + getReadList, getSeriesForLibrary, } from "../api"; import { KomgaStore } from "../store"; @@ -58,7 +59,8 @@ async function fetchDirectory(request: DirectoryRequest): IResponse { : buildSort(); const seriesId = request.context?.seriesId; const libraryId = request.context?.libraryId; - + const readlistId = request.context?.readlistId; + if (seriesId) { const result = await getBooksForSeries(seriesId, sort, request.page); return { @@ -82,7 +84,21 @@ async function fetchDirectory(request: DirectoryRequest): IResponse { results, isLastPage: results.length < RESULT_COUNT, }; - } + } else if (readlistId) { + // If a readlistId is provided, fetch books from the readlist + const host = await getHost(); + if (!host) throw new Error("Host not defined"); + + const results = await getBooksForReadList( + readlistId, + buildSort(request.sort?.id, request.sort?.ascending), + request.query + ).map((v) => bookToHighlight(v, host)); + + return { + results, + isLastPage: results.length < RESULT_COUNT, + }; const isSeriesDirectory = request.context?.isSeriesDirectory ?? false; const host = await getHost();