How do I add relationships to a recording from a userscript?

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!

1 Like

The good news is that such a script already exists! :wink:

Isn’t it the one you mentioned?

:thinking:

Unless I’m mistaken, it’s not. The script you mentioned allows to copy relationships between recordings in the same release. I’m trying to copy onto recordings that are not in the same release

An example might be these two

They are DJ-mixed recordings included in different releases so they can’t be merged, but are effectively the same song so relationships like vocals and production are the same

1 Like

You can clone recording relations from one recording to another. However, you need to be in the relationship editor of a release to do this. Simply copy and paste the recording MBID from the source recording into the release that includes the target recording. This function is found under “Clone recording relations to selected recordings”: “OR recording link.” It works.

Update: Don’t forget to check the box next to target recording.

4 Likes

That’s the solution, thanks a lot!
I didn’t looked deep enough into that userscript :sweat_smile:

3 Likes