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
This commit is contained in:
brios
2026-04-22 17:27:06 -04:00
committed by Zach Yale
parent cf7999044f
commit 4527d43e84
9 changed files with 76 additions and 23 deletions
@@ -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))
@@ -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<typeof vi.fn>;
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));
});
});
@@ -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<AudiobookInfo> {
const params: Record<string, string> = {};
@@ -44,21 +48,22 @@ export class AudiobookService {
}
saveProgress(bookId: number, progress: AudiobookProgress, bookFileId?: number): Observable<void> {
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<void>(`${this.booksUrl}/progress`, {
bookId,
fileProgress
});
}
return this.http.post<void>(`${this.booksUrl}/progress`, {
bookId,
audiobookProgress: progress
});
return this.http.post<void>(`${this.booksUrl}/progress`, body).pipe(
tap(() => {
patchBookFieldsInCache(this.queryClient, [{bookId, fields: {audiobookProgress: progress}}]);
})
);
}
}
@@ -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;
@@ -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;
@@ -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);