feat(shelf): add shelf filtering options and integrate into book processing (#2268)

* feat(shelf): add shelf filtering options and integrate into book processing

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* fix(book-rule-evaluator): normalize array values to lowercase for rule comparisons

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* feat(filter): enhance library and shelf filtering logic for 'or' and 'and' modes

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* feat(shelf): implement support for multiple shelf IDs in book retrieval and filtering

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

---------

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
Balázs Szücs
2026-01-15 19:58:30 -07:00
committed by GitHub
parent 869bea9ca5
commit 775887b55f
13 changed files with 545 additions and 28 deletions
@@ -29,6 +29,7 @@ import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
class OpdsBookServiceTest {
@@ -147,15 +148,19 @@ class OpdsBookServiceTest {
when(bookOpdsRepository.findRecentBookIds(any())).thenReturn(Page.empty());
when(bookOpdsRepository.findBookIdsByLibraryIds(anySet(), any())).thenReturn(Page.empty());
when(bookOpdsRepository.findBookIdsByShelfId(anyLong(), any())).thenReturn(Page.empty());
when(bookOpdsRepository.findBookIdsByShelfIds(anySet(), any())).thenReturn(Page.empty());
when(bookOpdsRepository.findBookIdsByMetadataSearch(anyString(), any())).thenReturn(Page.empty());
when(bookOpdsRepository.findBookIdsByMetadataSearchAndLibraryIds(anyString(), anySet(), any())).thenReturn(Page.empty());
when(bookOpdsRepository.findBookIdsByMetadataSearchAndShelfIds(anyString(), anySet(), any())).thenReturn(Page.empty());
when(bookOpdsRepository.findAllWithMetadataByIds(anyList())).thenReturn(List.of());
when(bookOpdsRepository.findAllWithMetadataByIdsAndLibraryIds(anyList(), anySet())).thenReturn(List.of());
when(bookOpdsRepository.findAllWithMetadataByIdsAndShelfId(anyList(), anyLong())).thenReturn(List.of());
when(bookOpdsRepository.findAllWithMetadataByIdsAndShelfIds(anyList(), anySet())).thenReturn(List.of());
when(bookOpdsRepository.findAllWithFullMetadataByIds(anyList())).thenReturn(List.of());
when(bookOpdsRepository.findAllWithFullMetadataByIdsAndLibraryIds(anyList(), anySet())).thenReturn(List.of());
when(bookOpdsRepository.findAllWithFullMetadataByIdsAndShelfIds(anyList(), anySet())).thenReturn(List.of());
opdsBookService.getBooksPage(details.getOpdsUserV2().getUserId(), "q", 1L, 2L, 0, 10);
opdsBookService.getBooksPage(details.getOpdsUserV2().getUserId(), "q", 1L, Set.of(2L), 0, 10);
}
@Test
@@ -165,12 +170,16 @@ class OpdsBookServiceTest {
when(bookOpdsRepository.findRecentBookIds(any())).thenReturn(Page.empty());
when(bookOpdsRepository.findBookIdsByLibraryIds(anySet(), any())).thenReturn(Page.empty());
when(bookOpdsRepository.findBookIdsByShelfId(anyLong(), any())).thenReturn(Page.empty());
when(bookOpdsRepository.findBookIdsByShelfIds(anySet(), any())).thenReturn(Page.empty());
when(bookOpdsRepository.findBookIdsByMetadataSearch(anyString(), any())).thenReturn(Page.empty());
when(bookOpdsRepository.findBookIdsByMetadataSearchAndShelfIds(anyString(), anySet(), any())).thenReturn(Page.empty());
when(bookOpdsRepository.findAllWithMetadataByIds(anyList())).thenReturn(List.of());
when(bookOpdsRepository.findAllWithMetadataByIdsAndLibraryIds(anyList(), anySet())).thenReturn(List.of());
when(bookOpdsRepository.findAllWithMetadataByIdsAndShelfId(anyList(), anyLong())).thenReturn(List.of());
when(bookOpdsRepository.findAllWithMetadataByIdsAndShelfIds(anyList(), anySet())).thenReturn(List.of());
when(bookOpdsRepository.findAllWithFullMetadataByIds(anyList())).thenReturn(List.of());
when(bookOpdsRepository.findAllWithFullMetadataByIdsAndLibraryIds(anyList(), anySet())).thenReturn(List.of());
when(bookOpdsRepository.findAllWithFullMetadataByIdsAndShelfIds(anyList(), anySet())).thenReturn(List.of());
BookLoreUserEntity entity = mock(BookLoreUserEntity.class);
var permissionsEntity = mock(com.adityachandel.booklore.model.entity.UserPermissionsEntity.class);
@@ -191,7 +200,7 @@ class OpdsBookServiceTest {
when(shelf.getUser()).thenReturn(shelfUser);
when(shelfRepository.findById(anyLong())).thenReturn(Optional.of(shelf));
opdsBookService.getBooksPage(details.getOpdsUserV2().getUserId(), "q", 1L, 2L, 0, 10);
opdsBookService.getBooksPage(details.getOpdsUserV2().getUserId(), "q", 1L, Set.of(2L), 0, 10);
}
@Test
@@ -331,4 +340,191 @@ class OpdsBookServiceTest {
).hasMessageContaining("You are not allowed to access this resource");
}
@Test
void getBooksPage_withSingleShelfId_returnsShelfBooks() {
OpdsUserDetails details = v2UserDetails(1L, false, Set.of(1L));
BookLoreUserEntity entity = mock(BookLoreUserEntity.class);
var permissionsEntity = mock(com.adityachandel.booklore.model.entity.UserPermissionsEntity.class);
when(permissionsEntity.isPermissionAccessOpds()).thenReturn(true);
when(permissionsEntity.isPermissionAdmin()).thenReturn(false);
when(entity.getPermissions()).thenReturn(permissionsEntity);
when(userRepository.findById(1L)).thenReturn(Optional.of(entity));
BookLoreUser user = mock(BookLoreUser.class);
BookLoreUser.UserPermissions perms = mock(BookLoreUser.UserPermissions.class);
when(bookLoreUserTransformer.toDTO(entity)).thenReturn(user);
when(user.getPermissions()).thenReturn(perms);
when(perms.isAdmin()).thenReturn(false);
when(perms.isCanAccessOpds()).thenReturn(true);
when(user.getId()).thenReturn(1L);
when(user.getAssignedLibraries()).thenReturn(List.of(Library.builder().id(1L).watch(false).build()));
ShelfEntity shelf = mock(ShelfEntity.class);
BookLoreUserEntity shelfUser = mock(BookLoreUserEntity.class);
when(shelfUser.getId()).thenReturn(1L);
when(shelf.getUser()).thenReturn(shelfUser);
when(shelfRepository.findById(10L)).thenReturn(Optional.of(shelf));
BookEntity bookEntity = mock(BookEntity.class);
when(bookEntity.getId()).thenReturn(1L);
Book book = Book.builder().id(1L).build();
when(bookMapper.toBook(bookEntity)).thenReturn(book);
when(bookOpdsRepository.findBookIdsByShelfIds(eq(Set.of(10L)), any())).thenReturn(new PageImpl<>(List.of(1L)));
when(bookOpdsRepository.findAllWithMetadataByIdsAndShelfIds(eq(List.of(1L)), eq(Set.of(10L)))).thenReturn(List.of(bookEntity));
Page<Book> result = opdsBookService.getBooksPage(1L, null, null, Set.of(10L), 0, 10);
assertThat(result.getContent()).hasSize(1);
verify(bookOpdsRepository).findBookIdsByShelfIds(eq(Set.of(10L)), any());
}
@Test
void getBooksPage_withMultipleShelfIds_returnsShelfBooks() {
OpdsUserDetails details = v2UserDetails(1L, false, Set.of(1L));
BookLoreUserEntity entity = mock(BookLoreUserEntity.class);
var permissionsEntity = mock(com.adityachandel.booklore.model.entity.UserPermissionsEntity.class);
when(permissionsEntity.isPermissionAccessOpds()).thenReturn(true);
when(permissionsEntity.isPermissionAdmin()).thenReturn(false);
when(entity.getPermissions()).thenReturn(permissionsEntity);
when(userRepository.findById(1L)).thenReturn(Optional.of(entity));
BookLoreUser user = mock(BookLoreUser.class);
BookLoreUser.UserPermissions perms = mock(BookLoreUser.UserPermissions.class);
when(bookLoreUserTransformer.toDTO(entity)).thenReturn(user);
when(user.getPermissions()).thenReturn(perms);
when(perms.isAdmin()).thenReturn(false);
when(perms.isCanAccessOpds()).thenReturn(true);
when(user.getId()).thenReturn(1L);
when(user.getAssignedLibraries()).thenReturn(List.of(Library.builder().id(1L).watch(false).build()));
ShelfEntity shelf1 = mock(ShelfEntity.class);
ShelfEntity shelf2 = mock(ShelfEntity.class);
BookLoreUserEntity shelfUser = mock(BookLoreUserEntity.class);
when(shelfUser.getId()).thenReturn(1L);
when(shelf1.getUser()).thenReturn(shelfUser);
when(shelf2.getUser()).thenReturn(shelfUser);
when(shelfRepository.findById(10L)).thenReturn(Optional.of(shelf1));
when(shelfRepository.findById(20L)).thenReturn(Optional.of(shelf2));
BookEntity bookEntity1 = mock(BookEntity.class);
BookEntity bookEntity2 = mock(BookEntity.class);
when(bookEntity1.getId()).thenReturn(1L);
when(bookEntity2.getId()).thenReturn(2L);
Book book1 = Book.builder().id(1L).build();
Book book2 = Book.builder().id(2L).build();
when(bookMapper.toBook(bookEntity1)).thenReturn(book1);
when(bookMapper.toBook(bookEntity2)).thenReturn(book2);
when(bookOpdsRepository.findBookIdsByShelfIds(eq(Set.of(10L, 20L)), any())).thenReturn(new PageImpl<>(List.of(1L, 2L)));
when(bookOpdsRepository.findAllWithMetadataByIdsAndShelfIds(eq(List.of(1L, 2L)), eq(Set.of(10L, 20L)))).thenReturn(List.of(bookEntity1, bookEntity2));
Page<Book> result = opdsBookService.getBooksPage(1L, null, null, Set.of(10L, 20L), 0, 10);
assertThat(result.getContent()).hasSize(2);
verify(bookOpdsRepository).findBookIdsByShelfIds(eq(Set.of(10L, 20L)), any());
}
@Test
void getBooksPage_withShelfIdAndQuery_searchesInShelf() {
OpdsUserDetails details = v2UserDetails(1L, false, Set.of(1L));
BookLoreUserEntity entity = mock(BookLoreUserEntity.class);
var permissionsEntity = mock(com.adityachandel.booklore.model.entity.UserPermissionsEntity.class);
when(permissionsEntity.isPermissionAccessOpds()).thenReturn(true);
when(permissionsEntity.isPermissionAdmin()).thenReturn(false);
when(entity.getPermissions()).thenReturn(permissionsEntity);
when(userRepository.findById(1L)).thenReturn(Optional.of(entity));
BookLoreUser user = mock(BookLoreUser.class);
BookLoreUser.UserPermissions perms = mock(BookLoreUser.UserPermissions.class);
when(bookLoreUserTransformer.toDTO(entity)).thenReturn(user);
when(user.getPermissions()).thenReturn(perms);
when(perms.isAdmin()).thenReturn(false);
when(perms.isCanAccessOpds()).thenReturn(true);
when(user.getId()).thenReturn(1L);
when(user.getAssignedLibraries()).thenReturn(List.of(Library.builder().id(1L).watch(false).build()));
ShelfEntity shelf = mock(ShelfEntity.class);
BookLoreUserEntity shelfUser = mock(BookLoreUserEntity.class);
when(shelfUser.getId()).thenReturn(1L);
when(shelf.getUser()).thenReturn(shelfUser);
when(shelfRepository.findById(10L)).thenReturn(Optional.of(shelf));
BookEntity bookEntity = mock(BookEntity.class);
when(bookEntity.getId()).thenReturn(1L);
Book book = Book.builder().id(1L).build();
when(bookMapper.toBook(bookEntity)).thenReturn(book);
when(bookOpdsRepository.findBookIdsByMetadataSearchAndShelfIds(eq("test"), eq(Set.of(10L)), any())).thenReturn(new PageImpl<>(List.of(1L)));
when(bookOpdsRepository.findAllWithFullMetadataByIdsAndShelfIds(eq(List.of(1L)), eq(Set.of(10L)))).thenReturn(List.of(bookEntity));
Page<Book> result = opdsBookService.getBooksPage(1L, "test", null, Set.of(10L), 0, 10);
assertThat(result.getContent()).hasSize(1);
verify(bookOpdsRepository).findBookIdsByMetadataSearchAndShelfIds(eq("test"), eq(Set.of(10L)), any());
}
@Test
void getBooksPage_withShelfId_throwsForbidden_whenNotOwner() {
OpdsUserDetails details = v2UserDetails(1L, false, Set.of(1L));
BookLoreUserEntity entity = mock(BookLoreUserEntity.class);
var permissionsEntity = mock(com.adityachandel.booklore.model.entity.UserPermissionsEntity.class);
when(permissionsEntity.isPermissionAccessOpds()).thenReturn(true);
when(permissionsEntity.isPermissionAdmin()).thenReturn(false);
when(entity.getPermissions()).thenReturn(permissionsEntity);
when(userRepository.findById(1L)).thenReturn(Optional.of(entity));
BookLoreUser user = mock(BookLoreUser.class);
BookLoreUser.UserPermissions perms = mock(BookLoreUser.UserPermissions.class);
when(bookLoreUserTransformer.toDTO(entity)).thenReturn(user);
when(user.getPermissions()).thenReturn(perms);
when(perms.isAdmin()).thenReturn(false);
when(perms.isCanAccessOpds()).thenReturn(true);
when(user.getId()).thenReturn(1L);
when(user.getAssignedLibraries()).thenReturn(List.of(Library.builder().id(1L).watch(false).build()));
ShelfEntity shelf = mock(ShelfEntity.class);
BookLoreUserEntity shelfUser = mock(BookLoreUserEntity.class);
when(shelfUser.getId()).thenReturn(999L); // Different user
when(shelf.getUser()).thenReturn(shelfUser);
when(shelfRepository.findById(10L)).thenReturn(Optional.of(shelf));
assertThatThrownBy(() ->
opdsBookService.getBooksPage(1L, null, null, Set.of(10L), 0, 10)
).hasMessageContaining("You are not allowed to access this shelf");
}
@Test
void getBooksPage_withShelfId_allowsAdmin_evenIfNotOwner() {
OpdsUserDetails details = v2UserDetails(1L, true, Set.of(1L));
BookLoreUserEntity entity = mock(BookLoreUserEntity.class);
var permissionsEntity = mock(com.adityachandel.booklore.model.entity.UserPermissionsEntity.class);
when(permissionsEntity.isPermissionAccessOpds()).thenReturn(true);
when(permissionsEntity.isPermissionAdmin()).thenReturn(true);
when(entity.getPermissions()).thenReturn(permissionsEntity);
when(userRepository.findById(1L)).thenReturn(Optional.of(entity));
BookLoreUser user = mock(BookLoreUser.class);
BookLoreUser.UserPermissions perms = mock(BookLoreUser.UserPermissions.class);
when(bookLoreUserTransformer.toDTO(entity)).thenReturn(user);
when(user.getPermissions()).thenReturn(perms);
when(perms.isAdmin()).thenReturn(true);
when(perms.isCanAccessOpds()).thenReturn(true);
when(user.getId()).thenReturn(1L);
when(user.getAssignedLibraries()).thenReturn(List.of(Library.builder().id(1L).watch(false).build()));
ShelfEntity shelf = mock(ShelfEntity.class);
BookLoreUserEntity shelfUser = mock(BookLoreUserEntity.class);
when(shelfUser.getId()).thenReturn(999L); // Different user, but admin
when(shelf.getUser()).thenReturn(shelfUser);
when(shelfRepository.findById(10L)).thenReturn(Optional.of(shelf));
when(bookOpdsRepository.findBookIdsByShelfIds(eq(Set.of(10L)), any())).thenReturn(Page.empty());
Page<Book> result = opdsBookService.getBooksPage(1L, null, null, Set.of(10L), 0, 10);
assertThat(result).isNotNull();
verify(bookOpdsRepository).findBookIdsByShelfIds(eq(Set.of(10L)), any());
}
}
@@ -311,4 +311,168 @@ class OpdsFeedServiceTest {
assertThat(userId).isNull();
}
@Test
void parseShelfIds_shouldParseSingleShelfId() throws Exception {
when(request.getParameter("shelfId")).thenReturn("10");
when(request.getParameter("shelfIds")).thenReturn(null);
var method = OpdsFeedService.class.getDeclaredMethod("parseShelfIds", HttpServletRequest.class);
method.setAccessible(true);
Set<Long> result = (Set<Long>) method.invoke(opdsFeedService, request);
assertThat(result).containsExactly(10L);
}
@Test
void parseShelfIds_shouldParseCommaSeparatedShelfIds() throws Exception {
when(request.getParameter("shelfId")).thenReturn(null);
when(request.getParameter("shelfIds")).thenReturn("10,20,30");
var method = OpdsFeedService.class.getDeclaredMethod("parseShelfIds", HttpServletRequest.class);
method.setAccessible(true);
Set<Long> result = (Set<Long>) method.invoke(opdsFeedService, request);
assertThat(result).containsExactlyInAnyOrder(10L, 20L, 30L);
}
@Test
void parseShelfIds_shouldCombineShelfIdAndShelfIds() throws Exception {
when(request.getParameter("shelfId")).thenReturn("5");
when(request.getParameter("shelfIds")).thenReturn("10,20");
var method = OpdsFeedService.class.getDeclaredMethod("parseShelfIds", HttpServletRequest.class);
method.setAccessible(true);
Set<Long> result = (Set<Long>) method.invoke(opdsFeedService, request);
assertThat(result).containsExactlyInAnyOrder(5L, 10L, 20L);
}
@Test
void parseShelfIds_shouldReturnNullWhenNoShelfIds() throws Exception {
when(request.getParameter("shelfId")).thenReturn(null);
when(request.getParameter("shelfIds")).thenReturn(null);
var method = OpdsFeedService.class.getDeclaredMethod("parseShelfIds", HttpServletRequest.class);
method.setAccessible(true);
Set<Long> result = (Set<Long>) method.invoke(opdsFeedService, request);
assertThat(result).isNull();
}
@Test
void parseShelfIds_shouldHandleInvalidIds() throws Exception {
when(request.getParameter("shelfId")).thenReturn("invalid");
when(request.getParameter("shelfIds")).thenReturn("10,abc,20");
var method = OpdsFeedService.class.getDeclaredMethod("parseShelfIds", HttpServletRequest.class);
method.setAccessible(true);
Set<Long> result = (Set<Long>) method.invoke(opdsFeedService, request);
// Should only parse valid IDs
assertThat(result).containsExactlyInAnyOrder(10L, 20L);
}
@Test
void generateCatalogFeed_withSingleShelfId_shouldFilterByShelf() {
mockAuthenticatedUser();
when(request.getParameter("libraryId")).thenReturn(null);
when(request.getParameter("shelfId")).thenReturn("10");
when(request.getParameter("shelfIds")).thenReturn(null);
when(request.getParameter("magicShelfId")).thenReturn(null);
when(request.getParameter("q")).thenReturn(null);
when(request.getParameter("author")).thenReturn(null);
when(request.getParameter("series")).thenReturn(null);
when(request.getParameter("page")).thenReturn(null);
when(request.getParameter("size")).thenReturn(null);
when(request.getRequestURI()).thenReturn("/api/v1/opds/catalog");
when(request.getQueryString()).thenReturn("shelfId=10");
Book book = Book.builder()
.id(1L)
.bookType(BookFileType.EPUB)
.addedOn(FIXED_INSTANT)
.metadata(BookMetadata.builder().title("Shelf Book").build())
.build();
Page<Book> page = new PageImpl<>(List.of(book), PageRequest.of(0, 50), 1);
when(opdsBookService.getBooksPage(eq(TEST_USER_ID), isNull(), isNull(), eq(Set.of(10L)), eq(0), eq(50))).thenReturn(page);
when(opdsBookService.applySortOrder(any(), any())).thenReturn(page);
when(opdsBookService.getShelfName(10L)).thenReturn("My Shelf - Shelf");
String xml = opdsFeedService.generateCatalogFeed(request);
assertThat(xml).contains("Shelf Book");
assertThat(xml).contains("My Shelf - Shelf");
assertThat(xml).contains("</feed>");
verify(opdsBookService).getBooksPage(TEST_USER_ID, null, null, Set.of(10L), 0, 50);
}
@Test
void generateCatalogFeed_withMultipleShelfIds_shouldFilterByMultipleShelves() {
mockAuthenticatedUser();
when(request.getParameter("libraryId")).thenReturn(null);
when(request.getParameter("shelfId")).thenReturn(null);
when(request.getParameter("shelfIds")).thenReturn("10,20");
when(request.getParameter("magicShelfId")).thenReturn(null);
when(request.getParameter("q")).thenReturn(null);
when(request.getParameter("author")).thenReturn(null);
when(request.getParameter("series")).thenReturn(null);
when(request.getParameter("page")).thenReturn(null);
when(request.getParameter("size")).thenReturn(null);
when(request.getRequestURI()).thenReturn("/api/v1/opds/catalog");
when(request.getQueryString()).thenReturn("shelfIds=10,20");
Book book = Book.builder()
.id(1L)
.bookType(BookFileType.EPUB)
.addedOn(FIXED_INSTANT)
.metadata(BookMetadata.builder().title("Multi Shelf Book").build())
.build();
Page<Book> page = new PageImpl<>(List.of(book), PageRequest.of(0, 50), 1);
when(opdsBookService.getBooksPage(eq(TEST_USER_ID), isNull(), isNull(), eq(Set.of(10L, 20L)), eq(0), eq(50))).thenReturn(page);
when(opdsBookService.applySortOrder(any(), any())).thenReturn(page);
String xml = opdsFeedService.generateCatalogFeed(request);
assertThat(xml).contains("Multi Shelf Book");
assertThat(xml).contains("Multiple Shelves");
assertThat(xml).contains("</feed>");
verify(opdsBookService).getBooksPage(TEST_USER_ID, null, null, Set.of(10L, 20L), 0, 50);
}
@Test
void generateCatalogFeed_withShelfIdAndQuery_shouldSearchInShelf() {
mockAuthenticatedUser();
when(request.getParameter("libraryId")).thenReturn(null);
when(request.getParameter("shelfId")).thenReturn("10");
when(request.getParameter("shelfIds")).thenReturn(null);
when(request.getParameter("magicShelfId")).thenReturn(null);
when(request.getParameter("q")).thenReturn("fantasy");
when(request.getParameter("author")).thenReturn(null);
when(request.getParameter("series")).thenReturn(null);
when(request.getParameter("page")).thenReturn(null);
when(request.getParameter("size")).thenReturn(null);
when(request.getRequestURI()).thenReturn("/api/v1/opds/catalog");
when(request.getQueryString()).thenReturn("shelfId=10&q=fantasy");
Book book = Book.builder()
.id(1L)
.bookType(BookFileType.EPUB)
.addedOn(FIXED_INSTANT)
.metadata(BookMetadata.builder().title("Fantasy Book").build())
.build();
Page<Book> page = new PageImpl<>(List.of(book), PageRequest.of(0, 50), 1);
when(opdsBookService.getBooksPage(eq(TEST_USER_ID), eq("fantasy"), isNull(), eq(Set.of(10L)), eq(0), eq(50))).thenReturn(page);
when(opdsBookService.applySortOrder(any(), any())).thenReturn(page);
when(opdsBookService.getShelfName(10L)).thenReturn("Fantasy Shelf - Shelf");
String xml = opdsFeedService.generateCatalogFeed(request);
assertThat(xml).contains("Fantasy Book");
assertThat(xml).contains("</feed>");
verify(opdsBookService).getBooksPage(TEST_USER_ID, "fantasy", null, Set.of(10L), 0, 50);
}
}