fix(opds): add library restrictions to shelf OPDS catalogs (#1731)
This commit is contained in:
@@ -115,36 +115,58 @@ public interface BookOpdsRepository extends JpaRepository<BookEntity, Long>, Jpa
|
||||
LEFT JOIN b.metadata m
|
||||
JOIN b.shelves s
|
||||
WHERE (b.deleted IS NULL OR b.deleted = false)
|
||||
AND b.library.id IN :libraryIds
|
||||
AND s.id IN :shelfIds
|
||||
AND (
|
||||
m.searchText LIKE CONCAT('%', :text, '%')
|
||||
)
|
||||
ORDER BY b.addedOn DESC
|
||||
""")
|
||||
Page<Long> findBookIdsByMetadataSearchAndShelfIds(@Param("text") String text, @Param("shelfIds") Collection<Long> shelfIds, Pageable pageable);
|
||||
""")
|
||||
Page<Long> findBookIdsByMetadataSearchAndShelfIds(@Param("text") String text, @Param("libraryIds") Collection<Long> libraryIds, @Param("shelfIds") Collection<Long> shelfIds, Pageable pageable);
|
||||
|
||||
@EntityGraph(attributePaths = {"metadata", "metadata.authors", "metadata.categories", "bookFiles", "shelves"})
|
||||
@Query("SELECT DISTINCT b FROM BookEntity b JOIN b.shelves s WHERE b.id IN :ids AND s.id IN :shelfIds AND (b.deleted IS NULL OR b.deleted = false)")
|
||||
List<BookEntity> findAllWithFullMetadataByIdsAndShelfIds(@Param("ids") Collection<Long> ids, @Param("shelfIds") Collection<Long> shelfIds);
|
||||
@Query("""
|
||||
SELECT DISTINCT b FROM BookEntity b
|
||||
JOIN b.shelves s
|
||||
WHERE
|
||||
b.id IN :ids
|
||||
AND b.library.id IN :libraryIds
|
||||
AND s.id IN :shelfIds
|
||||
AND (b.deleted IS NULL OR b.deleted = false)
|
||||
""")
|
||||
List<BookEntity> findAllWithFullMetadataByIdsAndShelfIds(@Param("ids") Collection<Long> ids, @Param("libraryIds") Collection<Long> libraryIds, @Param("shelfIds") Collection<Long> shelfIds);
|
||||
|
||||
// ============================================
|
||||
// BOOKS BY SHELF IDs - Two Query Pattern
|
||||
// ============================================
|
||||
|
||||
@Query("SELECT DISTINCT b.id FROM BookEntity b JOIN b.shelves s WHERE s.id IN :shelfIds AND (b.deleted IS NULL OR b.deleted = false) ORDER BY b.addedOn DESC")
|
||||
Page<Long> findBookIdsByShelfIds(@Param("shelfIds") Collection<Long> shelfIds, Pageable pageable);
|
||||
@Query("""
|
||||
SELECT DISTINCT b.id FROM BookEntity b
|
||||
JOIN b.shelves s
|
||||
WHERE
|
||||
b.library.id IN :libraryIds
|
||||
AND s.id IN :shelfIds
|
||||
AND (b.deleted IS NULL OR b.deleted = false)
|
||||
ORDER BY b.addedOn DESC
|
||||
""")
|
||||
Page<Long> findBookIdsByShelfIds(@Param("libraryIds") Collection<Long> libraryIds, @Param("shelfIds") Collection<Long> shelfIds, Pageable pageable);
|
||||
|
||||
@EntityGraph(attributePaths = {"metadata", "bookFiles", "shelves"})
|
||||
@Query("SELECT DISTINCT b FROM BookEntity b JOIN b.shelves s WHERE b.id IN :ids AND s.id IN :shelfIds AND (b.deleted IS NULL OR b.deleted = false)")
|
||||
List<BookEntity> findAllWithMetadataByIdsAndShelfIds(@Param("ids") Collection<Long> ids, @Param("shelfIds") Collection<Long> shelfIds);
|
||||
@Query("""
|
||||
SELECT DISTINCT b FROM BookEntity b
|
||||
JOIN b.shelves s
|
||||
WHERE
|
||||
b.id IN :ids
|
||||
AND b.library.id IN :libraryIds
|
||||
AND s.id IN :shelfIds
|
||||
AND (b.deleted IS NULL OR b.deleted = false)
|
||||
""")
|
||||
List<BookEntity> findAllWithMetadataByIdsAndShelfIds(@Param("ids") Collection<Long> ids, @Param("libraryIds") Collection<Long> libraryIds, @Param("shelfIds") Collection<Long> shelfIds);
|
||||
|
||||
// ============================================
|
||||
// RANDOM BOOKS - "Surprise Me" Feed
|
||||
// ============================================
|
||||
|
||||
@Query("SELECT b.id FROM BookEntity b WHERE (b.deleted IS NULL OR b.deleted = false) ORDER BY function('RAND')")
|
||||
List<Long> findRandomBookIds(Pageable pageable);
|
||||
|
||||
@Query("SELECT b.id FROM BookEntity b WHERE b.library.id IN :libraryIds AND (b.deleted IS NULL OR b.deleted = false) ORDER BY function('RAND')")
|
||||
List<Long> findRandomBookIdsByLibraryIds(@Param("libraryIds") Collection<Long> libraryIds, Pageable pageable);
|
||||
|
||||
|
||||
@@ -82,15 +82,13 @@ public class OpdsBookService {
|
||||
|
||||
BookLoreUser user = bookLoreUserTransformer.toDTO(entity);
|
||||
boolean isAdmin = user.getPermissions().isAdmin();
|
||||
Set<Long> userLibraryIds = user.getAssignedLibraries().stream()
|
||||
.map(Library::getId)
|
||||
.collect(Collectors.toSet());
|
||||
Set<Long> userLibraryIds = getAccessibleLibraries(userId).stream().map(Library::getId).collect(Collectors.toSet());
|
||||
|
||||
if (shelfIds != null && !shelfIds.isEmpty()) {
|
||||
validateShelfAccess(shelfIds, user.getId(), isAdmin);
|
||||
Page<Book> books = query != null && !query.isBlank()
|
||||
? searchByMetadataInShelvesPageInternal(BookUtils.normalizeForSearch(query), shelfIds, page, size, userId)
|
||||
: getBooksByShelfIdsPageInternal(shelfIds, page, size, userId);
|
||||
? searchByMetadataInShelvesPageInternal(BookUtils.normalizeForSearch(query), userLibraryIds, shelfIds, page, size, userId)
|
||||
: getBooksByShelfIdsPageInternal(userLibraryIds, shelfIds, page, size, userId);
|
||||
return applyBookFilters(books, userId);
|
||||
}
|
||||
|
||||
@@ -359,15 +357,15 @@ public class OpdsBookService {
|
||||
return createPageFromEntities(books, idPage, pageable, userId);
|
||||
}
|
||||
|
||||
private Page<Book> getBooksByShelfIdsPageInternal(Set<Long> shelfIds, int page, int size, Long userId) {
|
||||
private Page<Book> getBooksByShelfIdsPageInternal(Set<Long> libraryIds, Set<Long> shelfIds, int page, int size, Long userId) {
|
||||
Pageable pageable = PageRequest.of(Math.max(page, 0), size);
|
||||
|
||||
Page<Long> idPage = bookOpdsRepository.findBookIdsByShelfIds(shelfIds, pageable);
|
||||
Page<Long> idPage = bookOpdsRepository.findBookIdsByShelfIds(libraryIds, shelfIds, pageable);
|
||||
if (idPage.isEmpty()) {
|
||||
return new PageImpl<>(List.of(), pageable, 0);
|
||||
}
|
||||
|
||||
List<BookEntity> books = bookOpdsRepository.findAllWithMetadataByIdsAndShelfIds(idPage.getContent(), shelfIds);
|
||||
List<BookEntity> books = bookOpdsRepository.findAllWithMetadataByIdsAndShelfIds(idPage.getContent(), libraryIds, shelfIds);
|
||||
return createPageFromEntities(books, idPage, pageable, userId);
|
||||
}
|
||||
|
||||
@@ -395,15 +393,15 @@ public class OpdsBookService {
|
||||
return createPageFromEntities(books, idPage, pageable, userId);
|
||||
}
|
||||
|
||||
private Page<Book> searchByMetadataInShelvesPageInternal(String text, Set<Long> shelfIds, int page, int size, Long userId) {
|
||||
private Page<Book> searchByMetadataInShelvesPageInternal(String text, Set<Long> libraryIds, Set<Long> shelfIds, int page, int size, Long userId) {
|
||||
Pageable pageable = PageRequest.of(Math.max(page, 0), size);
|
||||
|
||||
Page<Long> idPage = bookOpdsRepository.findBookIdsByMetadataSearchAndShelfIds(text, shelfIds, pageable);
|
||||
Page<Long> idPage = bookOpdsRepository.findBookIdsByMetadataSearchAndShelfIds(text, libraryIds, shelfIds, pageable);
|
||||
if (idPage.isEmpty()) {
|
||||
return new PageImpl<>(List.of(), pageable, 0);
|
||||
}
|
||||
|
||||
List<BookEntity> books = bookOpdsRepository.findAllWithFullMetadataByIdsAndShelfIds(idPage.getContent(), shelfIds);
|
||||
List<BookEntity> books = bookOpdsRepository.findAllWithFullMetadataByIdsAndShelfIds(idPage.getContent(), libraryIds, shelfIds);
|
||||
return createPageFromEntities(books, idPage, pageable, userId);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user