From 9ba3745b943c1a821cd5f7bb0edea7381b23f6e5 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Mon, 15 Aug 2016 04:38:04 -0400 Subject: [PATCH] Don't clear md5/mtime in item JSON if skipped in patch mode The client skips synced storage properties (md5, mtime) when uploading items to ZFS-enabled libraries, but since the API returns JSON with those values included after writes, they do get saved to the sync cache. If the local attachment is then modified and the client generates a diff from the cached version with those properties skipped, they'll be included in the patch JSON as empty strings in order to clear them. This changes Zotero.Item::toJSON() to skip those properties in patch mode as well. This fixes a sync error ("Cannot change 'md5' directly in group library") when a group attachment is updated locally. --- chrome/content/zotero/xpcom/data/item.js | 7 ++++++- test/tests/itemTest.js | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index 37fadfde7f..20b5dd18b2 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -4237,7 +4237,12 @@ Zotero.Item.prototype.toJSON = function (options = {}) { obj.dateModified = Zotero.Date.sqlToISO8601(this.dateModified); } - return this._postToJSON(env); + var json = this._postToJSON(env); + if (options.skipStorageProperties) { + delete json.md5; + delete json.mtime; + } + return json; } diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js index 580b9e672d..c6cbf5ac30 100644 --- a/test/tests/itemTest.js +++ b/test/tests/itemTest.js @@ -1192,6 +1192,28 @@ describe("Zotero.Item", function () { var json = note.toJSON({ patchBase }); assert.isFalse(json.parentItem); }); + + it("shouldn't clear storage properties from original in .skipStorageProperties mode", function* () { + var item = new Zotero.Item('attachment'); + item.attachmentLinkMode = 'imported_file'; + item.attachmentFilename = 'test.txt'; + item.attachmentContentType = 'text/plain'; + item.attachmentCharset = 'utf-8'; + item.attachmentSyncedModificationTime = 1234567890000; + item.attachmentSyncedHash = '18d21750c8abd5e3afa8ea89e3dfa570'; + var patchBase = item.toJSON({ + syncedStorageProperties: true + }); + item.setNote("Test"); + var json = item.toJSON({ + patchBase, + skipStorageProperties: true + }); + Zotero.debug(json); + assert.equal(json.note, "Test"); + assert.notProperty(json, "md5"); + assert.notProperty(json, "mtime"); + }); }) })