Fix/hardcover api key per user (#1943)

* feat: add hardcover API key and sync settings to KoboSync and KoboUserSettings entities

* feat(hardcover): enhance sync functionality to use per-user API keys

- Updated HardcoverSyncService to utilize user-specific Hardcover API keys for syncing reading progress.
- Modified syncProgressToHardcover method to accept userId, allowing for personalized sync settings.
- Improved logging to include userId in sync operations for better traceability.
- Adjusted KoboReadingStateService to pass userId when triggering sync to Hardcover.

* feat(database): update kobo_location_source column size and add hardcover settings

- Increased the size of the kobo_location_source column to accommodate longer location strings from Kobo devices.
- Added new columns for hardcover API key and sync settings in the kobo_user_settings table to enhance user customization.

* refactor(hardcover): update tests to use user-specific Kobo settings

- Replaced AppSettingService with KoboSettingsService in HardcoverSyncServiceTest to utilize user-specific settings.
- Modified syncProgressToHardcover method calls to include userId for personalized sync operations.
- Added a new test case to handle scenarios where user settings are not found, ensuring robust error handling.

---------

Co-authored-by: akiraslingshot <akiraslingshot@gmail.com>
This commit is contained in:
Giancarlo Perrone
2025-12-19 18:56:41 -07:00
committed by GitHub
co-authored by akiraslingshot
parent 54108754f9
commit 32a1a2ac34
11 changed files with 271 additions and 117 deletions
@@ -12,4 +12,6 @@ public class KoboSyncSettings {
private Float progressMarkAsReadingThreshold;
private Float progressMarkAsFinishedThreshold;
private boolean autoAddToShelf;
private String hardcoverApiKey;
private boolean hardcoverSyncEnabled;
}
@@ -37,4 +37,11 @@ public class KoboUserSettingsEntity {
@Column(name = "auto_add_to_shelf")
@Builder.Default
private boolean autoAddToShelf = false;
@Column(name = "hardcover_api_key", length = 2048)
private String hardcoverApiKey;
@Column(name = "hardcover_sync_enabled")
@Builder.Default
private boolean hardcoverSyncEnabled = false;
}
@@ -69,7 +69,7 @@ public class UserBookProgressEntity {
@Column(name = "kobo_location_type", length = 50)
private String koboLocationType;
@Column(name = "kobo_location_source", length = 50)
@Column(name = "kobo_location_source", length = 512)
private String koboLocationSource;
@Enumerated(EnumType.STRING)
@@ -1,13 +1,14 @@
package com.adityachandel.booklore.service.hardcover;
import com.adityachandel.booklore.model.dto.settings.MetadataProviderSettings;
import com.adityachandel.booklore.model.dto.KoboSyncSettings;
import com.adityachandel.booklore.model.entity.BookEntity;
import com.adityachandel.booklore.model.entity.BookMetadataEntity;
import com.adityachandel.booklore.repository.BookRepository;
import com.adityachandel.booklore.service.appsettings.AppSettingService;
import com.adityachandel.booklore.service.kobo.KoboSettingsService;
import com.adityachandel.booklore.service.metadata.parser.hardcover.GraphQLRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Async;
@@ -23,8 +24,8 @@ import java.util.Map;
/**
* Service to sync reading progress to Hardcover.
* Uses the global Hardcover API token from Metadata Provider Settings.
* Sync only activates if the token is configured and Hardcover is enabled.
* Uses per-user Hardcover API tokens for reading progress sync.
* Each user can configure their own Hardcover API key in Kobo settings.
*/
@Slf4j
@Service
@@ -35,12 +36,15 @@ public class HardcoverSyncService {
private static final int STATUS_READ = 3;
private final RestClient restClient;
private final AppSettingService appSettingService;
private final KoboSettingsService koboSettingsService;
private final BookRepository bookRepository;
// Thread-local to hold the current API token for GraphQL requests
private final ThreadLocal<String> currentApiToken = new ThreadLocal<>();
@Autowired
public HardcoverSyncService(AppSettingService appSettingService, BookRepository bookRepository) {
this.appSettingService = appSettingService;
public HardcoverSyncService(@Lazy KoboSettingsService koboSettingsService, BookRepository bookRepository) {
this.koboSettingsService = koboSettingsService;
this.bookRepository = bookRepository;
this.restClient = RestClient.builder()
.baseUrl(HARDCOVER_API_URL)
@@ -50,105 +54,116 @@ public class HardcoverSyncService {
/**
* Asynchronously sync Kobo reading progress to Hardcover.
* This method is non-blocking and will not fail the calling process if sync fails.
* Uses the user's personal Hardcover API key if configured.
*
* @param bookId The book ID to sync progress for
* @param progressPercent The reading progress as a percentage (0-100)
* @param userId The user ID whose reading progress is being synced
*/
@Async
@Transactional(readOnly = true)
public void syncProgressToHardcover(Long bookId, Float progressPercent) {
public void syncProgressToHardcover(Long bookId, Float progressPercent, Long userId) {
try {
if (!isHardcoverSyncEnabled()) {
log.trace("Hardcover sync skipped: not enabled or no API token configured");
// Get user's Hardcover settings
KoboSyncSettings userSettings = koboSettingsService.getSettingsByUserId(userId);
if (!isHardcoverSyncEnabledForUser(userSettings)) {
log.trace("Hardcover sync skipped for user {}: not enabled or no API token configured", userId);
return;
}
if (progressPercent == null) {
log.debug("Hardcover sync skipped: no progress to sync");
return;
}
// Set the user's API token for this sync operation
currentApiToken.set(userSettings.getHardcoverApiKey());
// Fetch book fresh within the async context to avoid lazy loading issues
BookEntity book = bookRepository.findById(bookId).orElse(null);
if (book == null) {
log.debug("Hardcover sync skipped: book {} not found", bookId);
return;
}
BookMetadataEntity metadata = book.getMetadata();
if (metadata == null) {
log.debug("Hardcover sync skipped: book {} has no metadata", bookId);
return;
}
// Find the book on Hardcover - use stored ID if available
HardcoverBookInfo hardcoverBook;
if (metadata.getHardcoverBookId() != null) {
// Use the stored numeric book ID directly
hardcoverBook = new HardcoverBookInfo();
hardcoverBook.bookId = metadata.getHardcoverBookId();
hardcoverBook.pages = metadata.getPageCount();
log.debug("Using stored Hardcover book ID: {}", hardcoverBook.bookId);
} else {
// Search by ISBN
hardcoverBook = findHardcoverBook(metadata);
if (hardcoverBook == null) {
log.debug("Hardcover sync skipped: book {} not found on Hardcover", bookId);
try {
if (progressPercent == null) {
log.debug("Hardcover sync skipped: no progress to sync");
return;
}
}
// Determine the status based on progress
int statusId = progressPercent >= 99.0f ? STATUS_READ : STATUS_CURRENTLY_READING;
// Fetch book fresh within the async context to avoid lazy loading issues
BookEntity book = bookRepository.findById(bookId).orElse(null);
if (book == null) {
log.debug("Hardcover sync skipped: book {} not found", bookId);
return;
}
// Calculate progress in pages
int progressPages = 0;
if (hardcoverBook.pages != null && hardcoverBook.pages > 0) {
progressPages = Math.round((progressPercent / 100.0f) * hardcoverBook.pages);
progressPages = Math.max(0, Math.min(hardcoverBook.pages, progressPages));
}
log.info("Progress calculation: progressPercent={}%, totalPages={}, progressPages={}",
progressPercent, hardcoverBook.pages, progressPages);
BookMetadataEntity metadata = book.getMetadata();
if (metadata == null) {
log.debug("Hardcover sync skipped: book {} has no metadata", bookId);
return;
}
// Step 1: Add/update the book in user's library
Integer userBookId = insertOrGetUserBook(hardcoverBook.bookId, hardcoverBook.editionId, statusId);
if (userBookId == null) {
log.warn("Hardcover sync failed: could not get user_book_id for book {}", bookId);
return;
}
// Find the book on Hardcover - use stored ID if available
HardcoverBookInfo hardcoverBook;
if (metadata.getHardcoverBookId() != null) {
// Use the stored numeric book ID directly
hardcoverBook = new HardcoverBookInfo();
hardcoverBook.bookId = metadata.getHardcoverBookId();
hardcoverBook.pages = metadata.getPageCount();
log.debug("Using stored Hardcover book ID: {}", hardcoverBook.bookId);
} else {
// Search by ISBN
hardcoverBook = findHardcoverBook(metadata);
if (hardcoverBook == null) {
log.debug("Hardcover sync skipped: book {} not found on Hardcover", bookId);
return;
}
}
// Step 2: Create or update the reading progress
boolean success = upsertReadingProgress(userBookId, hardcoverBook.editionId, progressPages);
if (success) {
log.info("Synced progress to Hardcover: book={}, hardcoverBookId={}, progress={}% ({}pages)",
bookId, hardcoverBook.bookId, Math.round(progressPercent), progressPages);
// Determine the status based on progress
int statusId = progressPercent >= 99.0f ? STATUS_READ : STATUS_CURRENTLY_READING;
// Calculate progress in pages
int progressPages = 0;
if (hardcoverBook.pages != null && hardcoverBook.pages > 0) {
progressPages = Math.round((progressPercent / 100.0f) * hardcoverBook.pages);
progressPages = Math.max(0, Math.min(hardcoverBook.pages, progressPages));
}
log.info("Progress calculation: userId={}, progressPercent={}%, totalPages={}, progressPages={}",
userId, progressPercent, hardcoverBook.pages, progressPages);
// Step 1: Add/update the book in user's library
Integer userBookId = insertOrGetUserBook(hardcoverBook.bookId, hardcoverBook.editionId, statusId);
if (userBookId == null) {
log.warn("Hardcover sync failed: could not get user_book_id for book {}", bookId);
return;
}
// Step 2: Create or update the reading progress
boolean success = upsertReadingProgress(userBookId, hardcoverBook.editionId, progressPages);
if (success) {
log.info("Synced progress to Hardcover: userId={}, book={}, hardcoverBookId={}, progress={}% ({}pages)",
userId, bookId, hardcoverBook.bookId, Math.round(progressPercent), progressPages);
}
} finally {
// Clean up thread-local
currentApiToken.remove();
}
} catch (Exception e) {
log.error("Failed to sync progress to Hardcover for book {}: {}",
bookId, e.getMessage());
log.error("Failed to sync progress to Hardcover for book {} (user {}): {}",
bookId, userId, e.getMessage());
}
}
private boolean isHardcoverSyncEnabled() {
MetadataProviderSettings.Hardcover hardcoverSettings =
appSettingService.getAppSettings().getMetadataProviderSettings().getHardcover();
if (hardcoverSettings == null) {
/**
* Check if Hardcover sync is enabled for a specific user.
*/
private boolean isHardcoverSyncEnabledForUser(KoboSyncSettings userSettings) {
if (userSettings == null) {
return false;
}
return hardcoverSettings.isEnabled()
&& hardcoverSettings.getApiKey() != null
&& !hardcoverSettings.getApiKey().isBlank();
return userSettings.isHardcoverSyncEnabled()
&& userSettings.getHardcoverApiKey() != null
&& !userSettings.getHardcoverApiKey().isBlank();
}
private String getApiToken() {
return appSettingService.getAppSettings()
.getMetadataProviderSettings()
.getHardcover()
.getApiKey();
return currentApiToken.get();
}
/**
@@ -154,6 +154,9 @@ public class KoboReadingStateService {
KoboReadingState.CurrentBookmark.Location location = bookmark.getLocation();
if (location != null) {
log.debug("Kobo location data: value={}, type={}, source={} (length={})",
location.getValue(), location.getType(), location.getSource(),
location.getSource() != null ? location.getSource().length() : 0);
progress.setKoboLocation(location.getValue());
progress.setKoboLocationType(location.getType());
progress.setKoboLocationSource(location.getSource());
@@ -171,8 +174,8 @@ public class KoboReadingStateService {
progressRepository.save(progress);
log.debug("Synced Kobo progress: bookId={}, progress={}%", bookId, progress.getKoboProgressPercent());
// Sync progress to Hardcover asynchronously (if enabled)
hardcoverSyncService.syncProgressToHardcover(book.getId(), progress.getKoboProgressPercent());
// Sync progress to Hardcover asynchronously (if enabled for this user)
hardcoverSyncService.syncProgressToHardcover(book.getId(), progress.getKoboProgressPercent(), userId);
} catch (NumberFormatException e) {
log.warn("Invalid entitlement ID format: {}", readingState.getEntitlementId());
}
@@ -82,6 +82,10 @@ public class KoboSettingsService {
entity.setAutoAddToShelf(settings.isAutoAddToShelf());
// Update Hardcover settings
entity.setHardcoverApiKey(settings.getHardcoverApiKey());
entity.setHardcoverSyncEnabled(settings.isHardcoverSyncEnabled());
repository.save(entity);
return mapToDto(entity);
}
@@ -122,6 +126,19 @@ public class KoboSettingsService {
dto.setProgressMarkAsReadingThreshold(entity.getProgressMarkAsReadingThreshold());
dto.setProgressMarkAsFinishedThreshold(entity.getProgressMarkAsFinishedThreshold());
dto.setAutoAddToShelf(entity.isAutoAddToShelf());
dto.setHardcoverApiKey(entity.getHardcoverApiKey());
dto.setHardcoverSyncEnabled(entity.isHardcoverSyncEnabled());
return dto;
}
/**
* Get Hardcover settings for a specific user by ID.
* Used by HardcoverSyncService to get user-specific API key.
*/
@Transactional(readOnly = true)
public KoboSyncSettings getSettingsByUserId(Long userId) {
return repository.findByUserId(userId)
.map(this::mapToDto)
.orElse(null);
}
}
@@ -0,0 +1,6 @@
-- Add per-user Hardcover settings to kobo_user_settings table
ALTER TABLE kobo_user_settings ADD COLUMN hardcover_api_key VARCHAR(2048);
ALTER TABLE kobo_user_settings ADD COLUMN hardcover_sync_enabled BOOLEAN DEFAULT FALSE;
-- Fix kobo_location_source column size (Kobo devices can send longer location strings)
ALTER TABLE user_book_progress MODIFY COLUMN kobo_location_source VARCHAR(512);