-
-
Notifications
You must be signed in to change notification settings - Fork 199
fix(koreader): web reader to koreader progress sync #1064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
30a55a8
c33b3ef
c2df8cd
ca7a3a6
d412ced
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| 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,40 @@ public void saveProgress(String bookHash, KoreaderProgress koProgress) { | |
| } | ||
| } | ||
|
|
||
| @Transactional | ||
| public void syncProgressToKoreader(Long bookId, Float percentage, Long userId) { | ||
| if (percentage == null) return; | ||
|
|
||
| koreaderUserRepository.findByBookLoreUserId(userId) | ||
| .filter(KoreaderUserEntity::isSyncEnabled) | ||
| .filter(KoreaderUserEntity::isSyncWithBookloreReader) | ||
| .ifPresent(koreaderUser -> { | ||
| BookEntity book = bookRepository.findById(bookId).orElse(null); | ||
| BookLoreUserEntity user = userRepository.findById(userId).orElse(null); | ||
| if (book == null || user == null) return; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably log here - maybe a warn? This probably shouldn't happen. |
||
|
|
||
| UserBookProgressEntity progress = getOrCreateUserProgress(user, book); | ||
|
|
||
| float koreaderPercent = percentage / 100.0f; | ||
| progress.setKoreaderProgressPercent(koreaderPercent); | ||
| progress.setKoreaderLastSyncTime(Instant.now()); | ||
|
|
||
| BookFileEntity primaryFile = book.getPrimaryBookFile(); | ||
| if (primaryFile != null && primaryFile.getBookType() == BookFileType.EPUB | ||
| && progress.getEpubProgress() != null) { | ||
| try { | ||
| String xpointer = epubCfiService.convertCfiToProgressXPointer( | ||
| book.getFullFilePath(), progress.getEpubProgress()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using the book full file path we should be explicit and use the |
||
| progress.setKoreaderProgress(xpointer); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if this fails to convert and the koreader progress progress is mismatched with koreader progress percent? If that's a mismatch causes failure elsewhere, should we try to do the conversion first and then bail if it doesn't work as expected? |
||
| } catch (Exception e) { | ||
| log.warn("Failed to convert CFI to XPointer for KOReader sync", e); | ||
| } | ||
| } | ||
|
Comment on lines
+117
to
+124
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stale When This was flagged in the previous review ("Clear KOReader sync state when CFI conversion fails") and was not addressed when the logic was moved here. 🐛 Proposed fix } catch (Exception e) {
+ progress.setKoreaderProgress(null);
log.warn("Failed to convert CFI to XPointer for KOReader sync", e);
}🤖 Prompt for AI Agents |
||
|
|
||
| progressRepository.save(progress); | ||
| }); | ||
| } | ||
|
Comment on lines
+96
to
+128
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Synchronous I/O on the request thread and unconstrained transaction coupling.
As per coding guidelines, virtual threads / async dispatch should be preferred for I/O-bound work. ♻️ Proposed alignment with HardcoverSyncService pattern+import org.springframework.scheduling.annotation.Async;
- `@Transactional`
+ `@Async`
+ `@Transactional`(propagation = Propagation.REQUIRES_NEW)
public void syncProgressToKoreader(Long bookId, Float percentage, Long userId) {Using 🤖 Prompt for AI Agents |
||
|
|
||
| private void saveToFileProgress(BookLoreUserEntity user, BookEntity book, UserBookProgressEntity progress) { | ||
| try { | ||
| BookFileEntity primaryFile = book.getPrimaryBookFile(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| import org.booklore.repository.*; | ||
| import org.booklore.service.kobo.KoboReadingStateService; | ||
| import org.booklore.service.hardcover.HardcoverSyncService; | ||
| import org.booklore.service.koreader.KoreaderService; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.InjectMocks; | ||
|
|
@@ -46,6 +47,8 @@ class ReadingProgressServiceTest { | |
| private KoboReadingStateService koboReadingStateService; | ||
| @Mock | ||
| private HardcoverSyncService hardcoverSyncService; | ||
| @Mock | ||
| private KoreaderService koreaderService; | ||
|
|
||
| @InjectMocks | ||
| private ReadingProgressService readingProgressService; | ||
|
|
@@ -286,6 +289,106 @@ void updateReadProgress_pdf_shouldSaveProgress() { | |
| assertEquals(50f, progress.getPdfProgressPercent()); | ||
| } | ||
|
|
||
| @Test | ||
| void updateReadProgress_shouldDelegateKoreaderSync() { | ||
| long bookId = 1L; | ||
| BookEntity book = new BookEntity(); | ||
| book.setId(bookId); | ||
| BookFileEntity primaryFile = new BookFileEntity(); | ||
| primaryFile.setId(1L); | ||
| primaryFile.setBook(book); | ||
| primaryFile.setBookType(BookFileType.EPUB); | ||
| book.setBookFiles(List.of(primaryFile)); | ||
|
|
||
| BookLoreUser user = mock(BookLoreUser.class); | ||
| when(user.getId()).thenReturn(2L); | ||
| when(authenticationService.getAuthenticatedUser()).thenReturn(user); | ||
| when(bookRepository.findByIdWithBookFiles(bookId)).thenReturn(Optional.of(book)); | ||
|
|
||
| BookLoreUserEntity userEntity = new BookLoreUserEntity(); | ||
| userEntity.setId(2L); | ||
| when(userRepository.findById(2L)).thenReturn(Optional.of(userEntity)); | ||
|
|
||
| UserBookProgressEntity progress = new UserBookProgressEntity(); | ||
| when(userBookProgressRepository.findByUserIdAndBookId(2L, bookId)).thenReturn(Optional.of(progress)); | ||
|
|
||
| when(userBookFileProgressRepository.findByUserIdAndBookFileId(2L, 1L)).thenReturn(Optional.empty()); | ||
|
|
||
| ReadProgressRequest req = new ReadProgressRequest(); | ||
| req.setBookId(bookId); | ||
| EpubProgress epubProgress = EpubProgress.builder().cfi("cfi").percentage(100f).build(); | ||
| req.setEpubProgress(epubProgress); | ||
|
|
||
| readingProgressService.updateReadProgress(req); | ||
|
|
||
| verify(koreaderService).syncProgressToKoreader(bookId, 100f, 2L); | ||
| } | ||
|
Comment on lines
+292
to
+325
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing test for the CFI-conversion exception path. When ✅ Suggested test skeleton`@Test`
void updateReadProgress_shouldSyncPercentButNotXPointerWhenCfiConversionFails() {
// same book/user/koreaderUser setup as shouldSyncToKoreaderWhenEnabled...
when(epubCfiService.convertCfiToProgressXPointer(any(Path.class), eq("cfi")))
.thenThrow(new RuntimeException("conversion failed"));
// ... fire updateReadProgress ...
assertEquals(1.0f, progress.getKoreaderProgressPercent());
assertNull(progress.getKoreaderProgress()); // XPointer not set on failure
assertNotNull(progress.getKoreaderLastSyncTime()); // sync time still recorded
}🤖 Prompt for AI Agents |
||
|
|
||
| @Test | ||
| void updateReadProgress_shouldDelegateKoreaderSyncWithCorrectPercentage() { | ||
| long bookId = 1L; | ||
| BookEntity book = new BookEntity(); | ||
| book.setId(bookId); | ||
| BookFileEntity primaryFile = new BookFileEntity(); | ||
| primaryFile.setId(1L); | ||
| primaryFile.setBook(book); | ||
| primaryFile.setBookType(BookFileType.PDF); | ||
| book.setBookFiles(List.of(primaryFile)); | ||
|
|
||
| BookLoreUser user = mock(BookLoreUser.class); | ||
| when(user.getId()).thenReturn(2L); | ||
| when(authenticationService.getAuthenticatedUser()).thenReturn(user); | ||
| when(bookRepository.findByIdWithBookFiles(bookId)).thenReturn(Optional.of(book)); | ||
|
|
||
| BookLoreUserEntity userEntity = new BookLoreUserEntity(); | ||
| userEntity.setId(2L); | ||
| when(userRepository.findById(2L)).thenReturn(Optional.of(userEntity)); | ||
|
|
||
| UserBookProgressEntity progress = new UserBookProgressEntity(); | ||
| when(userBookProgressRepository.findByUserIdAndBookId(2L, bookId)).thenReturn(Optional.of(progress)); | ||
|
|
||
| when(userBookFileProgressRepository.findByUserIdAndBookFileId(2L, 1L)).thenReturn(Optional.empty()); | ||
|
|
||
| ReadProgressRequest req = new ReadProgressRequest(); | ||
| req.setBookId(bookId); | ||
| PdfProgress pdfProgress = PdfProgress.builder().page(10).percentage(50f).build(); | ||
| req.setPdfProgress(pdfProgress); | ||
|
|
||
| readingProgressService.updateReadProgress(req); | ||
|
|
||
| verify(koreaderService).syncProgressToKoreader(bookId, 50f, 2L); | ||
| } | ||
|
|
||
| @Test | ||
| void updateReadProgress_shouldNotDelegateKoreaderSyncWhenPercentageNull() { | ||
| long bookId = 1L; | ||
| BookEntity book = new BookEntity(); | ||
| book.setId(bookId); | ||
| BookFileEntity primaryFile = new BookFileEntity(); | ||
| primaryFile.setId(1L); | ||
| primaryFile.setBook(book); | ||
| primaryFile.setBookType(BookFileType.EPUB); | ||
| book.setBookFiles(List.of(primaryFile)); | ||
|
|
||
| BookLoreUser user = mock(BookLoreUser.class); | ||
| when(user.getId()).thenReturn(2L); | ||
| when(authenticationService.getAuthenticatedUser()).thenReturn(user); | ||
| when(bookRepository.findByIdWithBookFiles(bookId)).thenReturn(Optional.of(book)); | ||
|
|
||
| BookLoreUserEntity userEntity = new BookLoreUserEntity(); | ||
| userEntity.setId(2L); | ||
| when(userRepository.findById(2L)).thenReturn(Optional.of(userEntity)); | ||
|
|
||
| when(userBookProgressRepository.findByUserIdAndBookId(2L, bookId)).thenReturn(Optional.empty()); | ||
|
|
||
| ReadProgressRequest req = new ReadProgressRequest(); | ||
| req.setBookId(bookId); | ||
|
|
||
| readingProgressService.updateReadProgress(req); | ||
|
|
||
| verify(koreaderService, never()).syncProgressToKoreader(anyLong(), anyFloat(), anyLong()); | ||
| } | ||
|
Comment on lines
+292
to
+390
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win Three new delegation tests cover the happy path and the no-op path, but The new tests correctly verify that Key scenarios that should be covered in a
Would you like me to draft a 🤖 Prompt for AI Agents |
||
|
|
||
| @Test | ||
| void resetProgress_booklore_shouldCallBulkReset() { | ||
| BookLoreUser user = mock(BookLoreUser.class); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can unbox these types.