Skip to content

Commit ae4c229

Browse files
committed
connector server: handle metadata edits from popup
Handle updates to metadata sent by the connector popup. Needed for: zotero/zotero-connectors#615
1 parent 5a347f4 commit ae4c229

2 files changed

Lines changed: 122 additions & 0 deletions

File tree

chrome/content/zotero/xpcom/server/server_connector.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,18 @@ Zotero.Server.Connector.UpdateSession.prototype = {
776776

777777
await session.update(data.target, tags, note);
778778

779+
if (data.updatedMetadata) {
780+
let item = session.getItemByConnectorKey(data.updatedMetadata.id);
781+
if (item) {
782+
for (let field of data.updatedMetadata.fields) {
783+
if (field.name === 'itemType') continue;
784+
item.setField(field.name, field.value);
785+
}
786+
item.setCreators(data.updatedMetadata.creators || []);
787+
await item.saveTx();
788+
}
789+
}
790+
779791
return [200, "application/json", JSON.stringify({})];
780792
}
781793
};
@@ -1114,6 +1126,7 @@ Zotero.Server.Connector.Ping.prototype = {
11141126
downloadAssociatedFiles: Zotero.Prefs.get("downloadAssociatedFiles"),
11151127
supportsAttachmentUpload: true,
11161128
supportsTagsAutocomplete: true,
1129+
supportsMetadataUpdates: true,
11171130
googleDocsAddNoteEnabled: true,
11181131
googleDocsAddAnnotationEnabled: true,
11191132
canUserAddNote: true,

test/tests/server_connectorTest.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,115 @@ describe("Connector Server", function () {
10741074
assert.equal(note.getNote(), "Test note");
10751075
});
10761076

1077+
it("should update metadata of item saved via /saveItems", async function () {
1078+
var collection = await createDataObject('collection');
1079+
await select(win, collection);
1080+
1081+
const id = Zotero.Utilities.randomString();
1082+
var sessionID = Zotero.Utilities.randomString();
1083+
var body = {
1084+
sessionID,
1085+
items: [
1086+
{
1087+
itemType: "newspaperArticle",
1088+
title: "Title",
1089+
id,
1090+
creators: [
1091+
{
1092+
firstName: "First",
1093+
lastName: "Last",
1094+
creatorType: "author"
1095+
}
1096+
]
1097+
}
1098+
],
1099+
uri: "http://example.com"
1100+
};
1101+
1102+
var reqPromise = httpRequest(
1103+
'POST',
1104+
connectorServerPath + "/connector/saveItems",
1105+
{
1106+
headers: {
1107+
"Content-Type": "application/json"
1108+
},
1109+
body: JSON.stringify(body)
1110+
}
1111+
);
1112+
1113+
var ids = await waitForItemEvent('add');
1114+
var item = Zotero.Items.get(ids[0]);
1115+
var req = await reqPromise;
1116+
assert.equal(req.status, 201);
1117+
assert.equal(item.getField('title'), "Title");
1118+
1119+
// Update metadata
1120+
req = await httpRequest(
1121+
'POST',
1122+
connectorServerPath + "/connector/updateSession",
1123+
{
1124+
headers: {
1125+
"Content-Type": "application/json"
1126+
},
1127+
body: JSON.stringify({
1128+
sessionID,
1129+
target: collection.treeViewID,
1130+
tags: "",
1131+
note: "",
1132+
updatedMetadata: {
1133+
id,
1134+
fields: [
1135+
{
1136+
name: "itemType",
1137+
label: "Item Type",
1138+
value: "newspaperArticle"
1139+
},
1140+
{
1141+
name: "title",
1142+
label: "Title",
1143+
value: "Updated Title"
1144+
},
1145+
{
1146+
name: "abstractNote",
1147+
label: "Abstract",
1148+
value: "Updated abstract"
1149+
},
1150+
{
1151+
name: "date",
1152+
label: "Date",
1153+
value: "2026-04-08"
1154+
},
1155+
{
1156+
name: "url",
1157+
label: "URL",
1158+
value: "http://example.com/updated"
1159+
}
1160+
],
1161+
creators: [
1162+
{
1163+
firstName: "New First",
1164+
lastName: "New Last",
1165+
creatorType: "author"
1166+
}
1167+
]
1168+
}
1169+
})
1170+
}
1171+
);
1172+
1173+
assert.equal(req.status, 200);
1174+
assert.equal(item.getField('title'), "Updated Title");
1175+
assert.equal(item.getField('abstractNote'), "Updated abstract");
1176+
assert.equal(item.getField('date'), "2026-04-08");
1177+
assert.equal(item.getField('url'), "http://example.com/updated");
1178+
var creators = item.getCreators();
1179+
assert.equal(creators.length, 1);
1180+
assert.equal(creators[0].firstName, "New First");
1181+
assert.equal(creators[0].lastName, "New Last");
1182+
// Verify collection is preserved
1183+
assert.isTrue(collection.hasItem(item.id));
1184+
});
1185+
10771186
it("should update collections and tags of a PDF saved via /saveStandaloneAttachment", async function () {
10781187
const sessionID = Zotero.Utilities.randomString();
10791188

0 commit comments

Comments
 (0)