Fix: Two users cannot put the same book on their Kobo shelves (#1189)

This commit is contained in:
Aditya Chandel
2025-09-22 18:04:41 -06:00
committed by GitHub
parent c82bc3ced7
commit 6565368a55
5 changed files with 46 additions and 82 deletions
@@ -12,4 +12,5 @@ public class Shelf {
private String name;
private String icon;
private Sort sort;
private Long userId;
}
@@ -11,8 +11,6 @@ import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import java.util.List;
@@ -28,88 +26,36 @@ public class BookQueryService {
public List<Book> getAllBooks(boolean includeDescription) {
List<BookEntity> books = bookRepository.findAllWithMetadata();
return books.stream()
.map(book -> {
Book dto = bookMapperV2.toDTO(book);
if (!includeDescription && dto.getMetadata() != null) {
dto.getMetadata().setDescription(null);
}
return dto;
})
.collect(Collectors.toList());
return mapBooksToDto(books, includeDescription, null);
}
public Page<Book> getAllBooksPage(boolean includeDescription, int page, int size) {
Pageable pageable = PageRequest.of(Math.max(page - 1, 0), size);
Page<BookEntity> books = bookRepository.findAllWithMetadata(pageable);
List<Book> mapped = books.getContent().stream()
.map(book -> {
Book dto = bookMapperV2.toDTO(book);
if (!includeDescription && dto.getMetadata() != null) {
dto.getMetadata().setDescription(null);
}
return dto;
})
.collect(Collectors.toList());
return new PageImpl<>(mapped, pageable, books.getTotalElements());
return createPageFromBooks(books, pageable, includeDescription, null);
}
public Page<Book> getRecentBooksPage(boolean includeDescription, int page, int size) {
Pageable pageable = PageRequest.of(Math.max(page - 1, 0), size, Sort.by("addedOn").descending());
Page<BookEntity> books = bookRepository.findAllWithMetadata(pageable);
List<Book> mapped = books.getContent().stream()
.map(book -> {
Book dto = bookMapperV2.toDTO(book);
if (!includeDescription && dto.getMetadata() != null) {
dto.getMetadata().setDescription(null);
}
return dto;
})
.collect(Collectors.toList());
return new PageImpl<>(mapped, pageable, books.getTotalElements());
return createPageFromBooks(books, pageable, includeDescription, null);
}
public List<Book> getAllBooksByLibraryIds(Set<Long> libraryIds, boolean includeDescription) {
public List<Book> getAllBooksByLibraryIds(Set<Long> libraryIds, boolean includeDescription, Long userId) {
List<BookEntity> books = bookRepository.findAllWithMetadataByLibraryIds(libraryIds);
return books.stream()
.map(book -> {
Book dto = bookMapperV2.toDTO(book);
if (!includeDescription && dto.getMetadata() != null) {
dto.getMetadata().setDescription(null);
}
return dto;
})
.collect(Collectors.toList());
return mapBooksToDto(books, includeDescription, userId);
}
public Page<Book> getAllBooksByLibraryIdsPage(Set<Long> libraryIds, boolean includeDescription, int page, int size) {
public Page<Book> getAllBooksByLibraryIdsPage(Set<Long> libraryIds, boolean includeDescription, int page, int size, Long userId) {
Pageable pageable = PageRequest.of(Math.max(page - 1, 0), size);
Page<BookEntity> books = bookRepository.findAllWithMetadataByLibraryIds(libraryIds, pageable);
List<Book> mapped = books.getContent().stream()
.map(book -> {
Book dto = bookMapperV2.toDTO(book);
if (!includeDescription && dto.getMetadata() != null) {
dto.getMetadata().setDescription(null);
}
return dto;
})
.collect(Collectors.toList());
return new PageImpl<>(mapped, pageable, books.getTotalElements());
return createPageFromBooks(books, pageable, includeDescription, userId);
}
public Page<Book> getRecentBooksByLibraryIdsPage(Set<Long> libraryIds, boolean includeDescription, int page, int size) {
public Page<Book> getRecentBooksByLibraryIdsPage(Set<Long> libraryIds, boolean includeDescription, int page, int size, Long userId) {
Pageable pageable = PageRequest.of(Math.max(page - 1, 0), size, Sort.by("addedOn").descending());
Page<BookEntity> books = bookRepository.findAllWithMetadataByLibraryIds(libraryIds, pageable);
List<Book> mapped = books.getContent().stream()
.map(book -> {
Book dto = bookMapperV2.toDTO(book);
if (!includeDescription && dto.getMetadata() != null) {
dto.getMetadata().setDescription(null);
}
return dto;
})
.collect(Collectors.toList());
return new PageImpl<>(mapped, pageable, books.getTotalElements());
return createPageFromBooks(books, pageable, includeDescription, userId);
}
public List<BookEntity> findAllWithMetadataByIds(Set<Long> bookIds) {
@@ -160,21 +106,37 @@ public class BookQueryService {
public Page<Book> getAllBooksByShelfPage(Long shelfId, boolean includeDescription, int page, int size) {
Pageable pageable = PageRequest.of(Math.max(page - 1, 0), size);
Page<BookEntity> books = bookRepository.findAllWithMetadataByShelfId(shelfId, pageable);
List<Book> mapped = books.getContent().stream()
.map(book -> {
Book dto = bookMapperV2.toDTO(book);
if (!includeDescription && dto.getMetadata() != null) {
dto.getMetadata().setDescription(null);
}
return dto;
})
.collect(Collectors.toList());
return new PageImpl<>(mapped, pageable, books.getTotalElements());
return createPageFromBooks(books, pageable, includeDescription, null);
}
public void saveAll(List<BookEntity> books) {
bookRepository.saveAll(books);
}
// Removed OPDS Magic Shelves support
private List<Book> mapBooksToDto(List<BookEntity> books, boolean includeDescription, Long userId) {
return books.stream()
.map(book -> mapBookToDto(book, includeDescription, userId))
.collect(Collectors.toList());
}
private Book mapBookToDto(BookEntity bookEntity, boolean includeDescription, Long userId) {
Book dto = bookMapperV2.toDTO(bookEntity);
if (!includeDescription && dto.getMetadata() != null) {
dto.getMetadata().setDescription(null);
}
if (dto.getShelves() != null && userId != null) {
dto.setShelves(dto.getShelves().stream()
.filter(shelf -> userId.equals(shelf.getUserId()))
.collect(Collectors.toSet()));
}
return dto;
}
private Page<Book> createPageFromBooks(Page<BookEntity> books, Pageable pageable, boolean includeDescription, Long userId) {
List<Book> mapped = mapBooksToDto(books.getContent(), includeDescription, userId);
return new PageImpl<>(mapped, pageable, books.getTotalElements());
}
}
@@ -103,7 +103,8 @@ public class BookService {
user.getAssignedLibraries().stream()
.map(Library::getId)
.collect(Collectors.toSet()),
includeDescription
includeDescription,
user.getId()
);
Map<Long, UserBookProgressEntity> progressMap =
@@ -112,7 +112,7 @@ public class OpdsService {
java.util.Set<Long> libraryIds = user.getAssignedLibraries().stream()
.map(Library::getId)
.collect(java.util.stream.Collectors.toSet());
var result = bookQueryService.getRecentBooksByLibraryIdsPage(libraryIds, true, page, size);
var result = bookQueryService.getRecentBooksByLibraryIdsPage(libraryIds, true, page, size, v2.getUserId());
return generateOpdsV2Feed(result.getContent(), result.getTotalElements(), "/api/v2/opds/recent", qp, page, size);
}
@@ -340,7 +340,7 @@ public class OpdsService {
} else {
return (queryParam != null)
? bookQueryService.searchBooksByMetadataInLibraries(queryParam, libraryIds)
: bookQueryService.getAllBooksByLibraryIds(libraryIds, true);
: bookQueryService.getAllBooksByLibraryIds(libraryIds, true, opdsUserV2.getUserId());
}
}
@@ -682,7 +682,7 @@ public class OpdsService {
return bookQueryService.getAllBooksByShelfPage(shelfId, true, page, size);
}
if (libraryId != null) {
return bookQueryService.getAllBooksByLibraryIdsPage(java.util.Set.of(libraryId), true, page, size);
return bookQueryService.getAllBooksByLibraryIdsPage(java.util.Set.of(libraryId), true, page, size, null);
}
if (queryParam != null && !queryParam.isBlank()) {
return bookQueryService.searchBooksByMetadataPage(queryParam, page, size);
@@ -718,7 +718,7 @@ public class OpdsService {
}
return (queryParam != null && !queryParam.isBlank())
? bookQueryService.searchBooksByMetadataInLibrariesPage(queryParam, java.util.Set.of(libraryId), page, size)
: bookQueryService.getAllBooksByLibraryIdsPage(java.util.Set.of(libraryId), true, page, size);
: bookQueryService.getAllBooksByLibraryIdsPage(java.util.Set.of(libraryId), true, page, size, opdsUserV2.getUserId());
}
if (isAdmin) {
@@ -729,7 +729,7 @@ public class OpdsService {
return (queryParam != null && !queryParam.isBlank())
? bookQueryService.searchBooksByMetadataInLibrariesPage(queryParam, libraryIds, page, size)
: bookQueryService.getAllBooksByLibraryIdsPage(libraryIds, true, page, size);
: bookQueryService.getAllBooksByLibraryIdsPage(libraryIds, true, page, size, opdsUserV2.getUserId());
}
}
@@ -192,12 +192,12 @@ class OpdsServiceTest {
when(request.getHeader("Accept")).thenReturn(null); // v1 default; feed version not relevant for allowed-books logic
when(request.getRequestURI()).thenReturn("/opds/catalog");
when(bookQueryService.getAllBooksByLibraryIds(Set.of(7L), true)).thenReturn(List.of());
when(bookQueryService.getAllBooksByLibraryIds(Set.of(7L), true, 21L)).thenReturn(List.of());
String feed = service.generateCatalogFeed(request);
assertNotNull(feed);
verify(bookQueryService).getAllBooksByLibraryIds(Set.of(7L), true);
verify(bookQueryService).getAllBooksByLibraryIds(Set.of(7L), true, 21L);
verify(userRepository).findById(21L);
}