From 4527d43e84957caffe1ebeeb48bd6f659638dcc2 Mon Sep 17 00:00:00 2001 From: brios <127139797+balazs-szucs@users.noreply.github.com> Date: Tue, 7 Apr 2026 11:48:26 +0200 Subject: [PATCH] fix(reader): add fetchFreshBookDetail method to ensure up-to-date reading position across devices (#358) * fix(reader): add fetchFreshBookDetail method to ensure up-to-date reading position across devices * test(audiobook): enhance tests to verify queryClient interactions for progress updates * fix(audiobook): include audiobookProgress and fileProgress in progress payload --- .../book/service/book-patch.service.spec.ts | 5 ++++ .../book/service/book-patch.service.ts | 27 ++++++++++++++++--- .../app/features/book/service/book.service.ts | 14 ++++++++++ .../audiobook-player.component.ts | 2 +- .../audiobook.service.spec.ts | 18 +++++++++---- .../audiobook-player/audiobook.service.ts | 27 +++++++++++-------- .../cbx-reader/cbx-reader.component.ts | 2 +- .../ebook-reader/ebook-reader.component.ts | 2 +- .../pdf-reader/pdf-reader.component.ts | 2 +- 9 files changed, 76 insertions(+), 23 deletions(-) diff --git a/frontend/src/app/features/book/service/book-patch.service.spec.ts b/frontend/src/app/features/book/service/book-patch.service.spec.ts index 3dfae4a73..88b58d6b8 100644 --- a/frontend/src/app/features/book/service/book-patch.service.spec.ts +++ b/frontend/src/app/features/book/service/book-patch.service.spec.ts @@ -56,6 +56,9 @@ describe('BookPatchService', () => { }, }); request.flush(null); + + expect(queryClient.setQueryData).toHaveBeenCalledWith(BOOKS_QUERY_KEY, expect.any(Function)); + expect(queryClient.invalidateQueries).toHaveBeenCalledWith({queryKey: ['books', 'detail', 11]}); }); it('deduplicates identical EPUB progress updates before posting', () => { @@ -80,6 +83,8 @@ describe('BookPatchService', () => { request.flush(null); httpTestingController.expectNone(req => req.url.endsWith('/api/v1/books/progress')); + expect(queryClient.setQueryData).toHaveBeenCalledWith(BOOKS_QUERY_KEY, expect.any(Function)); + expect(queryClient.invalidateQueries).toHaveBeenCalledWith({queryKey: ['books', 'detail', 7]}); }); it('patches cached progress fields when resetting kobo progress', () => { diff --git a/frontend/src/app/features/book/service/book-patch.service.ts b/frontend/src/app/features/book/service/book-patch.service.ts index a19348b4b..b36da1ec2 100644 --- a/frontend/src/app/features/book/service/book-patch.service.ts +++ b/frontend/src/app/features/book/service/book-patch.service.ts @@ -79,7 +79,14 @@ export class BookPatchService { progressPercent: payload.percentage }; } - return this.http.post(`${this.url}/progress`, body); + return this.http.post(`${this.url}/progress`, body).pipe( + tap(() => { + patchBookFieldsInCache(this.queryClient, [{ + bookId: payload.bookId, + fields: {epubProgress: {cfi: payload.cfi, percentage: payload.percentage}} + }]); + }) + ); }), share() ); @@ -127,7 +134,14 @@ export class BookPatchService { progressPercent: percentage }; } - return this.http.post(`${this.url}/progress`, body); + return this.http.post(`${this.url}/progress`, body).pipe( + tap(() => { + patchBookFieldsInCache(this.queryClient, [{ + bookId, + fields: {pdfProgress: {page, percentage}} + }]); + }) + ); } saveEpubProgress(bookId: number, cfi: string, href: string, percentage: number, bookFileId?: number): void { @@ -160,7 +174,14 @@ export class BookPatchService { progressPercent: percentage }; } - return this.http.post(`${this.url}/progress`, body); + return this.http.post(`${this.url}/progress`, body).pipe( + tap(() => { + patchBookFieldsInCache(this.queryClient, [{ + bookId, + fields: {cbxProgress: {page, percentage}} + }]); + }) + ); } saveFileProgress(bookId: number, fileProgress: BookFileProgress): Observable { diff --git a/frontend/src/app/features/book/service/book.service.ts b/frontend/src/app/features/book/service/book.service.ts index b66de891f..469b09b3d 100644 --- a/frontend/src/app/features/book/service/book.service.ts +++ b/frontend/src/app/features/book/service/book.service.ts @@ -121,6 +121,20 @@ export class BookService { return this.queryClient.ensureQueryData(this.bookDetailQueryOptions(bookId, withDescription)); } + /** + * Always fetches book detail from the server, bypassing the cache. + * + * Used by reader components on initialization to guarantee the latest reading position. + * While optimistic cache updates keep progress current within a single session, + * this network request is necessary to pick up progress saved on another device or browser. + */ + fetchFreshBookDetail(bookId: number, withDescription: boolean): Promise { + return this.queryClient.fetchQuery({ + ...this.bookDetailQueryOptions(bookId, withDescription), + staleTime: 0, + }); + } + private getBookRecommendationsQueryOptions(bookId: number, limit: number) { return queryOptions({ queryKey: bookRecommendationsQueryKey(bookId, limit), diff --git a/frontend/src/app/features/readers/audiobook-player/audiobook-player.component.ts b/frontend/src/app/features/readers/audiobook-player/audiobook-player.component.ts index c092cf419..893628c2d 100644 --- a/frontend/src/app/features/readers/audiobook-player/audiobook-player.component.ts +++ b/frontend/src/app/features/readers/audiobook-player/audiobook-player.component.ts @@ -179,7 +179,7 @@ export class AudiobookPlayerComponent implements OnInit, OnDestroy { this.loadBookmarks(); }), switchMap(info => - from(this.bookService.ensureBookDetail(bookId, false)).pipe( + from(this.bookService.fetchFreshBookDetail(bookId, false)).pipe( tap(book => this.applyBookDetails(book, info)), map(() => void 0), catchError(() => of(void 0)) diff --git a/frontend/src/app/features/readers/audiobook-player/audiobook.service.spec.ts b/frontend/src/app/features/readers/audiobook-player/audiobook.service.spec.ts index c0f0a1b27..50932084a 100644 --- a/frontend/src/app/features/readers/audiobook-player/audiobook.service.spec.ts +++ b/frontend/src/app/features/readers/audiobook-player/audiobook.service.spec.ts @@ -1,6 +1,7 @@ import {provideHttpClient} from '@angular/common/http'; import {HttpTestingController, provideHttpClientTesting} from '@angular/common/http/testing'; import {TestBed} from '@angular/core/testing'; +import {QueryClient} from '@tanstack/angular-query-experimental'; import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; import {AuthService} from '../../../shared/service/auth.service'; @@ -10,20 +11,22 @@ import {AudiobookService} from './audiobook.service'; describe('AudiobookService', () => { let service: AudiobookService; let httpTestingController: HttpTestingController; + let queryClient: QueryClient; let getInternalAccessToken: ReturnType; beforeEach(() => { getInternalAccessToken = vi.fn(() => 'token 123'); + queryClient = new QueryClient(); + vi.spyOn(queryClient, 'setQueryData'); + vi.spyOn(queryClient, 'invalidateQueries').mockResolvedValue(); TestBed.configureTestingModule({ providers: [ provideHttpClient(), provideHttpClientTesting(), AudiobookService, - { - provide: AuthService, - useValue: {getInternalAccessToken}, - }, + {provide: AuthService, useValue: {getInternalAccessToken}}, + {provide: QueryClient, useValue: queryClient}, ], }); @@ -33,6 +36,7 @@ describe('AudiobookService', () => { afterEach(() => { httpTestingController.verify(); + queryClient.clear(); TestBed.resetTestingModule(); vi.restoreAllMocks(); }); @@ -85,9 +89,10 @@ describe('AudiobookService', () => { request.flush(null); expect(completed).toBe(true); + expect(queryClient.setQueryData).toHaveBeenCalledWith(['books'], expect.any(Function)); }); - it('maps audiobook progress into a file progress payload when a book file id is present', () => { + it('includes both audiobookProgress and fileProgress in the payload when a book file id is present', () => { const progress: AudiobookProgress = { positionMs: 75_000, trackIndex: 5, @@ -100,6 +105,7 @@ describe('AudiobookService', () => { expect(request.request.method).toBe('POST'); expect(request.request.body).toEqual({ bookId: 12, + audiobookProgress: progress, fileProgress: { bookFileId: 99, positionData: '75000', @@ -108,5 +114,7 @@ describe('AudiobookService', () => { }, }); request.flush(null); + + expect(queryClient.setQueryData).toHaveBeenCalledWith(['books'], expect.any(Function)); }); }); diff --git a/frontend/src/app/features/readers/audiobook-player/audiobook.service.ts b/frontend/src/app/features/readers/audiobook-player/audiobook.service.ts index 240b3fffc..7a511f47d 100644 --- a/frontend/src/app/features/readers/audiobook-player/audiobook.service.ts +++ b/frontend/src/app/features/readers/audiobook-player/audiobook.service.ts @@ -1,10 +1,13 @@ import { inject, Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; +import { tap } from 'rxjs/operators'; import { API_CONFIG } from '../../../core/config/api-config'; import { AudiobookInfo, AudiobookProgress } from './audiobook.model'; import { AuthService } from '../../../shared/service/auth.service'; import { BookFileProgress } from '../../book/model/book.model'; +import { QueryClient } from '@tanstack/angular-query-experimental'; +import { patchBookFieldsInCache } from '../../book/service/book-query-cache'; @Injectable({ providedIn: 'root' @@ -15,6 +18,7 @@ export class AudiobookService { private http = inject(HttpClient); private authService = inject(AuthService); + private queryClient = inject(QueryClient); getAudiobookInfo(bookId: number, bookType?: string): Observable { const params: Record = {}; @@ -44,21 +48,22 @@ export class AudiobookService { } saveProgress(bookId: number, progress: AudiobookProgress, bookFileId?: number): Observable { + const body: {bookId: number; audiobookProgress: AudiobookProgress; fileProgress?: BookFileProgress} = { + bookId, + audiobookProgress: progress, + }; if (bookFileId) { - const fileProgress: BookFileProgress = { - bookFileId: bookFileId, + body.fileProgress = { + bookFileId, positionData: progress.positionMs.toString(), positionHref: progress.trackIndex?.toString(), - progressPercent: progress.percentage + progressPercent: progress.percentage, }; - return this.http.post(`${this.booksUrl}/progress`, { - bookId, - fileProgress - }); } - return this.http.post(`${this.booksUrl}/progress`, { - bookId, - audiobookProgress: progress - }); + return this.http.post(`${this.booksUrl}/progress`, body).pipe( + tap(() => { + patchBookFieldsInCache(this.queryClient, [{bookId, fields: {audiobookProgress: progress}}]); + }) + ); } } diff --git a/frontend/src/app/features/readers/cbx-reader/cbx-reader.component.ts b/frontend/src/app/features/readers/cbx-reader/cbx-reader.component.ts index 3c0e8ece9..28a14eba4 100644 --- a/frontend/src/app/features/readers/cbx-reader/cbx-reader.component.ts +++ b/frontend/src/app/features/readers/cbx-reader/cbx-reader.component.ts @@ -243,7 +243,7 @@ export class CbxReaderComponent implements OnInit, OnDestroy { this.nextBookInSeries = null; this.currentBook = null; - return from(this.bookService.ensureBookDetail(this.bookId, false)).pipe( + return from(this.bookService.fetchFreshBookDetail(this.bookId, false)).pipe( switchMap((book) => { // Use alternative bookType from query param if provided, otherwise use primary const resolvedBookType = (this.altBookType as BookType | undefined) ?? book.primaryFile?.bookType; diff --git a/frontend/src/app/features/readers/ebook-reader/ebook-reader.component.ts b/frontend/src/app/features/readers/ebook-reader/ebook-reader.component.ts index e3a5579e7..54d7cf421 100644 --- a/frontend/src/app/features/readers/ebook-reader/ebook-reader.component.ts +++ b/frontend/src/app/features/readers/ebook-reader/ebook-reader.component.ts @@ -230,7 +230,7 @@ export class EbookReaderComponent implements OnInit, OnDestroy { this.bookId = +this.route.snapshot.paramMap.get('bookId')!; this.altBookType = this.route.snapshot.queryParamMap.get('bookType') ?? undefined; - return from(this.bookService.ensureBookDetail(this.bookId, false)).pipe( + return from(this.bookService.fetchFreshBookDetail(this.bookId, false)).pipe( switchMap((book) => { this.book = book; diff --git a/frontend/src/app/features/readers/pdf-reader/pdf-reader.component.ts b/frontend/src/app/features/readers/pdf-reader/pdf-reader.component.ts index 232f8e9ca..82fe4b926 100644 --- a/frontend/src/app/features/readers/pdf-reader/pdf-reader.component.ts +++ b/frontend/src/app/features/readers/pdf-reader/pdf-reader.component.ts @@ -126,7 +126,7 @@ export class PdfReaderComponent implements OnInit, OnDestroy, AfterViewInit { this.bookId = +params.get('bookId')!; this.altBookType = this.route.snapshot.queryParamMap.get('bookType') ?? undefined; - return from(this.bookService.ensureBookDetail(this.bookId, false)).pipe( + return from(this.bookService.fetchFreshBookDetail(this.bookId, false)).pipe( switchMap((book) => { if (this.altBookType) { const altFile = book.alternativeFormats?.find(f => f.bookType === this.altBookType);