Skip to content

Instantly share code, notes, and snippets.

@kellnerd
Last active March 1, 2023 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellnerd/a256f7823cce154690fbb629167356e6 to your computer and use it in GitHub Desktop.
Save kellnerd/a256f7823cce154690fbb629167356e6 to your computer and use it in GitHub Desktop.
/**
* 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',
});
@kellnerd
Copy link
Author

kellnerd commented Mar 1, 2023

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment