Skip to content

Commit a89057f

Browse files
committed
Fix Tidal album artist duplication
The existing code returned all matching entries from included items, but Tidal API sometimes returns duplicates. Also the included items not necessarily reflect the same order as the relationships.
1 parent 98547d4 commit a89057f

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

providers/Tidal/__snapshots__/mod.test.ts.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -442,26 +442,26 @@ snapshot[`Tidal provider > release lookup > single by two artists (v2 API) 1`] =
442442
{
443443
artists: [
444444
{
445-
creditedName: "Bruno Mars",
445+
creditedName: "Lady Gaga",
446446
externalIds: [
447447
{
448-
id: "3658521",
448+
id: "3534754",
449449
provider: "tidal",
450450
type: "artist",
451451
},
452452
],
453-
name: "Bruno Mars",
453+
name: "Lady Gaga",
454454
},
455455
{
456-
creditedName: "Lady Gaga",
456+
creditedName: "Bruno Mars",
457457
externalIds: [
458458
{
459-
id: "3534754",
459+
id: "3658521",
460460
provider: "tidal",
461461
type: "artist",
462462
},
463463
],
464-
name: "Lady Gaga",
464+
name: "Bruno Mars",
465465
},
466466
],
467467
copyright: "© 2024 Interscope Records",

providers/Tidal/v2/lookup.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,20 @@ export class TidalV2ReleaseLookup extends ReleaseApiLookup<TidalProvider, Single
349349
resourceType: 'artists' | 'artworks' | 'providers',
350350
): T[] {
351351
const targetRels = rawRelease.data.relationships[relationship];
352-
const relatedIds = new Set(targetRels?.data?.map((item) => item.id));
352+
if (!targetRels) {
353+
return [];
354+
}
355+
const relatedIds = new Set(targetRels.data?.map((item) => item.id));
353356

354-
return rawRelease.included
355-
.filter((resource) => resource.type === resourceType && relatedIds.has(resource.id)) as T[];
357+
const items = new Map<string, T>();
358+
rawRelease.included.forEach((resource) => {
359+
if (resource.type === resourceType && relatedIds.has(resource.id)) {
360+
items.set(resource.id, resource as T);
361+
}
362+
});
363+
// Rely on the target relationship to get the correct number and order of related
364+
// items. Consider the case that an item was not actually returned in the includes.
365+
return targetRels.data.map((rel) => items.get(rel.id) || { id: rel.id } as T);
356366
}
357367

358368
private constructReleaseUrlFromRelease(release: ReleaseResource): URL {

0 commit comments

Comments
 (0)