Fix: Two users cannot put the same book on their Kobo shelves (#1189)
This commit is contained in:
@@ -12,4 +12,5 @@ public class Shelf {
|
|||||||
private String name;
|
private String name;
|
||||||
private String icon;
|
private String icon;
|
||||||
private Sort sort;
|
private Sort sort;
|
||||||
|
private Long userId;
|
||||||
}
|
}
|
||||||
|
|||||||
+36
-74
@@ -11,8 +11,6 @@ import org.springframework.stereotype.Service;
|
|||||||
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.PageImpl;
|
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 org.springframework.data.domain.Sort;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -28,88 +26,36 @@ public class BookQueryService {
|
|||||||
|
|
||||||
public List<Book> getAllBooks(boolean includeDescription) {
|
public List<Book> getAllBooks(boolean includeDescription) {
|
||||||
List<BookEntity> books = bookRepository.findAllWithMetadata();
|
List<BookEntity> books = bookRepository.findAllWithMetadata();
|
||||||
return books.stream()
|
return mapBooksToDto(books, includeDescription, null);
|
||||||
.map(book -> {
|
|
||||||
Book dto = bookMapperV2.toDTO(book);
|
|
||||||
if (!includeDescription && dto.getMetadata() != null) {
|
|
||||||
dto.getMetadata().setDescription(null);
|
|
||||||
}
|
|
||||||
return dto;
|
|
||||||
})
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Page<Book> getAllBooksPage(boolean includeDescription, int page, int size) {
|
public Page<Book> getAllBooksPage(boolean includeDescription, int page, int size) {
|
||||||
Pageable pageable = PageRequest.of(Math.max(page - 1, 0), size);
|
Pageable pageable = PageRequest.of(Math.max(page - 1, 0), size);
|
||||||
Page<BookEntity> books = bookRepository.findAllWithMetadata(pageable);
|
Page<BookEntity> books = bookRepository.findAllWithMetadata(pageable);
|
||||||
List<Book> mapped = books.getContent().stream()
|
return createPageFromBooks(books, pageable, includeDescription, null);
|
||||||
.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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Page<Book> getRecentBooksPage(boolean includeDescription, int page, int size) {
|
public Page<Book> getRecentBooksPage(boolean includeDescription, int page, int size) {
|
||||||
Pageable pageable = PageRequest.of(Math.max(page - 1, 0), size, Sort.by("addedOn").descending());
|
Pageable pageable = PageRequest.of(Math.max(page - 1, 0), size, Sort.by("addedOn").descending());
|
||||||
Page<BookEntity> books = bookRepository.findAllWithMetadata(pageable);
|
Page<BookEntity> books = bookRepository.findAllWithMetadata(pageable);
|
||||||
List<Book> mapped = books.getContent().stream()
|
return createPageFromBooks(books, pageable, includeDescription, null);
|
||||||
.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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
List<BookEntity> books = bookRepository.findAllWithMetadataByLibraryIds(libraryIds);
|
||||||
return books.stream()
|
return mapBooksToDto(books, includeDescription, userId);
|
||||||
.map(book -> {
|
|
||||||
Book dto = bookMapperV2.toDTO(book);
|
|
||||||
if (!includeDescription && dto.getMetadata() != null) {
|
|
||||||
dto.getMetadata().setDescription(null);
|
|
||||||
}
|
|
||||||
return dto;
|
|
||||||
})
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
Pageable pageable = PageRequest.of(Math.max(page - 1, 0), size);
|
||||||
Page<BookEntity> books = bookRepository.findAllWithMetadataByLibraryIds(libraryIds, pageable);
|
Page<BookEntity> books = bookRepository.findAllWithMetadataByLibraryIds(libraryIds, pageable);
|
||||||
List<Book> mapped = books.getContent().stream()
|
return createPageFromBooks(books, pageable, includeDescription, userId);
|
||||||
.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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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());
|
Pageable pageable = PageRequest.of(Math.max(page - 1, 0), size, Sort.by("addedOn").descending());
|
||||||
Page<BookEntity> books = bookRepository.findAllWithMetadataByLibraryIds(libraryIds, pageable);
|
Page<BookEntity> books = bookRepository.findAllWithMetadataByLibraryIds(libraryIds, pageable);
|
||||||
List<Book> mapped = books.getContent().stream()
|
return createPageFromBooks(books, pageable, includeDescription, userId);
|
||||||
.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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<BookEntity> findAllWithMetadataByIds(Set<Long> bookIds) {
|
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) {
|
public Page<Book> getAllBooksByShelfPage(Long shelfId, boolean includeDescription, int page, int size) {
|
||||||
Pageable pageable = PageRequest.of(Math.max(page - 1, 0), size);
|
Pageable pageable = PageRequest.of(Math.max(page - 1, 0), size);
|
||||||
Page<BookEntity> books = bookRepository.findAllWithMetadataByShelfId(shelfId, pageable);
|
Page<BookEntity> books = bookRepository.findAllWithMetadataByShelfId(shelfId, pageable);
|
||||||
List<Book> mapped = books.getContent().stream()
|
return createPageFromBooks(books, pageable, includeDescription, null);
|
||||||
.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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveAll(List<BookEntity> books) {
|
public void saveAll(List<BookEntity> books) {
|
||||||
bookRepository.saveAll(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()
|
user.getAssignedLibraries().stream()
|
||||||
.map(Library::getId)
|
.map(Library::getId)
|
||||||
.collect(Collectors.toSet()),
|
.collect(Collectors.toSet()),
|
||||||
includeDescription
|
includeDescription,
|
||||||
|
user.getId()
|
||||||
);
|
);
|
||||||
|
|
||||||
Map<Long, UserBookProgressEntity> progressMap =
|
Map<Long, UserBookProgressEntity> progressMap =
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ public class OpdsService {
|
|||||||
java.util.Set<Long> libraryIds = user.getAssignedLibraries().stream()
|
java.util.Set<Long> libraryIds = user.getAssignedLibraries().stream()
|
||||||
.map(Library::getId)
|
.map(Library::getId)
|
||||||
.collect(java.util.stream.Collectors.toSet());
|
.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);
|
return generateOpdsV2Feed(result.getContent(), result.getTotalElements(), "/api/v2/opds/recent", qp, page, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -340,7 +340,7 @@ public class OpdsService {
|
|||||||
} else {
|
} else {
|
||||||
return (queryParam != null)
|
return (queryParam != null)
|
||||||
? bookQueryService.searchBooksByMetadataInLibraries(queryParam, libraryIds)
|
? 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);
|
return bookQueryService.getAllBooksByShelfPage(shelfId, true, page, size);
|
||||||
}
|
}
|
||||||
if (libraryId != null) {
|
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()) {
|
if (queryParam != null && !queryParam.isBlank()) {
|
||||||
return bookQueryService.searchBooksByMetadataPage(queryParam, page, size);
|
return bookQueryService.searchBooksByMetadataPage(queryParam, page, size);
|
||||||
@@ -718,7 +718,7 @@ public class OpdsService {
|
|||||||
}
|
}
|
||||||
return (queryParam != null && !queryParam.isBlank())
|
return (queryParam != null && !queryParam.isBlank())
|
||||||
? bookQueryService.searchBooksByMetadataInLibrariesPage(queryParam, java.util.Set.of(libraryId), page, size)
|
? 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) {
|
if (isAdmin) {
|
||||||
@@ -729,7 +729,7 @@ public class OpdsService {
|
|||||||
|
|
||||||
return (queryParam != null && !queryParam.isBlank())
|
return (queryParam != null && !queryParam.isBlank())
|
||||||
? bookQueryService.searchBooksByMetadataInLibrariesPage(queryParam, libraryIds, page, size)
|
? bookQueryService.searchBooksByMetadataInLibrariesPage(queryParam, libraryIds, page, size)
|
||||||
: bookQueryService.getAllBooksByLibraryIdsPage(libraryIds, true, page, size);
|
: bookQueryService.getAllBooksByLibraryIdsPage(libraryIds, true, page, size, opdsUserV2.getUserId());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -192,12 +192,12 @@ class OpdsServiceTest {
|
|||||||
|
|
||||||
when(request.getHeader("Accept")).thenReturn(null); // v1 default; feed version not relevant for allowed-books logic
|
when(request.getHeader("Accept")).thenReturn(null); // v1 default; feed version not relevant for allowed-books logic
|
||||||
when(request.getRequestURI()).thenReturn("/opds/catalog");
|
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);
|
String feed = service.generateCatalogFeed(request);
|
||||||
|
|
||||||
assertNotNull(feed);
|
assertNotNull(feed);
|
||||||
verify(bookQueryService).getAllBooksByLibraryIds(Set.of(7L), true);
|
verify(bookQueryService).getAllBooksByLibraryIds(Set.of(7L), true, 21L);
|
||||||
verify(userRepository).findById(21L);
|
verify(userRepository).findById(21L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user