I’m trying to write a userscript (my first) that allows to copy-paste relationships between different, separated recordings, but I’m having some trouble understanding how to add relationships in the editor (the “paste” action)
By looking at loujine’s “Clone recording relations onto other recordings” userscript, it seems that I need to use the MB.relationshipEditor.dispatch()
function, but that doesn’t appear to be working.
This is the my current code mostly “borrowed” from loujine’s aforementioned script:
originalRecording.relationships.forEach(relationship => {
MB.relationshipEditor.dispatch({
type: 'update-relationship-state',
sourceEntity: originalRecording,
batchSelectionCount: null,
creditsToChangeForSource: '',
creditsToChangeForTarget: '',
oldRelationshipState: null,
batchSelectionCount: null,
newRelationshipState: {
_lineage: [],
_original: null,
_status: 0,
attributes: null,
begin_date: null,
editsPending: false,
end_date: null,
ended: false,
entity0_credit: '',
entity1_credit: '',
id: null,
linkOrder: 0,
linkTypeID: null,
_status: 1,
entity0: relationship.backward ? relationship.target : originalRecording,
entity1: relationship.backward ? originalRecording : relationship.target,
entity0_credit: relationship.entity0_credit,
entity1_credit: relationship.entity1_credit,
begin_date: relationship.begin_date,
end_date: relationship.end_date,
ended: relationship.ended,
attributes: createAttributeTree(relationship.attributes),
id: MB.relationshipEditor.getRelationshipStateId(),
linkTypeID: relationship.linkTypeID,
}
});
});
originalRecording
is the object returned by MB.relationshipEditor.state.entity
for the copied recording.
That createAttributeTree()
function is this
function createAttributeTree(attributes) {
return MB.tree.fromDistinctAscArray(attributes.map((attribute) => {
var attributeType = MB.linkedEntities.link_attribute_type[attribute.type.gid];
return {
...attribute,
type: attributeType,
typeID: attributeType.id,
};
})
);
}
The paste function gets executed since a console.log()
that I put at the end works but no relationship appears in MusicBrainz’s editor
Does anyone have any suggestions as to why it’s not working?
Thanks in advance!