feat(opds): add pagination to authors / series navigation (#2315)

This commit is contained in:
James Ward
2026-09-11 19:07:49 -04:00
committed by GitHub
parent 4c160cd2f3
commit 7d768a2879
3 changed files with 81 additions and 46 deletions
@@ -175,23 +175,27 @@ public interface BookOpdsRepository extends JpaRepository<BookEntity, Long>, Jpa
// ============================================
@Query("""
SELECT DISTINCT a FROM AuthorEntity a
SELECT DISTINCT a.name
FROM AuthorEntity a
JOIN a.bookMetadataEntityList m
JOIN m.book b
WHERE (b.deleted IS NULL OR b.deleted = false)
AND a.name IS NOT NULL
ORDER BY a.name
""")
List<AuthorEntity> findDistinctAuthors();
Page<String> findDistinctAuthorNames(Pageable pageable);
@Query("""
SELECT DISTINCT a FROM AuthorEntity a
SELECT DISTINCT a.name
FROM AuthorEntity a
JOIN a.bookMetadataEntityList m
JOIN m.book b
WHERE (b.deleted IS NULL OR b.deleted = false)
AND b.library.id IN :libraryIds
AND a.name IS NOT NULL
ORDER BY a.name
""")
List<AuthorEntity> findDistinctAuthorsByLibraryIds(@Param("libraryIds") Collection<Long> libraryIds);
Page<String> findDistinctAuthorNamesByLibraryIds(@Param("libraryIds") Collection<Long> libraryIds, Pageable pageable);
// ============================================
// BOOKS BY AUTHOR - Two Query Pattern
@@ -230,7 +234,7 @@ public interface BookOpdsRepository extends JpaRepository<BookEntity, Long>, Jpa
AND m.seriesName != ''
ORDER BY m.seriesName
""")
List<String> findDistinctSeries();
Page<String> findDistinctSeries(Pageable pageable);
@Query("""
SELECT DISTINCT m.seriesName FROM BookMetadataEntity m
@@ -241,7 +245,7 @@ public interface BookOpdsRepository extends JpaRepository<BookEntity, Long>, Jpa
AND m.seriesName != ''
ORDER BY m.seriesName
""")
List<String> findDistinctSeriesByLibraryIds(@Param("libraryIds") Collection<Long> libraryIds);
Page<String> findDistinctSeriesByLibraryIds(@Param("libraryIds") Collection<Long> libraryIds, Pageable pageable);
// ============================================
// BOOKS BY SERIES - Two Query Pattern (sorted by series number)
@@ -176,32 +176,25 @@ public class OpdsBookService {
return books.stream().map(bookMapper::toBook).toList();
}
public List<String> getDistinctAuthors(Long userId) {
public Page<String> getDistinctAuthors(Long userId, int page, int size) {
if (userId == null) {
return List.of();
return Page.empty();
}
Pageable pageable = PageRequest.of(Math.max(page, 0), size);
BookLoreUserEntity entity = userRepository.findByIdWithDetails(userId)
.orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(userId));
BookLoreUser user = bookLoreUserTransformer.toDTO(entity);
List<AuthorEntity> authors;
if (user.getPermissions().isAdmin()) {
authors = bookOpdsRepository.findDistinctAuthors();
return bookOpdsRepository.findDistinctAuthorNames(pageable);
} else {
Set<Long> libraryIds = user.getAssignedLibraries().stream()
.map(Library::getId)
.collect(Collectors.toSet());
authors = bookOpdsRepository.findDistinctAuthorsByLibraryIds(libraryIds);
return bookOpdsRepository.findDistinctAuthorNamesByLibraryIds(libraryIds, pageable);
}
return authors.stream()
.map(AuthorEntity::getName)
.filter(Objects::nonNull)
.distinct()
.sorted()
.toList();
}
public Page<Book> getBooksByAuthorName(Long userId, String authorName, int page, int size) {
@@ -238,24 +231,26 @@ public class OpdsBookService {
return applyBookFilters(booksPage, userId);
}
public List<String> getDistinctSeries(Long userId) {
public Page<String> getDistinctSeries(Long userId, int page, int size) {
if (userId == null) {
return List.of();
return Page.empty();
}
Pageable pageable = PageRequest.of(Math.max(page, 0), size);
BookLoreUserEntity entity = userRepository.findByIdWithDetails(userId)
.orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(userId));
BookLoreUser user = bookLoreUserTransformer.toDTO(entity);
if (user.getPermissions().isAdmin()) {
return bookOpdsRepository.findDistinctSeries();
return bookOpdsRepository.findDistinctSeries(pageable);
}
Set<Long> libraryIds = user.getAssignedLibraries().stream()
.map(Library::getId)
.collect(Collectors.toSet());
return bookOpdsRepository.findDistinctSeriesByLibraryIds(libraryIds);
return bookOpdsRepository.findDistinctSeriesByLibraryIds(libraryIds, pageable);
}
public Page<Book> getBooksBySeriesName(Long userId, String seriesName, int page, int size) {
@@ -249,20 +249,27 @@ public class OpdsFeedService {
public String generateAuthorsNavigation(HttpServletRequest request) {
Long userId = getUserId();
List<String> authors = opdsBookService.getDistinctAuthors(userId);
int page = Math.max(1, parseLongParam(request, "page", 1L).intValue());
int size = Math.min(parseLongParam(request, "size", (long) DEFAULT_PAGE_SIZE).intValue(), MAX_PAGE_SIZE);
Page<String> authorsPage = opdsBookService.getDistinctAuthors(userId, page - 1, size);
String selfUrl = buildCurrentUrl(request, page, size);
var feed = new StringBuilder("""
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:opds="http://opds-spec.org/2010/catalog">
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:opds="http://opds-spec.org/2010/catalog" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
<id>urn:booklore:navigation:authors</id>
<title>Authors</title>
<updated>%s</updated>
<link rel="self" href="/api/v1/opds/authors" type="application/atom+xml;profile=opds-catalog;kind=navigation"/>
<link rel="self" href="%s" type="application/atom+xml;profile=opds-catalog;kind=navigation"/>
<link rel="start" href="/api/v1/opds" type="application/atom+xml;profile=opds-catalog;kind=navigation"/>
<link rel="search" type="application/opensearchdescription+xml" title="Search" href="/api/v1/opds/search.opds"/>
""".formatted(now()));
""".formatted(now(), escapeXml(selfUrl)));
for (String author : authors) {
appendPaginationMeta(feed, authorsPage.getTotalElements(), page, size);
for (String author : authorsPage) {
feed.append("""
<entry>
<title>%s</title>
@@ -280,26 +287,35 @@ public class OpdsFeedService {
));
}
appendPaginationLinks(feed, request, "navigation", page, authorsPage.getTotalPages(), size);
feed.append("</feed>");
return feed.toString();
}
public String generateSeriesNavigation(HttpServletRequest request) {
Long userId = getUserId();
List<String> seriesList = opdsBookService.getDistinctSeries(userId);
int page = Math.max(1, parseLongParam(request, "page", 1L).intValue());
int size = Math.min(parseLongParam(request, "size", (long) DEFAULT_PAGE_SIZE).intValue(), MAX_PAGE_SIZE);
Page<String> seriesPage = opdsBookService.getDistinctSeries(userId, page - 1, size);
String selfUrl = buildCurrentUrl(request, page, size);
var feed = new StringBuilder("""
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:opds="http://opds-spec.org/2010/catalog">
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:opds="http://opds-spec.org/2010/catalog" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
<id>urn:booklore:navigation:series</id>
<title>Series</title>
<updated>%s</updated>
<link rel="self" href="/api/v1/opds/series" type="application/atom+xml;profile=opds-catalog;kind=navigation"/>
<link rel="self" href="%s" type="application/atom+xml;profile=opds-catalog;kind=navigation"/>
<link rel="start" href="/api/v1/opds" type="application/atom+xml;profile=opds-catalog;kind=navigation"/>
<link rel="search" type="application/opensearchdescription+xml" title="Search" href="/api/v1/opds/search.opds"/>
""".formatted(now()));
""".formatted(now(), escapeXml(selfUrl)));
for (String series : seriesList) {
appendPaginationMeta(feed, seriesPage.getTotalElements(), page, size);
for (String series : seriesPage) {
feed.append("""
<entry>
<title>%s</title>
@@ -317,6 +333,8 @@ public class OpdsFeedService {
));
}
appendPaginationLinks(feed, request, "navigation", page, seriesPage.getTotalPages(), size);
feed.append("</feed>");
return feed.toString();
}
@@ -357,9 +375,6 @@ public class OpdsFeedService {
<id>%s</id>
<title>%s</title>
<updated>%s</updated>
<opensearch:totalResults>%d</opensearch:totalResults>
<opensearch:startIndex>%d</opensearch:startIndex>
<opensearch:itemsPerPage>%d</opensearch:itemsPerPage>
<link rel="self" href="%s" type="application/atom+xml;profile=opds-catalog;kind=acquisition"/>
<link rel="start" href="/api/v1/opds" type="application/atom+xml;profile=opds-catalog;kind=navigation"/>
<link rel="search" type="application/opensearchdescription+xml" title="Search" href="/api/v1/opds/search.opds"/>
@@ -367,13 +382,12 @@ public class OpdsFeedService {
escapeXml(feedId),
escapeXml(feedTitle),
now(),
booksPage.getTotalElements(),
((page - 1) * size) + 1,
size,
escapeXml(buildCurrentUrl(request, page, size))
));
appendPaginationLinks(feed, request, page, booksPage.getTotalPages(), size);
appendPaginationMeta(feed, booksPage.getTotalElements(), page, size);
appendPaginationLinks(feed, request, "acquisition", page, booksPage.getTotalPages(), size);
booksPage.getContent().forEach(book -> appendBookEntry(feed, book));
@@ -406,7 +420,7 @@ public class OpdsFeedService {
<link rel="search" type="application/opensearchdescription+xml" title="Search" href="/api/v1/opds/search.opds"/>
""".formatted(now(), booksPage.getTotalElements(), ((page - 1) * size) + 1, size, escapeXml(buildCurrentUrl(request, page, size))));
appendPaginationLinks(feed, request, page, booksPage.getTotalPages(), size);
appendPaginationLinks(feed, request, "acquisition", page, booksPage.getTotalPages(), size);
booksPage.getContent().forEach(book -> appendBookEntry(feed, book));
@@ -451,26 +465,48 @@ public class OpdsFeedService {
""";
}
private void appendPaginationLinks(StringBuilder feed, HttpServletRequest request, int currentPage, int totalPages, int size) {
private void appendPaginationMeta(StringBuilder feed, long totalElements, int page, int size) {
feed.append("""
<opensearch:totalResults>%d</opensearch:totalResults>
<opensearch:startIndex>%d</opensearch:startIndex>
<opensearch:itemsPerPage>%d</opensearch:itemsPerPage>
""".formatted(
totalElements,
((page - 1) * size) + 1,
size
));
}
private void appendPaginationLinks(StringBuilder feed, HttpServletRequest request, String kind, int currentPage, int totalPages, int size) {
String mediaType = "application/atom+xml;profile=opds-catalog;kind=" + kind;
if (totalPages > 0) {
feed.append(" <link rel=\"first\" href=\"")
.append(escapeXml(buildPaginationUrl(request, 1, size)))
.append("\" type=\"application/atom+xml;profile=opds-catalog;kind=acquisition\"/>\n");
.append("\" type=\"")
.append(escapeXml(mediaType))
.append("\"/>\n");
}
if (currentPage > 1) {
feed.append(" <link rel=\"previous\" href=\"")
.append(escapeXml(buildPaginationUrl(request, currentPage - 1, size)))
.append("\" type=\"application/atom+xml;profile=opds-catalog;kind=acquisition\"/>\n");
.append("\" type=\"")
.append(escapeXml(mediaType))
.append("\"/>\n");
}
if (currentPage < totalPages) {
feed.append(" <link rel=\"next\" href=\"")
.append(escapeXml(buildPaginationUrl(request, currentPage + 1, size)))
.append("\" type=\"application/atom+xml;profile=opds-catalog;kind=acquisition\"/>\n");
.append("\" type=\"")
.append(escapeXml(mediaType))
.append("\"/>\n");
}
if (totalPages > 0) {
feed.append(" <link rel=\"last\" href=\"")
.append(escapeXml(buildPaginationUrl(request, totalPages, size)))
.append("\" type=\"application/atom+xml;profile=opds-catalog;kind=acquisition\"/>\n");
.append("\" type=\"")
.append(escapeXml(mediaType))
.append("\"/>\n");
}
}