Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lumen/src/recentMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ export function recentMediaTitles(rows: MediaRow[]): RecentMediaTitle[] {
}));
}
// Some recent-view-only accounts receive a deliberately limited /media projection
// without catalog titles. Reuse the existing authorized detail endpoint only
// for those rows; a failed lookup must never erase the entire recent card.
// without catalog titles, and a title saved before TMDB had a poster keeps no
// poster in the list. Reuse the existing authorized detail endpoint only for
// those rows; a failed lookup must never erase the entire recent card.
export async function enrichRecentMediaTitles(
cards: RecentMediaTitle[],
lookup: (card: RecentMediaTitle) => Promise<TitleResult>
Expand All @@ -47,18 +48,20 @@ export async function enrichRecentMediaTitles(
card.mediaType === 'movie'
? card.title === 'Untitled movie'
: card.name === 'Untitled series';
if (!missingName) return card;
if (!missingName && card.posterPath) return card;
try {
const detail = await lookup(card);
const resolvedTitle = detail.title || detail.name;
return {
...card,
title:
card.mediaType === 'movie'
? resolvedTitle || card.title
? (missingName && resolvedTitle) || card.title
: undefined,
name:
card.mediaType === 'tv' ? resolvedTitle || card.name : undefined,
card.mediaType === 'tv'
? (missingName && resolvedTitle) || card.name
: undefined,
posterPath: card.posterPath || detail.posterPath,
releaseDate: card.releaseDate || detail.releaseDate,
firstAirDate: card.firstAirDate || detail.firstAirDate,
Expand Down
38 changes: 37 additions & 1 deletion lumen/tests/recentMedia.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ test('complete catalog rows need no extra detail requests', async () => {
mediaType: i % 2 ? 'tv' : 'movie',
status: 5,
title: `Title ${i}`,
posterPath: `/poster/${i}.jpg`,
}));
let lookups = 0;
const titles = await enrichRecentMediaTitles(
Expand All @@ -86,7 +87,13 @@ test('missing restricted-view titles are enriched where permitted, not discarded
const cards = recentMediaTitles([
{ tmdbId: 51, mediaType: 'movie', status: 5 },
{ tmdbId: 52, mediaType: 'tv', status: 5 },
{ tmdbId: 53, mediaType: 'movie', status: 5, title: 'Already provided' },
{
tmdbId: 53,
mediaType: 'movie',
status: 5,
title: 'Already provided',
posterPath: '/b.jpg',
},
]);
const calls = [];
const result = await enrichRecentMediaTitles(cards, async (item) => {
Expand All @@ -107,3 +114,32 @@ test('missing restricted-view titles are enriched where permitted, not discarded
assert.equal(result[2].title, 'Already provided');
assert.equal(result.length, 3);
});

test('a titled card without a poster takes the poster from the title, and keeps its own title', async () => {
const cards = recentMediaTitles([
{ tmdbId: 61, mediaType: 'movie', status: 5, title: 'Fjord' },
{ tmdbId: 62, mediaType: 'tv', status: 5, title: 'Show' },
{ tmdbId: 63, mediaType: 'movie', status: 5, title: 'Lookup fails' },
]);
const calls = [];
const result = await enrichRecentMediaTitles(cards, async (item) => {
calls.push(item.id);
if (item.id === 63) throw Error('Detail endpoint unavailable');
return {
id: item.id,
mediaType: item.mediaType,
title: 'Other title',
name: 'Other name',
posterPath: `/${item.id}.jpg`,
};
});
assert.deepEqual(calls, [61, 62, 63]);
assert.deepEqual(
result.map((r) => [r.title ?? r.name, r.posterPath]),
[
['Fjord', '/61.jpg'],
['Show', '/62.jpg'],
['Lookup fails', undefined],
]
);
});
Loading