Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/runners/komga/api/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
LibraryDto,
PageBookDto,
PageSeriesDto,
ReadListDto,
SeriesDto,
} from "../types";
import { getHost } from "./auth";
Expand Down Expand Up @@ -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<PageBookDto>({
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<BookDto>({
url: await genURL(`/api/v1/books/${id}`),
Expand Down Expand Up @@ -203,3 +229,9 @@ export const getBooks2 = async (

return data ?? [];
};

export const getReadList = async (id: string) => {
return request<ReadListDto>({
url: await genURL(`/api/v1/readlists/${id}`),
});
};
20 changes: 18 additions & 2 deletions src/runners/komga/impl/directoryHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
getBooks2,
getBooksForSeries,
getHost,
getReadList,
getSeriesForLibrary,
} from "../api";
import { KomgaStore } from "../store";
Expand Down Expand Up @@ -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 {
Expand All @@ -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();
Expand Down