fix(kobo): limit kobo shelf additions to permissible books (#1423)
This commit is contained in:
@@ -3,12 +3,15 @@ package org.booklore.service.kobo;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.booklore.model.entity.BookEntity;
|
import org.booklore.model.entity.BookEntity;
|
||||||
|
import org.booklore.model.entity.BookLoreUserEntity;
|
||||||
import org.booklore.model.entity.KoboUserSettingsEntity;
|
import org.booklore.model.entity.KoboUserSettingsEntity;
|
||||||
import org.booklore.model.entity.ShelfEntity;
|
import org.booklore.model.entity.ShelfEntity;
|
||||||
|
import org.booklore.model.entity.UserPermissionsEntity;
|
||||||
import org.booklore.model.enums.ShelfType;
|
import org.booklore.model.enums.ShelfType;
|
||||||
import org.booklore.repository.BookRepository;
|
import org.booklore.repository.BookRepository;
|
||||||
import org.booklore.repository.KoboUserSettingsRepository;
|
import org.booklore.repository.KoboUserSettingsRepository;
|
||||||
import org.booklore.repository.ShelfRepository;
|
import org.booklore.repository.ShelfRepository;
|
||||||
|
import org.booklore.repository.UserRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Propagation;
|
import org.springframework.transaction.annotation.Propagation;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
@@ -25,6 +28,7 @@ public class KoboAutoShelfService {
|
|||||||
|
|
||||||
private final KoboUserSettingsRepository koboUserSettingsRepository;
|
private final KoboUserSettingsRepository koboUserSettingsRepository;
|
||||||
private final ShelfRepository shelfRepository;
|
private final ShelfRepository shelfRepository;
|
||||||
|
private final UserRepository userRepository;
|
||||||
private final BookRepository bookRepository;
|
private final BookRepository bookRepository;
|
||||||
private final KoboCompatibilityService koboCompatibilityService;
|
private final KoboCompatibilityService koboCompatibilityService;
|
||||||
|
|
||||||
@@ -36,7 +40,13 @@ public class KoboAutoShelfService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
BookEntity book = bookRepository.findByIdWithBookFiles(bookId).orElse(null);
|
BookEntity book = bookRepository.findByIdWithBookFiles(bookId).orElse(null);
|
||||||
|
if (book == null) {
|
||||||
|
log.warn("Book {} not found for Kobo auto-add", bookId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!isBookEligible(book)) {
|
if (!isBookEligible(book)) {
|
||||||
|
log.debug("Book {} is not eligible for Kobo auto-add", bookId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,6 +61,10 @@ public class KoboAutoShelfService {
|
|||||||
.map(KoboUserSettingsEntity::getUserId)
|
.map(KoboUserSettingsEntity::getUserId)
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
|
Map<Long, BookLoreUserEntity> users = userRepository.findAllById(userIds)
|
||||||
|
.stream()
|
||||||
|
.collect(Collectors.toMap(BookLoreUserEntity::getId, u -> u));
|
||||||
|
|
||||||
List<ShelfEntity> shelves = shelfRepository.findByUserIdInAndName(userIds, ShelfType.KOBO.getName());
|
List<ShelfEntity> shelves = shelfRepository.findByUserIdInAndName(userIds, ShelfType.KOBO.getName());
|
||||||
|
|
||||||
Map<Long, ShelfEntity> shelfByUser = shelves
|
Map<Long, ShelfEntity> shelfByUser = shelves
|
||||||
@@ -67,6 +81,11 @@ public class KoboAutoShelfService {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!hasBookLibraryAccess(users.get(setting.getUserId()), book)) {
|
||||||
|
log.debug("Book {} is not available for user {}", book.getId(), setting.getUserId());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (book.getShelves() == null) {
|
if (book.getShelves() == null) {
|
||||||
book.setShelves(new HashSet<>());
|
book.setShelves(new HashSet<>());
|
||||||
}
|
}
|
||||||
@@ -86,17 +105,22 @@ public class KoboAutoShelfService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean hasBookLibraryAccess(BookLoreUserEntity user, BookEntity bookEntity) {
|
||||||
|
if (user == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
UserPermissionsEntity permissions = user.getPermissions();
|
||||||
|
if (permissions != null && permissions.isPermissionAdmin()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return user.getLibraries()
|
||||||
|
.stream()
|
||||||
|
.anyMatch(library -> library.equals(bookEntity.getLibrary()));
|
||||||
|
}
|
||||||
|
|
||||||
private boolean isBookEligible(BookEntity book) {
|
private boolean isBookEligible(BookEntity book) {
|
||||||
if (book == null) {
|
return koboCompatibilityService.isBookSupportedForKobo(book);
|
||||||
log.warn("Book not found for Kobo auto-add");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!koboCompatibilityService.isBookSupportedForKobo(book)) {
|
|
||||||
log.debug("Book {} is not Kobo-compatible, skipping", book.getId());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
package org.booklore.service.kobo;
|
package org.booklore.service.kobo;
|
||||||
|
|
||||||
import org.booklore.model.entity.BookEntity;
|
import org.booklore.model.entity.*;
|
||||||
import org.booklore.model.entity.BookLoreUserEntity;
|
|
||||||
import org.booklore.model.entity.KoboUserSettingsEntity;
|
|
||||||
import org.booklore.model.entity.ShelfEntity;
|
|
||||||
import org.booklore.model.enums.ShelfType;
|
import org.booklore.model.enums.ShelfType;
|
||||||
import org.booklore.repository.BookRepository;
|
import org.booklore.repository.BookRepository;
|
||||||
import org.booklore.repository.KoboUserSettingsRepository;
|
import org.booklore.repository.KoboUserSettingsRepository;
|
||||||
import org.booklore.repository.ShelfRepository;
|
import org.booklore.repository.ShelfRepository;
|
||||||
|
import org.booklore.repository.UserRepository;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
@@ -18,6 +16,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
@@ -34,6 +33,9 @@ class KoboAutoShelfServiceTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private BookRepository bookRepository;
|
private BookRepository bookRepository;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private KoboCompatibilityService koboCompatibilityService;
|
private KoboCompatibilityService koboCompatibilityService;
|
||||||
|
|
||||||
@@ -43,6 +45,8 @@ class KoboAutoShelfServiceTest {
|
|||||||
private BookEntity testBook;
|
private BookEntity testBook;
|
||||||
private BookLoreUserEntity testUser1;
|
private BookLoreUserEntity testUser1;
|
||||||
private BookLoreUserEntity testUser2;
|
private BookLoreUserEntity testUser2;
|
||||||
|
private LibraryEntity library1;
|
||||||
|
private LibraryEntity library2;
|
||||||
private ShelfEntity koboShelf1;
|
private ShelfEntity koboShelf1;
|
||||||
private ShelfEntity koboShelf2;
|
private ShelfEntity koboShelf2;
|
||||||
private KoboUserSettingsEntity settings1;
|
private KoboUserSettingsEntity settings1;
|
||||||
@@ -50,17 +54,30 @@ class KoboAutoShelfServiceTest {
|
|||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
|
library1 = LibraryEntity.builder()
|
||||||
|
.id(1L)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
library2 = LibraryEntity.builder()
|
||||||
|
.id(2L)
|
||||||
|
.build();
|
||||||
|
|
||||||
testBook = BookEntity.builder()
|
testBook = BookEntity.builder()
|
||||||
.id(1L)
|
.id(1L)
|
||||||
|
.library(library1)
|
||||||
.shelves(new HashSet<>())
|
.shelves(new HashSet<>())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
testUser1 = BookLoreUserEntity.builder()
|
testUser1 = BookLoreUserEntity.builder()
|
||||||
.id(100L)
|
.id(100L)
|
||||||
|
.libraries(Set.of(library1))
|
||||||
|
.permissions(UserPermissionsEntity.builder().build())
|
||||||
.isDefaultPassword(false).build();
|
.isDefaultPassword(false).build();
|
||||||
|
|
||||||
testUser2 = BookLoreUserEntity.builder()
|
testUser2 = BookLoreUserEntity.builder()
|
||||||
.id(200L)
|
.id(200L)
|
||||||
|
.libraries(Set.of(library1, library2))
|
||||||
|
.permissions(UserPermissionsEntity.builder().build())
|
||||||
.isDefaultPassword(false).build();
|
.isDefaultPassword(false).build();
|
||||||
|
|
||||||
koboShelf1 = ShelfEntity.builder()
|
koboShelf1 = ShelfEntity.builder()
|
||||||
@@ -142,6 +159,7 @@ class KoboAutoShelfServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void autoAddBookToKoboShelves_successfully_shouldAddBookToShelves() {
|
void autoAddBookToKoboShelves_successfully_shouldAddBookToShelves() {
|
||||||
|
when(userRepository.findAllById(List.of(100L, 200L))).thenReturn(List.of(testUser1, testUser2));
|
||||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(testBook));
|
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(testBook));
|
||||||
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
||||||
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
||||||
@@ -164,6 +182,7 @@ class KoboAutoShelfServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void autoAddBookToKoboShelves_withOneUserMissingShelf_shouldAddOnlyToExistingShelves() {
|
void autoAddBookToKoboShelves_withOneUserMissingShelf_shouldAddOnlyToExistingShelves() {
|
||||||
|
when(userRepository.findAllById(List.of(100L, 200L))).thenReturn(List.of(testUser1, testUser2));
|
||||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(testBook));
|
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(testBook));
|
||||||
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
||||||
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
||||||
@@ -183,6 +202,7 @@ class KoboAutoShelfServiceTest {
|
|||||||
void autoAddBookToKoboShelves_withBookAlreadyOnShelf_shouldNotDuplicate() {
|
void autoAddBookToKoboShelves_withBookAlreadyOnShelf_shouldNotDuplicate() {
|
||||||
testBook.getShelves().add(koboShelf1);
|
testBook.getShelves().add(koboShelf1);
|
||||||
|
|
||||||
|
when(userRepository.findAllById(List.of(100L, 200L))).thenReturn(List.of(testUser1, testUser2));
|
||||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(testBook));
|
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(testBook));
|
||||||
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
||||||
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
||||||
@@ -207,6 +227,7 @@ class KoboAutoShelfServiceTest {
|
|||||||
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
||||||
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
||||||
.thenReturn(List.of(settings1, settings2));
|
.thenReturn(List.of(settings1, settings2));
|
||||||
|
when(userRepository.findAllById(List.of(100L, 200L))).thenReturn(List.of(testUser1, testUser2));
|
||||||
when(shelfRepository.findByUserIdInAndName(List.of(100L, 200L), ShelfType.KOBO.getName()))
|
when(shelfRepository.findByUserIdInAndName(List.of(100L, 200L), ShelfType.KOBO.getName()))
|
||||||
.thenReturn(List.of(koboShelf1, koboShelf2));
|
.thenReturn(List.of(koboShelf1, koboShelf2));
|
||||||
|
|
||||||
@@ -234,6 +255,7 @@ class KoboAutoShelfServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void autoAddBookToKoboShelves_withSingleUser_shouldAddToSingleShelf() {
|
void autoAddBookToKoboShelves_withSingleUser_shouldAddToSingleShelf() {
|
||||||
|
when(userRepository.findAllById(List.of(100L))).thenReturn(List.of(testUser1));
|
||||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(testBook));
|
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(testBook));
|
||||||
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
||||||
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
||||||
@@ -252,9 +274,11 @@ class KoboAutoShelfServiceTest {
|
|||||||
void autoAddBookToKoboShelves_withNullShelves_shouldInitializeAndAdd() {
|
void autoAddBookToKoboShelves_withNullShelves_shouldInitializeAndAdd() {
|
||||||
testBook = BookEntity.builder()
|
testBook = BookEntity.builder()
|
||||||
.id(1L)
|
.id(1L)
|
||||||
|
.library(library1)
|
||||||
.shelves(null)
|
.shelves(null)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
when(userRepository.findAllById(List.of(100L))).thenReturn(List.of(testUser1));
|
||||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(testBook));
|
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(testBook));
|
||||||
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
||||||
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
||||||
@@ -269,4 +293,48 @@ class KoboAutoShelfServiceTest {
|
|||||||
assert testBook.getShelves().contains(koboShelf1);
|
assert testBook.getShelves().contains(koboShelf1);
|
||||||
assert testBook.getShelves().size() == 1;
|
assert testBook.getShelves().size() == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void autoAddBookToKoboShelves_shouldRespectLibraryAssignment() {
|
||||||
|
testBook = BookEntity.builder()
|
||||||
|
.id(1L)
|
||||||
|
.library(library2)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
when(userRepository.findAllById(List.of(100L))).thenReturn(List.of(testUser1, testUser2));
|
||||||
|
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(testBook));
|
||||||
|
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
||||||
|
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
||||||
|
.thenReturn(List.of(settings1));
|
||||||
|
when(shelfRepository.findByUserIdInAndName(List.of(100L), ShelfType.KOBO.getName()))
|
||||||
|
.thenReturn(List.of(koboShelf1));
|
||||||
|
|
||||||
|
koboAutoShelfService.autoAddBookToKoboShelves(1L);
|
||||||
|
|
||||||
|
verify(bookRepository, never()).save(testBook);
|
||||||
|
assert testBook.getShelves() != null;
|
||||||
|
assert testBook.getShelves().isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void autoAddBookToKoboShelves_shouldHandleMissingUsers() {
|
||||||
|
testBook = BookEntity.builder()
|
||||||
|
.id(1L)
|
||||||
|
.library(library1)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
when(userRepository.findAllById(List.of(100L))).thenReturn(List.of());
|
||||||
|
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(testBook));
|
||||||
|
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
||||||
|
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
||||||
|
.thenReturn(List.of(settings1));
|
||||||
|
when(shelfRepository.findByUserIdInAndName(List.of(100L), ShelfType.KOBO.getName()))
|
||||||
|
.thenReturn(List.of(koboShelf1));
|
||||||
|
|
||||||
|
koboAutoShelfService.autoAddBookToKoboShelves(1L);
|
||||||
|
|
||||||
|
verify(bookRepository, never()).save(testBook);
|
||||||
|
assert testBook.getShelves() != null;
|
||||||
|
assert testBook.getShelves().isEmpty();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user