fix(koreader): fix bidirectional progress sync (#1687)

Co-authored-by: Wesam Jabali <wesamjabali@icloud.com>
This commit is contained in:
Zach
2026-06-09 21:00:25 -04:00
committed by GitHub
co-authored by Wesam Jabali
parent 46f4457fee
commit 590bd9eea5
7 changed files with 302 additions and 0 deletions
@@ -17,6 +17,7 @@ import org.booklore.model.entity.UserBookProgressEntity;
import org.booklore.model.enums.ReadStatus;
import org.booklore.repository.*;
import org.booklore.service.hardcover.HardcoverSyncService;
import org.booklore.service.koreader.KoreaderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -56,6 +57,7 @@ public class KoboReadingStateService {
private final KoboSettingsService koboSettingsService;
private final KoboReadingStateBuilder readingStateBuilder;
private final HardcoverSyncService hardcoverSyncService;
private final KoreaderService koreaderService;
@Transactional
public KoboReadingStateResponse saveReadingState(List<KoboReadingState> readingStates) {
@@ -251,6 +253,7 @@ public class KoboReadingStateService {
&& progress.getKoboProgressPercent() != null
&& (!progress.getKoboProgressPercent().equals(previousKoboProgressPercent)
|| progress.getReadStatus() != previousReadStatus)) {
koreaderService.syncProgressToKoreader(book.getId(), progress.getKoboProgressPercent(), userId);
hardcoverSyncService.syncProgressToHardcover(book.getId(), progress.getKoboProgressPercent(), userId);
}
} catch (NumberFormatException e) {
@@ -6,6 +6,7 @@ import org.booklore.config.security.userdetails.KoreaderUserDetails;
import org.booklore.exception.ApiError;
import org.booklore.model.dto.progress.KoreaderProgress;
import org.booklore.model.entity.*;
import org.booklore.model.enums.BookFileType;
import org.booklore.model.enums.ReadStatus;
import org.booklore.repository.*;
import org.booklore.service.hardcover.HardcoverSyncService;
@@ -92,6 +93,47 @@ public class KoreaderService {
}
}
@Transactional
public void syncProgressToKoreader(long bookId, float percentage, long userId) {
try {
koreaderUserRepository.findByBookLoreUserId(userId)
.filter(KoreaderUserEntity::isSyncEnabled)
.filter(KoreaderUserEntity::isSyncWithWebReader)
.ifPresent(koreaderUser -> {
BookEntity book = bookRepository.findById(bookId).orElse(null);
BookLoreUserEntity user = userRepository.findById(userId).orElse(null);
if (book == null || user == null) {
log.warn("KOReader sync skipped: bookId={} or userId={} was not found", bookId, userId);
return;
}
UserBookProgressEntity progress = getOrCreateUserProgress(user, book);
float koreaderPercent = percentage / 100.0f;
progress.setKoreaderProgressPercent(koreaderPercent);
progress.setKoreaderLastSyncTime(Instant.now());
progress.setKoreaderProgress(null);
BookFileEntity primaryFile = book.getPrimaryBookFile();
if (primaryFile != null && primaryFile.getBookType() == BookFileType.EPUB
&& progress.getEpubProgress() != null) {
try {
String xpointer = epubCfiService.convertCfiToProgressXPointer(
primaryFile.getFullFilePath(), progress.getEpubProgress());
progress.setKoreaderProgress(xpointer);
} catch (Exception e) {
progress.setKoreaderProgress(null);
log.warn("Failed to convert CFI to XPointer for KOReader sync", e);
}
}
progressRepository.save(progress);
});
} catch (Exception e) {
log.warn("Failed to sync progress to KOReader for userId={} bookId={}", userId, bookId, e);
}
}
private void saveToFileProgress(BookLoreUserEntity user, BookEntity book, UserBookProgressEntity progress) {
try {
BookFileEntity primaryFile = book.getPrimaryBookFile();
@@ -20,6 +20,7 @@ import org.booklore.model.enums.UserPermission;
import org.booklore.repository.*;
import org.booklore.service.hardcover.HardcoverSyncService;
import org.booklore.service.kobo.KoboReadingStateService;
import org.booklore.service.koreader.KoreaderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
@@ -46,6 +47,7 @@ public class ReadingProgressService {
private final UserRepository userRepository;
private final AuthenticationService authenticationService;
private final KoboReadingStateService koboReadingStateService;
private final KoreaderService koreaderService;
private final HardcoverSyncService hardcoverSyncService;
// ==================== Methods from UserProgressService ====================
@@ -270,6 +272,7 @@ public class ReadingProgressService {
userBookProgressRepository.save(progress);
if (percentage != null) {
koreaderService.syncProgressToKoreader(book.getId(), percentage, user.getId());
hardcoverSyncService.syncProgressToHardcover(book.getId(), percentage, user.getId());
}
}