Feature/koreader hardcover sync (#2204)
* feat(settings): add hardcover sync settings and integrate into device settings UI * feat(hardcover): implement Hardcover sync settings API and UI integration * test(hardcover): add unit tests for HardcoverSyncSettingsService and HardcoverSettingsComponent * fix(hardcover-settings): correct API key copy function to handle null values properly --------- Co-authored-by: akiraslingshot <akiraslingshot@gmail.com> Co-authored-by: ACX <8075870+acx10@users.noreply.github.com>
This commit is contained in:
co-authored by
akiraslingshot
ACX
parent
20ed3e5b25
commit
77bb2904e3
+42
@@ -0,0 +1,42 @@
|
||||
package com.adityachandel.booklore.controller;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.HardcoverSyncSettings;
|
||||
import com.adityachandel.booklore.service.hardcover.HardcoverSyncSettingsService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/hardcover-sync-settings")
|
||||
@RequiredArgsConstructor
|
||||
@Tag(name = "Hardcover Sync Settings", description = "Endpoints for managing Hardcover progress sync settings")
|
||||
public class HardcoverSyncSettingsController {
|
||||
|
||||
private final HardcoverSyncSettingsService hardcoverSyncSettingsService;
|
||||
|
||||
@Operation(summary = "Get Hardcover sync settings", description = "Retrieve the current user's Hardcover sync settings.")
|
||||
@ApiResponse(responseCode = "200", description = "Settings returned successfully")
|
||||
@GetMapping
|
||||
@PreAuthorize("@securityUtil.canSyncKobo() or @securityUtil.canSyncKoReader() or @securityUtil.isAdmin()")
|
||||
public ResponseEntity<HardcoverSyncSettings> getSettings() {
|
||||
HardcoverSyncSettings settings = hardcoverSyncSettingsService.getCurrentUserSettings();
|
||||
return ResponseEntity.ok(settings);
|
||||
}
|
||||
|
||||
@Operation(summary = "Update Hardcover sync settings", description = "Update the current user's Hardcover sync settings.")
|
||||
@ApiResponse(responseCode = "200", description = "Settings updated successfully")
|
||||
@PutMapping
|
||||
@PreAuthorize("@securityUtil.canSyncKobo() or @securityUtil.canSyncKoReader() or @securityUtil.isAdmin()")
|
||||
public ResponseEntity<HardcoverSyncSettings> updateSettings(@RequestBody HardcoverSyncSettings settings) {
|
||||
HardcoverSyncSettings updated = hardcoverSyncSettingsService.updateCurrentUserSettings(settings);
|
||||
return ResponseEntity.ok(updated);
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.adityachandel.booklore.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class HardcoverSyncSettings {
|
||||
private String hardcoverApiKey;
|
||||
private boolean hardcoverSyncEnabled;
|
||||
}
|
||||
+4
-2
@@ -18,7 +18,9 @@ public enum UserSettingKey {
|
||||
FILTER_MODE("filterMode", false),
|
||||
FILTER_SORTING_MODE("filterSortingMode", false),
|
||||
METADATA_CENTER_VIEW_MODE("metadataCenterViewMode", false),
|
||||
ENABLE_SERIES_VIEW("enableSeriesView", false);
|
||||
ENABLE_SERIES_VIEW("enableSeriesView", false),
|
||||
HARDCOVER_API_KEY("hardcoverApiKey", false),
|
||||
HARDCOVER_SYNC_ENABLED("hardcoverSyncEnabled", false);
|
||||
|
||||
|
||||
private final String dbKey;
|
||||
@@ -42,4 +44,4 @@ public enum UserSettingKey {
|
||||
}
|
||||
throw new IllegalArgumentException("Unknown setting key: " + dbKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.adityachandel.booklore.repository;
|
||||
|
||||
import com.adityachandel.booklore.model.entity.UserSettingEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface UserSettingRepository extends JpaRepository<UserSettingEntity, Long> {
|
||||
long countBySettingKeyAndSettingValue(String settingKey, String settingValue);
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import com.adityachandel.booklore.model.dto.InstallationPing;
|
||||
import com.adityachandel.booklore.model.dto.settings.AppSettings;
|
||||
import com.adityachandel.booklore.model.dto.settings.MetadataProviderSettings;
|
||||
import com.adityachandel.booklore.model.dto.settings.MetadataPublicReviewsSettings;
|
||||
import com.adityachandel.booklore.model.dto.settings.UserSettingKey;
|
||||
import com.adityachandel.booklore.model.entity.LibraryEntity;
|
||||
import com.adityachandel.booklore.model.enums.BookFileType;
|
||||
import com.adityachandel.booklore.model.enums.MetadataProvider;
|
||||
@@ -42,6 +43,7 @@ public class TelemetryService {
|
||||
private final EmailRecipientV2Repository emailRecipientV2Repository;
|
||||
private final AppSettingService appSettingService;
|
||||
private final KoboUserSettingsRepository koboUserSettingsRepository;
|
||||
private final UserSettingRepository userSettingRepository;
|
||||
private final KoreaderUserRepository koreaderUserRepository;
|
||||
private final OpdsUserV2Repository opdsUserV2Repository;
|
||||
private final InstallationService installationService;
|
||||
@@ -121,7 +123,8 @@ public class TelemetryService {
|
||||
.koboStatistics(BookloreTelemetry.KoboStatistics.builder()
|
||||
.convertToKepubEnabled(settings.getKoboSettings().isConvertToKepub())
|
||||
.totalKoboUsers((int) koboUserSettingsRepository.count())
|
||||
.totalHardcoverSyncEnabled((int) koboUserSettingsRepository.countByHardcoverSyncEnabledTrue())
|
||||
.totalHardcoverSyncEnabled((int) userSettingRepository.countBySettingKeyAndSettingValue(
|
||||
UserSettingKey.HARDCOVER_SYNC_ENABLED.getDbKey(), "true"))
|
||||
.totalAutoAddToShelf((int) koboUserSettingsRepository.countByAutoAddToShelfTrue())
|
||||
.build())
|
||||
.bookStatistics(bookStatistics)
|
||||
|
||||
+132
-15
@@ -1,14 +1,12 @@
|
||||
package com.adityachandel.booklore.service.hardcover;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.KoboSyncSettings;
|
||||
import com.adityachandel.booklore.model.dto.HardcoverSyncSettings;
|
||||
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.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;
|
||||
@@ -25,7 +23,7 @@ import java.util.Map;
|
||||
/**
|
||||
* Service to sync reading progress to Hardcover.
|
||||
* Uses per-user Hardcover API tokens for reading progress sync.
|
||||
* Each user can configure their own Hardcover API key in Kobo settings.
|
||||
* Each user can configure their own Hardcover API key in their sync settings.
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -36,15 +34,15 @@ public class HardcoverSyncService {
|
||||
private static final int STATUS_READ = 3;
|
||||
|
||||
private final RestClient restClient;
|
||||
private final KoboSettingsService koboSettingsService;
|
||||
private final HardcoverSyncSettingsService hardcoverSyncSettingsService;
|
||||
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(@Lazy KoboSettingsService koboSettingsService, BookRepository bookRepository) {
|
||||
this.koboSettingsService = koboSettingsService;
|
||||
public HardcoverSyncService(HardcoverSyncSettingsService hardcoverSyncSettingsService, BookRepository bookRepository) {
|
||||
this.hardcoverSyncSettingsService = hardcoverSyncSettingsService;
|
||||
this.bookRepository = bookRepository;
|
||||
this.restClient = RestClient.builder()
|
||||
.baseUrl(HARDCOVER_API_URL)
|
||||
@@ -65,7 +63,7 @@ public class HardcoverSyncService {
|
||||
public void syncProgressToHardcover(Long bookId, Float progressPercent, Long userId) {
|
||||
try {
|
||||
// Get user's Hardcover settings
|
||||
KoboSyncSettings userSettings = koboSettingsService.getSettingsByUserId(userId);
|
||||
HardcoverSyncSettings userSettings = hardcoverSyncSettingsService.getSettingsForUserId(userId);
|
||||
|
||||
if (!isHardcoverSyncEnabledForUser(userSettings)) {
|
||||
log.trace("Hardcover sync skipped for user {}: not enabled or no API token configured", userId);
|
||||
@@ -102,6 +100,18 @@ public class HardcoverSyncService {
|
||||
hardcoverBook.bookId = metadata.getHardcoverBookId();
|
||||
hardcoverBook.pages = metadata.getPageCount();
|
||||
log.debug("Using stored Hardcover book ID: {}", hardcoverBook.bookId);
|
||||
|
||||
if (hardcoverBook.bookId != null && (hardcoverBook.pages == null || hardcoverBook.pages <= 0 || hardcoverBook.editionId == null)) {
|
||||
HardcoverBookInfo fetched = findHardcoverBookById(hardcoverBook.bookId);
|
||||
if (fetched != null) {
|
||||
if (hardcoverBook.editionId == null) {
|
||||
hardcoverBook.editionId = fetched.editionId;
|
||||
}
|
||||
if ((hardcoverBook.pages == null || hardcoverBook.pages <= 0) && fetched.pages != null) {
|
||||
hardcoverBook.pages = fetched.pages;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Search by ISBN
|
||||
hardcoverBook = findHardcoverBook(metadata);
|
||||
@@ -152,7 +162,7 @@ public class HardcoverSyncService {
|
||||
/**
|
||||
* Check if Hardcover sync is enabled for a specific user.
|
||||
*/
|
||||
private boolean isHardcoverSyncEnabledForUser(KoboSyncSettings userSettings) {
|
||||
private boolean isHardcoverSyncEnabledForUser(HardcoverSyncSettings userSettings) {
|
||||
if (userSettings == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -247,15 +257,17 @@ public class HardcoverSyncService {
|
||||
}
|
||||
|
||||
// If no default edition, try to look up edition by ISBN
|
||||
// This also gets the page count from the specific edition
|
||||
if (info.bookId != null) {
|
||||
if (info.bookId != null && info.editionId == null) {
|
||||
EditionInfo edition = findEditionByIsbn(info.bookId, isbn);
|
||||
if (edition != null) {
|
||||
info.editionId = edition.id;
|
||||
// Prefer edition page count over book page count
|
||||
if (edition.pages != null && edition.pages > 0) {
|
||||
info.pages = edition.pages;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (info.editionId != null && (info.pages == null || info.pages <= 0)) {
|
||||
EditionInfo edition = findEditionById(info.editionId);
|
||||
if (edition != null && edition.pages != null && edition.pages > 0) {
|
||||
info.pages = edition.pages;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -326,6 +338,111 @@ public class HardcoverSyncService {
|
||||
}
|
||||
}
|
||||
|
||||
private EditionInfo findEditionById(Integer editionId) {
|
||||
String query = """
|
||||
query FindEditionById($editionId: Int!) {
|
||||
editions(where: {id: {_eq: $editionId}}, limit: 1) {
|
||||
id
|
||||
pages
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
GraphQLRequest request = new GraphQLRequest();
|
||||
request.setQuery(query);
|
||||
request.setVariables(Map.of("editionId", editionId));
|
||||
|
||||
try {
|
||||
Map<String, Object> response = executeGraphQL(request);
|
||||
if (response == null) return null;
|
||||
|
||||
Map<String, Object> data = (Map<String, Object>) response.get("data");
|
||||
if (data == null) return null;
|
||||
|
||||
List<Map<String, Object>> editions = (List<Map<String, Object>>) data.get("editions");
|
||||
if (editions == null || editions.isEmpty()) return null;
|
||||
|
||||
Map<String, Object> edition = editions.getFirst();
|
||||
EditionInfo info = new EditionInfo();
|
||||
|
||||
Object idObj = edition.get("id");
|
||||
if (idObj instanceof Number) {
|
||||
info.id = ((Number) idObj).intValue();
|
||||
}
|
||||
|
||||
Object pagesObj = edition.get("pages");
|
||||
if (pagesObj instanceof Number) {
|
||||
info.pages = ((Number) pagesObj).intValue();
|
||||
}
|
||||
|
||||
return info.id != null ? info : null;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.debug("Failed to find edition by ID: {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private HardcoverBookInfo findHardcoverBookById(Integer bookId) {
|
||||
String query = """
|
||||
query FindBookById($bookId: Int!) {
|
||||
books(where: {id: {_eq: $bookId}}, limit: 1) {
|
||||
id
|
||||
default_edition_id
|
||||
pages
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
GraphQLRequest request = new GraphQLRequest();
|
||||
request.setQuery(query);
|
||||
request.setVariables(Map.of("bookId", bookId));
|
||||
|
||||
try {
|
||||
Map<String, Object> response = executeGraphQL(request);
|
||||
if (response == null) return null;
|
||||
|
||||
Map<String, Object> data = (Map<String, Object>) response.get("data");
|
||||
if (data == null) return null;
|
||||
|
||||
List<Map<String, Object>> books = (List<Map<String, Object>>) data.get("books");
|
||||
if (books == null || books.isEmpty()) return null;
|
||||
|
||||
Map<String, Object> book = books.getFirst();
|
||||
HardcoverBookInfo info = new HardcoverBookInfo();
|
||||
info.bookId = bookId;
|
||||
|
||||
Object defaultEditionObj = book.get("default_edition_id");
|
||||
if (defaultEditionObj instanceof Number) {
|
||||
info.editionId = ((Number) defaultEditionObj).intValue();
|
||||
} else if (defaultEditionObj instanceof String) {
|
||||
try {
|
||||
info.editionId = Integer.parseInt((String) defaultEditionObj);
|
||||
} catch (NumberFormatException e) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
|
||||
Object pagesObj = book.get("pages");
|
||||
if (pagesObj instanceof Number) {
|
||||
info.pages = ((Number) pagesObj).intValue();
|
||||
}
|
||||
|
||||
if (info.editionId != null && (info.pages == null || info.pages <= 0)) {
|
||||
EditionInfo edition = findEditionById(info.editionId);
|
||||
if (edition != null && edition.pages != null && edition.pages > 0) {
|
||||
info.pages = edition.pages;
|
||||
}
|
||||
}
|
||||
|
||||
return info.editionId != null || info.pages != null ? info : null;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.debug("Failed to find Hardcover book by ID {}: {}", bookId, e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a book into the user's library or get existing user_book_id.
|
||||
*/
|
||||
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
package com.adityachandel.booklore.service.hardcover;
|
||||
|
||||
import com.adityachandel.booklore.config.security.service.AuthenticationService;
|
||||
import com.adityachandel.booklore.exception.ApiError;
|
||||
import com.adityachandel.booklore.model.dto.BookLoreUser;
|
||||
import com.adityachandel.booklore.model.dto.HardcoverSyncSettings;
|
||||
import com.adityachandel.booklore.model.dto.settings.UserSettingKey;
|
||||
import com.adityachandel.booklore.model.entity.BookLoreUserEntity;
|
||||
import com.adityachandel.booklore.model.entity.KoboUserSettingsEntity;
|
||||
import com.adityachandel.booklore.model.entity.UserSettingEntity;
|
||||
import com.adityachandel.booklore.repository.KoboUserSettingsRepository;
|
||||
import com.adityachandel.booklore.repository.UserRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class HardcoverSyncSettingsService {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
private final AuthenticationService authenticationService;
|
||||
private final KoboUserSettingsRepository koboUserSettingsRepository;
|
||||
|
||||
@Transactional
|
||||
public HardcoverSyncSettings getCurrentUserSettings() {
|
||||
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||
return getSettingsForUserId(user.getId());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public HardcoverSyncSettings getSettingsForUserId(Long userId) {
|
||||
BookLoreUserEntity user = userRepository.findById(userId)
|
||||
.orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(userId));
|
||||
return readSettings(user, userId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public HardcoverSyncSettings updateCurrentUserSettings(HardcoverSyncSettings settings) {
|
||||
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||
return updateSettingsForUserId(user.getId(), settings);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public HardcoverSyncSettings updateSettingsForUserId(Long userId, HardcoverSyncSettings settings) {
|
||||
if (settings == null) {
|
||||
throw ApiError.INVALID_INPUT.createException("Hardcover settings cannot be null.");
|
||||
}
|
||||
|
||||
BookLoreUserEntity user = userRepository.findById(userId)
|
||||
.orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(userId));
|
||||
|
||||
String apiKey = settings.getHardcoverApiKey();
|
||||
if (apiKey == null) {
|
||||
apiKey = "";
|
||||
} else {
|
||||
apiKey = apiKey.trim();
|
||||
}
|
||||
upsertSetting(user, UserSettingKey.HARDCOVER_API_KEY, apiKey);
|
||||
upsertSetting(user, UserSettingKey.HARDCOVER_SYNC_ENABLED, Boolean.toString(settings.isHardcoverSyncEnabled()));
|
||||
|
||||
userRepository.save(user);
|
||||
|
||||
HardcoverSyncSettings updated = new HardcoverSyncSettings();
|
||||
updated.setHardcoverApiKey(apiKey);
|
||||
updated.setHardcoverSyncEnabled(settings.isHardcoverSyncEnabled());
|
||||
return updated;
|
||||
}
|
||||
|
||||
private HardcoverSyncSettings readSettings(BookLoreUserEntity user, Long userId) {
|
||||
UserSettingEntity apiKeySetting = findSetting(user, UserSettingKey.HARDCOVER_API_KEY).orElse(null);
|
||||
UserSettingEntity syncEnabledSetting = findSetting(user, UserSettingKey.HARDCOVER_SYNC_ENABLED).orElse(null);
|
||||
|
||||
String apiKey = apiKeySetting != null ? apiKeySetting.getSettingValue() : null;
|
||||
boolean syncEnabled = syncEnabledSetting != null && Boolean.parseBoolean(syncEnabledSetting.getSettingValue());
|
||||
|
||||
boolean shouldSave = false;
|
||||
if (apiKeySetting == null || syncEnabledSetting == null) {
|
||||
KoboUserSettingsEntity legacySettings = koboUserSettingsRepository.findByUserId(userId).orElse(null);
|
||||
if (legacySettings != null) {
|
||||
if (apiKeySetting == null && legacySettings.getHardcoverApiKey() != null && !legacySettings.getHardcoverApiKey().isBlank()) {
|
||||
apiKey = legacySettings.getHardcoverApiKey();
|
||||
upsertSetting(user, UserSettingKey.HARDCOVER_API_KEY, apiKey);
|
||||
shouldSave = true;
|
||||
}
|
||||
if (syncEnabledSetting == null && legacySettings.isHardcoverSyncEnabled()) {
|
||||
syncEnabled = legacySettings.isHardcoverSyncEnabled();
|
||||
upsertSetting(user, UserSettingKey.HARDCOVER_SYNC_ENABLED, Boolean.toString(syncEnabled));
|
||||
shouldSave = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (shouldSave) {
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
||||
HardcoverSyncSettings settings = new HardcoverSyncSettings();
|
||||
settings.setHardcoverApiKey(apiKey);
|
||||
settings.setHardcoverSyncEnabled(syncEnabled);
|
||||
return settings;
|
||||
}
|
||||
|
||||
private Optional<UserSettingEntity> findSetting(BookLoreUserEntity user, UserSettingKey key) {
|
||||
return user.getSettings().stream()
|
||||
.filter(setting -> key.getDbKey().equals(setting.getSettingKey()))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
private void upsertSetting(BookLoreUserEntity user, UserSettingKey key, String value) {
|
||||
UserSettingEntity setting = findSetting(user, key)
|
||||
.orElseGet(() -> {
|
||||
UserSettingEntity newSetting = new UserSettingEntity();
|
||||
newSetting.setUser(user);
|
||||
newSetting.setSettingKey(key.getDbKey());
|
||||
user.getSettings().add(newSetting);
|
||||
return newSetting;
|
||||
});
|
||||
setting.setSettingValue(value);
|
||||
}
|
||||
}
|
||||
+17
-9
@@ -2,6 +2,7 @@ package com.adityachandel.booklore.service.kobo;
|
||||
|
||||
import com.adityachandel.booklore.config.security.service.AuthenticationService;
|
||||
import com.adityachandel.booklore.model.dto.BookLoreUser;
|
||||
import com.adityachandel.booklore.model.dto.HardcoverSyncSettings;
|
||||
import com.adityachandel.booklore.model.dto.KoboSyncSettings;
|
||||
import com.adityachandel.booklore.model.dto.Shelf;
|
||||
import com.adityachandel.booklore.model.dto.request.ShelfCreateRequest;
|
||||
@@ -11,6 +12,7 @@ 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;
|
||||
import com.adityachandel.booklore.service.hardcover.HardcoverSyncSettingsService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -25,6 +27,7 @@ public class KoboSettingsService {
|
||||
private final KoboUserSettingsRepository repository;
|
||||
private final AuthenticationService authenticationService;
|
||||
private final ShelfService shelfService;
|
||||
private final HardcoverSyncSettingsService hardcoverSyncSettingsService;
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public KoboSyncSettings getCurrentUserSettings() {
|
||||
@@ -82,12 +85,8 @@ public class KoboSettingsService {
|
||||
|
||||
entity.setAutoAddToShelf(settings.isAutoAddToShelf());
|
||||
|
||||
// Update Hardcover settings
|
||||
entity.setHardcoverApiKey(settings.getHardcoverApiKey());
|
||||
entity.setHardcoverSyncEnabled(settings.isHardcoverSyncEnabled());
|
||||
|
||||
repository.save(entity);
|
||||
return mapToDto(entity);
|
||||
return mapToDto(entity, hardcoverSyncSettingsService.getSettingsForUserId(user.getId()));
|
||||
}
|
||||
|
||||
private KoboUserSettingsEntity initDefaultSettings(Long userId) {
|
||||
@@ -118,6 +117,11 @@ public class KoboSettingsService {
|
||||
}
|
||||
|
||||
private KoboSyncSettings mapToDto(KoboUserSettingsEntity entity) {
|
||||
HardcoverSyncSettings hardcoverSettings = hardcoverSyncSettingsService.getSettingsForUserId(entity.getUserId());
|
||||
return mapToDto(entity, hardcoverSettings);
|
||||
}
|
||||
|
||||
private KoboSyncSettings mapToDto(KoboUserSettingsEntity entity, HardcoverSyncSettings hardcoverSettings) {
|
||||
KoboSyncSettings dto = new KoboSyncSettings();
|
||||
dto.setId(entity.getId());
|
||||
dto.setUserId(entity.getUserId().toString());
|
||||
@@ -126,14 +130,17 @@ public class KoboSettingsService {
|
||||
dto.setProgressMarkAsReadingThreshold(entity.getProgressMarkAsReadingThreshold());
|
||||
dto.setProgressMarkAsFinishedThreshold(entity.getProgressMarkAsFinishedThreshold());
|
||||
dto.setAutoAddToShelf(entity.isAutoAddToShelf());
|
||||
dto.setHardcoverApiKey(entity.getHardcoverApiKey());
|
||||
dto.setHardcoverSyncEnabled(entity.isHardcoverSyncEnabled());
|
||||
if (hardcoverSettings != null) {
|
||||
dto.setHardcoverApiKey(hardcoverSettings.getHardcoverApiKey());
|
||||
dto.setHardcoverSyncEnabled(hardcoverSettings.isHardcoverSyncEnabled());
|
||||
} else {
|
||||
dto.setHardcoverSyncEnabled(false);
|
||||
}
|
||||
return dto;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Hardcover settings for a specific user by ID.
|
||||
* Used by HardcoverSyncService to get user-specific API key.
|
||||
* Get Kobo settings for a specific user by ID.
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public KoboSyncSettings getSettingsByUserId(Long userId) {
|
||||
@@ -141,4 +148,5 @@ public class KoboSettingsService {
|
||||
.map(this::mapToDto)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+15
@@ -22,6 +22,7 @@ import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.repository.KoreaderUserRepository;
|
||||
import com.adityachandel.booklore.repository.UserBookProgressRepository;
|
||||
import com.adityachandel.booklore.repository.UserRepository;
|
||||
import com.adityachandel.booklore.service.hardcover.HardcoverSyncService;
|
||||
|
||||
@Slf4j
|
||||
@AllArgsConstructor
|
||||
@@ -32,6 +33,7 @@ public class KoreaderService {
|
||||
private final BookRepository bookRepository;
|
||||
private final UserRepository userRepository;
|
||||
private final KoreaderUserRepository koreaderUserRepository;
|
||||
private final HardcoverSyncService hardcoverSyncService;
|
||||
|
||||
public ResponseEntity<Map<String, String>> authorizeUser() {
|
||||
KoreaderUserDetails authDetails = getAuthDetails();
|
||||
@@ -76,6 +78,9 @@ public class KoreaderService {
|
||||
progressRepository.save(userProgress);
|
||||
|
||||
log.info("saveProgress: saved progress='{}' percentage={} for userId={} bookHash={}", koProgress.getProgress(), koProgress.getPercentage(), authDetails.getBookLoreUserId(), bookHash);
|
||||
|
||||
Float progressPercent = normalizeProgressPercent(koProgress.getPercentage());
|
||||
hardcoverSyncService.syncProgressToHardcover(book.getId(), progressPercent, authDetails.getBookLoreUserId());
|
||||
}
|
||||
|
||||
private void updateProgressData(UserBookProgressEntity userProgress, KoreaderProgress koProgress) {
|
||||
@@ -101,6 +106,16 @@ public class KoreaderService {
|
||||
}
|
||||
}
|
||||
|
||||
private Float normalizeProgressPercent(Float progress) {
|
||||
if (progress == null) {
|
||||
return null;
|
||||
}
|
||||
if (progress <= 1.0f) {
|
||||
return progress * 100.0f;
|
||||
}
|
||||
return progress;
|
||||
}
|
||||
|
||||
private KoreaderUserDetails getAuthDetails() {
|
||||
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
||||
if (!(principal instanceof KoreaderUserDetails details)) {
|
||||
|
||||
Reference in New Issue
Block a user