-
-
Save kellnerd/a256f7823cce154690fbb629167356e6 to your computer and use it in GitHub Desktop.
Debugging a regression: https://github.com/kellnerd/musicbrainz-scripts/issues/34
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Fetches the core entity with the given MBID from the internal API ws/js. | |
* @param {string} gid MBID of the entity. | |
* @param {string[]} inc Include parameters for ws/js. | |
* @returns {Promise<CoreEntityT>} | |
*/ | |
async function fetchCoreEntity(gid, inc = []) { | |
const query = new URLSearchParams({ inc: inc.join(' ') }); | |
const result = await fetch(`/ws/js/entity/${gid}?${query}`); | |
return result.json(); | |
} | |
/** | |
* @param {CoreEntityTypeT} sourceType | |
* @param {CoreEntityTypeT} targetType | |
*/ | |
function isRelBackward(sourceType, targetType) { | |
return sourceType > targetType; | |
} | |
// Taken from root/static/scripts/relationship-editor/hooks/useRelationshipDialogContent.js | |
const RELATIONSHIP_DEFAULTS = { | |
_lineage: [], | |
_original: null, | |
_status: 1, // add relationship | |
attributes: null, | |
begin_date: null, | |
editsPending: false, | |
end_date: null, | |
ended: false, | |
entity0_credit: '', | |
entity1_credit: '', | |
id: null, | |
linkOrder: 0, | |
linkTypeID: null, | |
}; | |
/** | |
* Creates a relationship between the given source and target entity. | |
* @param {Partial<RelationshipT> & { source?: CoreEntityT, target: CoreEntityT, batchSelectionCount?: number }} options | |
* @param {CoreEntityT} [options.source] Source entity, defaults to the currently edited entity. | |
* @param {CoreEntityT} options.target Target entity. | |
* @param {number} [options.batchSelectionCount] Batch-edit all selected entities which have the same type as the source. | |
* The source entity only acts as a placeholder in this case. | |
* @param {Partial<RelationshipT>} props Relationship properties. | |
*/ | |
function createRelationship({ | |
source = MB.relationshipEditor.state.entity, | |
target, | |
batchSelectionCount = null, | |
...props | |
}) { | |
const backward = isRelBackward(source.entityType, target.entityType); | |
MB.relationshipEditor.dispatch({ | |
type: 'update-relationship-state', | |
sourceEntity: source, | |
batchSelectionCount, | |
creditsToChangeForSource: '', | |
creditsToChangeForTarget: '', | |
newRelationshipState: { | |
...RELATIONSHIP_DEFAULTS, | |
entity0: backward ? target : source, | |
entity1: backward ? source : target, | |
id: MB.relationshipEditor.getRelationshipStateId(), | |
...props, | |
}, | |
oldRelationshipState: null, | |
}); | |
} | |
/** | |
* Creates the same relationship between each of the selected source entities and the given target entity. | |
* @param {import('weight-balanced-tree').ImmutableTree<CoreEntityT>} sourceSelection Selected source entities. | |
* @param {CoreEntityT} target Target entity. | |
* @param {RelationshipProps} props Relationship properties. | |
*/ | |
function batchCreateRelationships(sourceSelection, target, props) { | |
return createRelationship({ | |
source: sourceSelection.value, // use the root node entity as a placeholder | |
target, | |
batchSelectionCount: sourceSelection.size, | |
...props, | |
}); | |
} | |
/* | |
* Run the following code on any release relationship editor page to add the example label | |
* as phongraphic copyright holder of all selected recordings. | |
*/ | |
let targetLabel = await fetchCoreEntity('e5d95d7c-efec-47f6-8644-aedcc661d03e'); | |
let selectedRecordings = MB.relationshipEditor.state.selectedRecordings ; | |
batchCreateRelationships(selectedRecordings, targetLabel, { | |
linkTypeID: 867, | |
entity0_credit: 'Test', | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Without selecting any recordings I get at least an error message (as expected):
Uncaught (in promise) TypeError: sourceSelection is null
But once I have selected recordings, the code silently fails and tries to perform a sentry request (which I've blocked after the first time, because the contained error message
e is undefined
is useless).