Automatically add newly added books to Kobo shelf (#1826)
This commit is contained in:
+6
-19
@@ -3,7 +3,6 @@ package com.adityachandel.booklore.controller;
|
||||
import com.adityachandel.booklore.model.dto.KoboSyncSettings;
|
||||
import com.adityachandel.booklore.service.kobo.KoboSettingsService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -31,31 +30,19 @@ public class KoboSettingsController {
|
||||
|
||||
@Operation(summary = "Create or update Kobo token", description = "Create or update the Kobo sync token for the current user. Requires sync permission or admin.")
|
||||
@ApiResponse(responseCode = "200", description = "Token created/updated successfully")
|
||||
@PutMapping
|
||||
@PutMapping("/token")
|
||||
@PreAuthorize("@securityUtil.canSyncKobo() or @securityUtil.isAdmin()")
|
||||
public ResponseEntity<KoboSyncSettings> createOrUpdateToken() {
|
||||
KoboSyncSettings updated = koboService.createOrUpdateToken();
|
||||
return ResponseEntity.ok(updated);
|
||||
}
|
||||
|
||||
@Operation(summary = "Toggle Kobo sync", description = "Enable or disable Kobo sync for the current user. Requires sync permission or admin.")
|
||||
@ApiResponse(responseCode = "204", description = "Sync toggled successfully")
|
||||
@PutMapping("/sync")
|
||||
@Operation(summary = "Update Kobo settings", description = "Update Kobo sync settings for the current user. Requires sync permission or admin.")
|
||||
@ApiResponse(responseCode = "200", description = "Settings updated successfully")
|
||||
@PutMapping
|
||||
@PreAuthorize("@securityUtil.canSyncKobo() or @securityUtil.isAdmin()")
|
||||
public ResponseEntity<Void> toggleSync(
|
||||
@Parameter(description = "Enable or disable sync") @RequestParam boolean enabled) {
|
||||
koboService.setSyncEnabled(enabled);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@Operation(summary = "Update progress thresholds", description = "Update the progress thresholds for marking books as reading or finished. Requires sync permission or admin.")
|
||||
@ApiResponse(responseCode = "200", description = "Thresholds updated successfully")
|
||||
@PutMapping("/progress-thresholds")
|
||||
@PreAuthorize("@securityUtil.canSyncKobo() or @securityUtil.isAdmin()")
|
||||
public ResponseEntity<KoboSyncSettings> updateProgressThresholds(
|
||||
@Parameter(description = "Progress percentage to mark as reading (0-100)") @RequestParam(required = false) Float readingThreshold,
|
||||
@Parameter(description = "Progress percentage to mark as finished (0-100)") @RequestParam(required = false) Float finishedThreshold) {
|
||||
KoboSyncSettings updated = koboService.updateProgressThresholds(readingThreshold, finishedThreshold);
|
||||
public ResponseEntity<KoboSyncSettings> updateSettings(@RequestBody KoboSyncSettings settings) {
|
||||
KoboSyncSettings updated = koboService.updateSettings(settings);
|
||||
return ResponseEntity.ok(updated);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,4 +11,5 @@ public class KoboSyncSettings {
|
||||
private boolean syncEnabled;
|
||||
private Float progressMarkAsReadingThreshold;
|
||||
private Float progressMarkAsFinishedThreshold;
|
||||
private boolean autoAddToShelf;
|
||||
}
|
||||
|
||||
+4
@@ -33,4 +33,8 @@ public class KoboUserSettingsEntity {
|
||||
@Column(name = "progress_mark_as_finished_threshold")
|
||||
@Builder.Default
|
||||
private Float progressMarkAsFinishedThreshold = 99f;
|
||||
|
||||
@Column(name = "auto_add_to_shelf")
|
||||
@Builder.Default
|
||||
private boolean autoAddToShelf = false;
|
||||
}
|
||||
|
||||
+3
@@ -4,6 +4,7 @@ import com.adityachandel.booklore.model.entity.KoboUserSettingsEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
@@ -12,4 +13,6 @@ public interface KoboUserSettingsRepository extends JpaRepository<KoboUserSettin
|
||||
Optional<KoboUserSettingsEntity> findByUserId(Long userId);
|
||||
|
||||
Optional<KoboUserSettingsEntity> findByToken(String token);
|
||||
|
||||
List<KoboUserSettingsEntity> findByAutoAddToShelfTrueAndSyncEnabledTrue();
|
||||
}
|
||||
@@ -18,4 +18,6 @@ public interface ShelfRepository extends JpaRepository<ShelfEntity, Long> {
|
||||
List<ShelfEntity> findByUserId(Long id);
|
||||
|
||||
Optional<ShelfEntity> findByUserIdAndName(Long id, String name);
|
||||
|
||||
List<ShelfEntity> findByUserIdInAndName(List<Long> userIds, String name);
|
||||
}
|
||||
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
package com.adityachandel.booklore.service.kobo;
|
||||
|
||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||
import com.adityachandel.booklore.model.entity.KoboUserSettingsEntity;
|
||||
import com.adityachandel.booklore.model.entity.ShelfEntity;
|
||||
import com.adityachandel.booklore.model.enums.ShelfType;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.repository.KoboUserSettingsRepository;
|
||||
import com.adityachandel.booklore.repository.ShelfRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class KoboAutoShelfService {
|
||||
|
||||
private final KoboUserSettingsRepository koboUserSettingsRepository;
|
||||
private final ShelfRepository shelfRepository;
|
||||
private final BookRepository bookRepository;
|
||||
private final KoboCompatibilityService koboCompatibilityService;
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void autoAddBookToKoboShelves(Long bookId) {
|
||||
if (bookId == null) {
|
||||
log.warn("Book ID is null for auto-add to Kobo shelf");
|
||||
return;
|
||||
}
|
||||
|
||||
BookEntity book = bookRepository.findById(bookId).orElse(null);
|
||||
if (!isBookEligible(book)) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<KoboUserSettingsEntity> eligibleUsers = koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue();
|
||||
|
||||
if (eligibleUsers.isEmpty()) {
|
||||
log.debug("No Kobo auto-add enabled users for book {}", book.getId());
|
||||
return;
|
||||
}
|
||||
|
||||
List<Long> userIds = eligibleUsers.stream()
|
||||
.map(KoboUserSettingsEntity::getUserId)
|
||||
.toList();
|
||||
|
||||
List<ShelfEntity> shelves = shelfRepository.findByUserIdInAndName(userIds, ShelfType.KOBO.getName());
|
||||
|
||||
Map<Long, ShelfEntity> shelfByUser = shelves
|
||||
.stream()
|
||||
.collect(Collectors.toMap(s -> s.getUser().getId(), s -> s));
|
||||
|
||||
boolean modified = false;
|
||||
|
||||
for (KoboUserSettingsEntity setting : eligibleUsers) {
|
||||
ShelfEntity shelf = shelfByUser.get(setting.getUserId());
|
||||
|
||||
if (shelf == null) {
|
||||
log.debug("User {} has auto-add enabled but no Kobo shelf exists", setting.getUserId());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (book.getShelves().contains(shelf)) {
|
||||
log.debug("Book {} already on Kobo shelf for user {}", book.getId(), setting.getUserId());
|
||||
continue;
|
||||
}
|
||||
|
||||
book.getShelves().add(shelf);
|
||||
modified = true;
|
||||
log.info("Auto-added book {} to Kobo shelf for user {}", book.getId(), setting.getUserId());
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
bookRepository.save(book);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isBookEligible(BookEntity book) {
|
||||
if (book == null) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
+24
-24
@@ -7,6 +7,7 @@ import com.adityachandel.booklore.model.dto.Shelf;
|
||||
import com.adityachandel.booklore.model.dto.request.ShelfCreateRequest;
|
||||
import com.adityachandel.booklore.model.entity.KoboUserSettingsEntity;
|
||||
import com.adityachandel.booklore.model.entity.ShelfEntity;
|
||||
import com.adityachandel.booklore.model.enums.IconType;
|
||||
import com.adityachandel.booklore.model.enums.ShelfType;
|
||||
import com.adityachandel.booklore.repository.KoboUserSettingsRepository;
|
||||
import com.adityachandel.booklore.service.ShelfService;
|
||||
@@ -56,34 +57,31 @@ public class KoboSettingsService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void setSyncEnabled(boolean enabled) {
|
||||
public KoboSyncSettings updateSettings(KoboSyncSettings settings) {
|
||||
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||
KoboUserSettingsEntity entity = repository.findByUserId(user.getId()).orElseThrow(() -> new IllegalStateException("Kobo settings not found for user"));
|
||||
Shelf userKoboShelf = shelfService.getUserKoboShelf();
|
||||
if (!enabled) {
|
||||
if (userKoboShelf != null) {
|
||||
shelfService.deleteShelf(userKoboShelf.getId());
|
||||
}
|
||||
} else {
|
||||
ensureKoboShelfExists(user.getId());
|
||||
}
|
||||
entity.setSyncEnabled(enabled);
|
||||
repository.save(entity);
|
||||
}
|
||||
KoboUserSettingsEntity entity = repository.findByUserId(user.getId()).orElseGet(() -> initDefaultSettings(user.getId()));
|
||||
|
||||
@Transactional
|
||||
public KoboSyncSettings updateProgressThresholds(Float readingThreshold, Float finishedThreshold) {
|
||||
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||
KoboUserSettingsEntity entity = repository.findByUserId(user.getId())
|
||||
.orElseGet(() -> initDefaultSettings(user.getId()));
|
||||
|
||||
if (readingThreshold != null) {
|
||||
entity.setProgressMarkAsReadingThreshold(readingThreshold);
|
||||
if (settings.isSyncEnabled() != entity.isSyncEnabled()) {
|
||||
Shelf userKoboShelf = shelfService.getUserKoboShelf();
|
||||
if (!settings.isSyncEnabled()) {
|
||||
if (userKoboShelf != null) {
|
||||
shelfService.deleteShelf(userKoboShelf.getId());
|
||||
}
|
||||
} else {
|
||||
ensureKoboShelfExists(user.getId());
|
||||
}
|
||||
entity.setSyncEnabled(settings.isSyncEnabled());
|
||||
}
|
||||
if (finishedThreshold != null) {
|
||||
entity.setProgressMarkAsFinishedThreshold(finishedThreshold);
|
||||
|
||||
if (settings.getProgressMarkAsReadingThreshold() != null) {
|
||||
entity.setProgressMarkAsReadingThreshold(settings.getProgressMarkAsReadingThreshold());
|
||||
}
|
||||
|
||||
if (settings.getProgressMarkAsFinishedThreshold() != null) {
|
||||
entity.setProgressMarkAsFinishedThreshold(settings.getProgressMarkAsFinishedThreshold());
|
||||
}
|
||||
|
||||
entity.setAutoAddToShelf(settings.isAutoAddToShelf());
|
||||
|
||||
repository.save(entity);
|
||||
return mapToDto(entity);
|
||||
}
|
||||
@@ -105,6 +103,7 @@ public class KoboSettingsService {
|
||||
ShelfCreateRequest.builder()
|
||||
.name(ShelfType.KOBO.getName())
|
||||
.icon(ShelfType.KOBO.getIcon())
|
||||
.iconType(IconType.PRIME_NG)
|
||||
.build()
|
||||
);
|
||||
}
|
||||
@@ -122,6 +121,7 @@ public class KoboSettingsService {
|
||||
dto.setSyncEnabled(entity.isSyncEnabled());
|
||||
dto.setProgressMarkAsReadingThreshold(entity.getProgressMarkAsReadingThreshold());
|
||||
dto.setProgressMarkAsFinishedThreshold(entity.getProgressMarkAsFinishedThreshold());
|
||||
dto.setAutoAddToShelf(entity.isAutoAddToShelf());
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
|
||||
+3
@@ -8,6 +8,7 @@ import com.adityachandel.booklore.model.enums.LibraryScanMode;
|
||||
import com.adityachandel.booklore.service.event.BookEventBroadcaster;
|
||||
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessor;
|
||||
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessorRegistry;
|
||||
import com.adityachandel.booklore.service.kobo.KoboAutoShelfService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -22,6 +23,7 @@ public class FileAsBookProcessor implements LibraryFileProcessor {
|
||||
|
||||
private final BookEventBroadcaster bookEventBroadcaster;
|
||||
private final BookFileProcessorRegistry processorRegistry;
|
||||
private final KoboAutoShelfService koboAutoShelfService;
|
||||
|
||||
@Override
|
||||
public LibraryScanMode getScanMode() {
|
||||
@@ -43,6 +45,7 @@ public class FileAsBookProcessor implements LibraryFileProcessor {
|
||||
FileProcessResult result = processLibraryFile(libraryFile);
|
||||
if (result != null) {
|
||||
bookEventBroadcaster.broadcastBookAddEvent(result.getBook());
|
||||
koboAutoShelfService.autoAddBookToKoboShelves(result.getBook().getId());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to process file '{}': {}", libraryFile.getFileName(), e.getMessage());
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE kobo_user_settings
|
||||
ADD COLUMN IF NOT EXISTS auto_add_to_shelf BOOLEAN NOT NULL DEFAULT FALSE;
|
||||
Reference in New Issue
Block a user