Skip to content

Commit

Permalink
fix: link composers and authors
Browse files Browse the repository at this point in the history
  • Loading branch information
dvirtz committed Dec 22, 2024
1 parent 350609d commit 5ff98db
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 48 deletions.
28 changes: 7 additions & 21 deletions src/acum-work-import/import-album.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
zip,
} from 'rxjs';
import {Setter} from 'solid-js';
import {COMPOSER_LINK_TYPE_ID, LYRICIST_LINK_TYPE_ID, TRANSLATOR_LINK_TYPE_ID} from 'src/common/musicbrainz/constants';
import {addEditNote} from 'src/common/musicbrainz/edit-note';
import {trackRecordingState} from 'src/common/musicbrainz/track-recording-state';
import {albumUrl, Creator, Creators, IPBaseNumber, trackName, WorkVersion} from './acum';
Expand All @@ -26,7 +25,7 @@ import {addArrangerRelationship, addWriterRelationship} from './relationships';
import {AddWarning} from './ui/warnings';
import {workEditDataEqual} from './ui/work-edit-data';
import {WorkStateWithEditDataT} from './work-state';
import {addWork} from './works';
import {addWork, linkWriters} from './works';

export async function importAlbum(
albumId: string,
Expand All @@ -43,22 +42,6 @@ export async function importAlbum(
// map of promises so that we don't fetch the same artist multiple times
const artistCache = new Map<IPBaseNumber, Promise<ArtistT | null>>();

const linkWriters = async (
work: WorkT,
writers: ReadonlyArray<Creator> | undefined,
creators: Creators,
linkTypeId: number,
addWarning: AddWarning
) => {
await linkArtists(
artistCache,
writers,
creators,
(artist: ArtistT) => addWriterRelationship(work, artist, linkTypeId),
addWarning
);
};

const linkArrangers = async (
recording: RecordingT,
arrangers: ReadonlyArray<Creator> | undefined,
Expand Down Expand Up @@ -97,9 +80,12 @@ export async function importAlbum(
]): Promise<WorkStateWithEditDataT> => {
const work = workState.work;
const addWarning = addTrackWarning(track);
await linkWriters(work, track.authors, track.creators, LYRICIST_LINK_TYPE_ID, addWarning);
await linkWriters(work, track.composers, track.creators, COMPOSER_LINK_TYPE_ID, addWarning);
await linkWriters(work, track.translators, track.creators, TRANSLATOR_LINK_TYPE_ID, addWarning);
await linkWriters(
artistCache,
track,
(artist: ArtistT, linkTypeId: number) => addWriterRelationship(work, artist, linkTypeId),
addWarning
);
await linkArrangers(recording, track.arrangers, track.creators, addWarning);
return workState;
};
Expand Down
33 changes: 9 additions & 24 deletions src/acum-work-import/import-work.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import {from, lastValueFrom, switchMap, take, tap} from 'rxjs';
import {asyncTap} from 'src/common/lib/asyncTap';
import {COMPOSER_LINK_TYPE_ID, LYRICIST_LINK_TYPE_ID, TRANSLATOR_LINK_TYPE_ID} from 'src/common/musicbrainz/constants';
import {addEditNote} from 'src/common/musicbrainz/edit-note';
import {Creator, Creators, IPBaseNumber, workUrl, workVersions} from './acum';
import {linkArtists} from './artists';
import {IPBaseNumber, workUrl, workVersions} from './acum';
import {addWriterRelationship} from './relationships';
import {AddWarning} from './ui/warnings';
import {workEditData} from './ui/work-edit-data';
import {createWork} from './works';
import {createWork, linkWriters} from './works';

export async function importWork(workId: string, form: HTMLFormElement, addWarning: AddWarning) {
// map of promises so that we don't fetch the same artist multiple times
Expand All @@ -19,21 +17,6 @@ export async function importWork(workId: string, form: HTMLFormElement, addWarni
name: form.querySelector('[name="edit-work.name"]')?.getAttribute('value') || '',
});

const linkWriters = async (
work: WorkT,
writers: ReadonlyArray<Creator> | undefined,
creators: Creators,
linkTypeId: number
) => {
await linkArtists(
artistCache,
writers,
creators,
(artist: ArtistT) => addWriterRelationship(work, artist, linkTypeId),
addWarning
);
};

const versions = await workVersions(workId);
if (!versions) {
alert(`failed to find work ID ${workId}`);
Expand Down Expand Up @@ -89,11 +72,13 @@ export async function importWork(workId: string, form: HTMLFormElement, addWarni
setInput(form, `attributes.${index}.value`, attr.value, addWarning);
});
}),
switchMap(async ([track]) => {
await linkWriters(work, track.authors, track.creators, LYRICIST_LINK_TYPE_ID);
await linkWriters(work, track.composers, track.creators, COMPOSER_LINK_TYPE_ID);
await linkWriters(work, track.translators, track.creators, TRANSLATOR_LINK_TYPE_ID);
return track;
asyncTap(async ([track]) => {
await linkWriters(
artistCache,
track,
(artist: ArtistT, linkTypeId: number) => addWriterRelationship(work, artist, linkTypeId),
addWarning
);
}),
tap(() => addEditNote(`Imported from ${workUrl(workId)}`, form.ownerDocument))
)
Expand Down
43 changes: 40 additions & 3 deletions src/acum-work-import/works.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import {compareNumbers} from 'src/common/lib/compare';
import {head} from 'src/common/lib/head';
import {compareTargetTypeWithGroup} from 'src/common/musicbrainz/compare';
import {RECORDING_OF_LINK_TYPE_ID, REL_STATUS_ADD, REL_STATUS_REMOVE} from 'src/common/musicbrainz/constants';
import {
COMPOSER_LINK_TYPE_ID,
LYRICIST_LINK_TYPE_ID,
RECORDING_OF_LINK_TYPE_ID,
REL_STATUS_ADD,
REL_STATUS_REMOVE,
TRANSLATOR_LINK_TYPE_ID,
} from 'src/common/musicbrainz/constants';
import {iterateRelationshipsInTargetTypeGroup} from 'src/common/musicbrainz/type-group';
import {WorkVersion} from './acum';
import {trackName, WorkVersion} from './acum';
import {linkArtists} from './artists';
import {createRelationshipState} from './relationships';
import {AddWarning} from './ui/warnings';
import {workEditData} from './ui/work-edit-data';
Expand Down Expand Up @@ -53,7 +61,7 @@ async function createNewWork(track: WorkVersion, recordingState: MediumRecording
const newWork = createWork({
_fromBatchCreateWorksDialog: true,
id: MB.relationshipEditor.getRelationshipStateId(),
name: track.workHebName,
name: trackName(track),
});
workCache.set(track.fullWorkId, newWork);
return newWork;
Expand Down Expand Up @@ -120,3 +128,32 @@ export function createWork(attributes: Partial<WorkT>): WorkT {
...attributes,
});
}

export async function linkWriters(
artistCache: Map<string, Promise<ArtistT | null>>,
track: WorkVersion,
doLink: (artist: ArtistT, linkTypeID: number) => void,
addWarning: (message: string) => Set<string>
) {
await linkArtists(
artistCache,
[...(track.authors ?? []), ...(track.composersAndAuthors ?? [])],
track.creators,
artist => doLink(artist, LYRICIST_LINK_TYPE_ID),
addWarning
);
await linkArtists(
artistCache,
[...(track.composers ?? []), ...(track.composersAndAuthors ?? [])],
track.creators,
artist => doLink(artist, COMPOSER_LINK_TYPE_ID),
addWarning
);
await linkArtists(
artistCache,
track.translators,
track.creators,
artist => doLink(artist, TRANSLATOR_LINK_TYPE_ID),
addWarning
);
}

0 comments on commit 5ff98db

Please sign in to comment.