Skip to content
Merged
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
12 changes: 6 additions & 6 deletions providers/Tidal/__snapshots__/mod.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -442,26 +442,26 @@ snapshot[`Tidal provider > release lookup > single by two artists (v2 API) 1`] =
{
artists: [
{
creditedName: "Bruno Mars",
creditedName: "Lady Gaga",
externalIds: [
{
id: "3658521",
id: "3534754",
Comment thread
kellnerd marked this conversation as resolved.
provider: "tidal",
type: "artist",
},
],
name: "Bruno Mars",
name: "Lady Gaga",
},
{
creditedName: "Lady Gaga",
creditedName: "Bruno Mars",
externalIds: [
{
id: "3534754",
id: "3658521",
provider: "tidal",
type: "artist",
},
],
name: "Lady Gaga",
name: "Bruno Mars",
},
],
copyright: "© 2024 Interscope Records",
Expand Down
16 changes: 13 additions & 3 deletions providers/Tidal/v2/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,20 @@ export class TidalV2ReleaseLookup extends ReleaseApiLookup<TidalProvider, Single
resourceType: 'artists' | 'artworks' | 'providers',
): T[] {
const targetRels = rawRelease.data.relationships[relationship];
const relatedIds = new Set(targetRels?.data?.map((item) => item.id));
if (!targetRels) {
return [];
}
const relatedIds = new Set(targetRels.data?.map((item) => item.id));

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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we currently need the fallback value at all? As far as I can see it doesn't matter whether we return the incomplete object or undefined, all callers of getRelatedItems aren't prepared to handle either case.
That's the big downside of the type assertion here, you don't notice such issues until they happen.

So far I've only seen missing resources for track artists, which has a completely separate implementation in getTrackArtists which doesn't call getRelatedItems.
In my local files I've even left a note on this function which mentions the possibility of the current issue, but I hadn't addressed it so far:

TODO: Track AC conversion is inconsistent with release AC conversion (which might be in the wrong order since it filters the existing resources instead of mapping the IDs to resources).
Store artistMap as this.artistMap?

In the long term we probably want to properly type the getRelatedItems helper or abandon it if there is no clean way to do it. I like the approach with the artistMap that we do for tracks, maybe a generic this.resourceMap and a suitable helper function is the way to go?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly because of the concerns that also release artists might be missing. I haven't seen such a case, but I think it could happen.

But yes, we could refactor this to a similar system then as used for tracks. Actually the way tracks handle this was added later, and it was done separated as the existing getRelatedItems didn't work for this use case.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added #185 to track this.

}

private constructReleaseUrlFromRelease(release: ReleaseResource): URL {
Expand Down