Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance language support and link relationships for works #36

Merged
merged 3 commits into from
Dec 22, 2024
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
5 changes: 3 additions & 2 deletions src/acum-work-import/acum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ export async function workISWCs(workID: string) {
.map(formatISWC);
}

export function searchName(name: string) {
return /[א-ת]/.test(name) ? 'workHebName' : 'workEngName';
export function trackName(track: WorkVersion) {
return workLanguage(track) == WorkLanguage.Hebrew ? track.workHebName : track.workEngName;
}

export enum EssenceType {
Expand All @@ -153,6 +153,7 @@ export function essenceType(track: WorkVersion): EssenceType {

export enum WorkLanguage {
Hebrew = '1',
Foreign = '2',
/** @knipignore */
Unknown = '-1',
}
Expand Down
42 changes: 15 additions & 27 deletions src/acum-work-import/import-album.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,22 @@ 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, searchName, WorkVersion} from './acum';
import {albumUrl, Creator, Creators, IPBaseNumber, trackName, WorkVersion} from './acum';
import {albumInfo} from './albums';
import {linkArtists} from './artists';
import {addArrangerRelationship, addWriterRelationship} from './relationships';
import {AddWarning, ClearWarnings} from './ui/warnings';
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,
addWarning: AddWarning,
clearWarnings: ClearWarnings,
setProgress: Setter<readonly [number, string]>
): Promise<boolean> {
clearWarnings();
setProgress([0, 'Loading album info']);

const addTrackWarning = (track: WorkVersion) => (warning: string) =>
Expand All @@ -45,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 @@ -99,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 Expand Up @@ -130,8 +114,12 @@ export async function importAlbum(
map(([track, recordingState]) => [track, recordingState, addTrackWarning(track)] as const),
tap(([track, recordingState, addWarning]) => {
const recording = recordingState.recording;
if (track[searchName(recording.name)] != recording.name) {
addWarning(`Work name of ${recording.name} is different from recording name, please verify`);
if (trackName(track) != recording.name) {
if (trackName(track).toLowerCase() == recording.name.toLowerCase()) {
track.workEngName = recording.name;
} else {
addWarning(`Work name of ${recording.name} is different from recording name, please verify`);
}
}
}),
mergeMap(
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
2 changes: 1 addition & 1 deletion src/acum-work-import/ui/release-editor-ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function AcumImporter(props: {recordingCheckboxes: NodeListOf<HTMLInputElement>}
return;
}
try {
await tryImportWorks(albumId, addWarning, clearWarnings, setProgress);
await tryImportWorks(albumId, addWarning, setProgress);
setWorksPending(true);
} catch (err) {
console.error(err);
Expand Down
1 change: 0 additions & 1 deletion src/acum-work-import/ui/warnings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export function useWarnings() {
}

export type AddWarning = ReturnType<typeof useWarnings>['addWarning'];
export type ClearWarnings = ReturnType<typeof useWarnings>['clearWarnings'];

function Warnings() {
const {state} = useWarnings();
Expand Down
6 changes: 4 additions & 2 deletions src/acum-work-import/ui/work-edit-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {mergeArrays} from 'src/common/lib/merge-arrays';
import {LANGUAGE_ZXX_ID} from 'src/common/musicbrainz/constants';
import {fetchEditParams, urlFromMbid} from 'src/common/musicbrainz/edits';
import {workAttributeTypes, workLanguages, workTypes} from 'src/common/musicbrainz/type-info';
import {essenceType, EssenceType, workISWCs, workLanguage, WorkLanguage, WorkVersion} from '../acum';
import {essenceType, EssenceType, trackName, workISWCs, workLanguage, WorkLanguage, WorkVersion} from '../acum';
import {WorkStateWithEditDataT} from '../work-state';
import {AddWarning} from './warnings';

Expand Down Expand Up @@ -73,7 +73,7 @@ export async function workEditData(
return {
originalEditData,
editData: {
name: track.workHebName,
name: trackName(track),
comment: originalEditData.comment,
type_id: [EssenceType.Song, EssenceType.ChoirSong].includes(essenceType(track))
? (Object.values(await workTypes).find(workType => workType.name === 'Song')?.id ?? null)
Expand All @@ -93,6 +93,8 @@ export async function workEditData(
return Object.values(await workLanguages)
.filter(language => language.name === 'Hebrew')
.map(language => language.id);
case WorkLanguage.Foreign:
return [];
default:
addWarning(`Unknown language ${track.workLanguage}`);
return [];
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
);
}
Loading