fix(metadata): show the translation a search matched on Open Library and Hardcover (#1451)

Searching a translated title on Open Library or Hardcover returned the
original-language work. "Der Weg der Könige" came back as "The Way of
Kings" (2010), and that English title then went into the release search
and the download name. Both providers keep one record per work, titled
after the original, and list translations underneath it.

#483 already searches releases per language with Hardcover's localized
titles, which fixed the release side of #360 when the matching language
is selected. This PR fixes the search result itself, so the book a user
picks is the translation they searched for, and it covers Open Library,
the default provider, which has no titles_by_language.

- Open Library: request the matched edition (editions.*) and prefer its
title, ISBNs, cover, language and publication year when its title
differs from the work's.
- Hardcover: use the alternative title named in the hit's highlights
(alternative_titles, best index first), without the original's subtitle.
A hit whose title also matched keeps the work title.
- /api/releases: when the modal sends a title that differs from the book
record's, it becomes search_title. Before, Hardcover's search_title from
the original won, so picking "Kinder des Nebels" still searched "The
Final Empire" first.
- Release cache: keyed on title and language as well as the provider ID.
All translations share that ID, so opening "The Way of Kings" after "Der
Weg der Könige" showed the German releases until a reload.

Known limitations: neither provider has descriptions per edition, so the
description stays the work's (usually English). Hardcover's search hits
carry no year or language for alternative titles, so those stay the
work's; Open Library's do not. Google Books is unaffected, since it
already returns one volume per edition.

Tests: Open Library edition parsing, Hardcover alternative-title
matching, the /api/releases title override, and the release cache, each
written to fail first. The Python suite passes apart from the 9
test_entrypoint_permissions.py tests, which fail the same way on an
unmodified main on macOS. The frontend typecheck, lint, format check and
unit tests pass. Checked on a live instance with Open Library, Hardcover
and Google Books.

Refs #360, #483

Disclaimer:
This was developed with the help of AI but reviewed and tested as well
as deployed on my server by a real human.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
HeihoffJandClaude Opus 5.5 authored and GitHub committed 2026-10-20 01:08:45 -04:00
1 parent 45548687b7
commit 756bdec19b
9 files changed
+504 -30

No files matched your search

@@ -202,6 +202,18 @@ export function useReleaseSearchSession(
[],
);
// Editions of one work can share `provider_id`; the cache tells them apart by title
// and language (see releaseCache).
const cacheBook = useMemo(
() => ({
provider: book.provider,
provider_id: book.provider_id,
title: book.title,
language: book.language,
}),
[book.language, book.provider, book.provider_id, book.title],
);
const fetchReleaseResults = useCallback(
async (
tabName: string,
@@ -245,7 +257,7 @@ export function useReleaseSearchSession(
useFilters && supportsIndexerFilter && indexerFilter.length > 0 ? indexerFilter : undefined;
if (!expandSearch) {
const cached = getCachedReleases(provider, bookId, tabName, contentType);
const cached = getCachedReleases(cacheBook, tabName, contentType);
if (cached) {
setReleasesBySource((prev) => ({ ...prev, [tabName]: cached }));
initializeIndexerFilterForTab(tabName, cached);
@@ -293,7 +305,7 @@ export function useReleaseSearchSession(
};
});
} else {
setCachedReleases(provider, bookId, tabName, contentType, response);
setCachedReleases(cacheBook, tabName, contentType, response);
setReleasesBySource((prev) => ({ ...prev, [tabName]: response }));
}
@@ -312,6 +324,7 @@ export function useReleaseSearchSession(
book.provider_id,
book.title,
bookLanguages,
cacheBook,
clearSearchStatusForTab,
contentType,
defaultLanguages,
@@ -494,7 +507,7 @@ export function useReleaseSearchSession(
const supportsIndexerFilter =
releasesBySource[activeTab]?.column_config?.supported_filters?.includes('indexer') ?? false;
invalidateCachedReleases(book.provider, book.provider_id, activeTab, contentType);
invalidateCachedReleases(cacheBook, activeTab, contentType);
setExpandedBySource((prev) => {
const next = { ...prev };
delete next[activeTab];
@@ -520,6 +533,7 @@ export function useReleaseSearchSession(
activeTab,
book.provider,
book.provider_id,
cacheBook,
contentType,
fetchReleaseResults,
releasesBySource,
@@ -536,7 +550,7 @@ export function useReleaseSearchSession(
}
for (const tab of allTabs) {
invalidateCachedReleases(book.provider, book.provider_id, tab.name, contentType);
invalidateCachedReleases(cacheBook, tab.name, contentType);
}
setExpandedBySource({});
@@ -552,6 +566,7 @@ export function useReleaseSearchSession(
allTabs,
book.provider,
book.provider_id,
cacheBook,
contentType,
fetchReleaseResults,
manualQuery,
@@ -0,0 +1,53 @@
import { describe, it, expect } from 'vitest';
import type { ReleasesResponse } from '../types/index';
import {
getCachedReleases,
invalidateCachedReleases,
setCachedReleases,
} from '../utils/releaseCache';
// Open Library reports every translation of a book under one work ID, so the
// German and the English edition of the same book share `provider_id`.
const germanEdition = {
provider: 'openlibrary',
provider_id: 'OL15358691W',
title: 'Der Weg der Könige',
language: 'ger',
};
const englishEdition = {
provider: 'openlibrary',
provider_id: 'OL15358691W',
title: 'The Way of Kings',
language: 'eng',
};
const response = (title: string): ReleasesResponse => ({
releases: [],
book: { provider: 'openlibrary', provider_id: 'OL15358691W', title },
sources_searched: ['prowlarr'],
});
describe('releaseCache', () => {
it('keeps editions of the same work apart', () => {
setCachedReleases(germanEdition, 'prowlarr', 'ebook', response('Der Weg der Könige'));
expect(getCachedReleases(englishEdition, 'prowlarr', 'ebook')).toBeNull();
expect(getCachedReleases(germanEdition, 'prowlarr', 'ebook')).toEqual(
response('Der Weg der Könige'),
);
});
it('invalidates only the edition it was given', () => {
setCachedReleases(germanEdition, 'prowlarr', 'audiobook', response('Der Weg der Könige'));
setCachedReleases(englishEdition, 'prowlarr', 'audiobook', response('The Way of Kings'));
invalidateCachedReleases(germanEdition, 'prowlarr', 'audiobook');
expect(getCachedReleases(germanEdition, 'prowlarr', 'audiobook')).toBeNull();
expect(getCachedReleases(englishEdition, 'prowlarr', 'audiobook')).toEqual(
response('The Way of Kings'),
);
});
});
+24 -18
View File
@@ -1,17 +1,26 @@
import type { ReleasesResponse } from '../types';
import type { Book, ReleasesResponse } from '../types';
/** The book fields that identify one edition's release search. */
export type ReleaseCacheBook = Pick<Book, 'provider' | 'provider_id' | 'title' | 'language'>;
// Module-level cache for release search results
// Key format: `${provider}:${provider_id}:${source}:${contentType}`
// Key format: `${provider}:${provider_id}:${language}:${title}:${source}:${contentType}`
// This persists across modal open/close cycles
const releaseCache = new Map<string, ReleasesResponse>();
function getCacheKey(
provider: string,
providerId: string,
source: string,
contentType: string,
): string {
return `${provider}:${providerId}:${source}:${contentType}`;
// A provider can report every edition of a book under one ID - Open Library uses the
// work ID for "The Way of Kings" and its German translation "Der Weg der Könige" alike -
// and each edition searches for releases by its own title and language. Keying on the
// ID alone would serve one edition's cached releases to the other.
function getCacheKey(book: ReleaseCacheBook, source: string, contentType: string): string {
return [
book.provider,
book.provider_id,
book.language ?? '',
book.title,
source,
contentType,
].join(':');
}
// Default cache TTL (5 minutes) - sources can override via column_config.cache_ttl_seconds
@@ -19,12 +28,11 @@ const DEFAULT_CACHE_TTL_MS = 5 * 60 * 1000;
const cacheTimestamps = new Map<string, number>();
export function getCachedReleases(
provider: string,
providerId: string,
book: ReleaseCacheBook,
source: string,
contentType: string,
): ReleasesResponse | null {
const key = getCacheKey(provider, providerId, source, contentType);
const key = getCacheKey(book, source, contentType);
const timestamp = cacheTimestamps.get(key);
const cached = releaseCache.get(key);
@@ -49,24 +57,22 @@ export function getCachedReleases(
}
export function setCachedReleases(
provider: string,
providerId: string,
book: ReleaseCacheBook,
source: string,
contentType: string,
data: ReleasesResponse,
): void {
const key = getCacheKey(provider, providerId, source, contentType);
const key = getCacheKey(book, source, contentType);
releaseCache.set(key, data);
cacheTimestamps.set(key, Date.now());
}
export function invalidateCachedReleases(
provider: string,
providerId: string,
book: ReleaseCacheBook,
source: string,
contentType: string,
): void {
const key = getCacheKey(provider, providerId, source, contentType);
const key = getCacheKey(book, source, contentType);
releaseCache.delete(key);
cacheTimestamps.delete(key);
}