commit 0d8643087a3b39c929328288e5b8269a76ad971e
parent ba91a2ea526fa0b1e0594b4b70916f3a282f56a1
Author: Dan Stillman <dstillman@zotero.org>
Date: Wed, 1 Mar 2017 23:34:11 -0500
Update relations using local user key when first setting sync user
In case items are merged before the first sync
Diffstat:
4 files changed, 30 insertions(+), 4 deletions(-)
diff --git a/chrome/content/zotero/xpcom/data/relations.js b/chrome/content/zotero/xpcom/data/relations.js
@@ -165,14 +165,14 @@ Zotero.Relations = new function () {
};
- this.updateUser = Zotero.Promise.coroutine(function* (toUserID) {
- var fromUserID = Zotero.Users.getCurrentUserID();
+ this.updateUser = Zotero.Promise.coroutine(function* (fromUserID, toUserID) {
if (!fromUserID) {
fromUserID = "local/" + Zotero.Users.getLocalUserKey();
}
if (!toUserID) {
throw new Error("Invalid target userID " + toUserID);
}
+
Zotero.DB.requireTransaction();
for (let type of _types) {
let sql = `SELECT DISTINCT object FROM ${type}Relations WHERE object LIKE ?`;
diff --git a/chrome/content/zotero/xpcom/sync/syncLocal.js b/chrome/content/zotero/xpcom/sync/syncLocal.js
@@ -169,6 +169,10 @@ Zotero.Sync.Data.Local = {
}
if (!lastUserID) {
yield Zotero.Users.setCurrentUserID(userID);
+
+ // Replace local user key with libraryID, in case duplicates were merged before the
+ // first sync
+ yield Zotero.Relations.updateUser(null, userID);
}
});
diff --git a/test/tests/relationsTest.js b/test/tests/relationsTest.js
@@ -38,7 +38,7 @@ describe("Zotero.Relations", function () {
assert.include(rels[0], "/users/local");
yield Zotero.DB.executeTransaction(function* () {
- yield Zotero.Relations.updateUser(1);
+ yield Zotero.Relations.updateUser(null, 1);
})
var rels = item2.getRelationsByPredicate(Zotero.Relations.relatedItemPredicate);
@@ -57,7 +57,7 @@ describe("Zotero.Relations", function () {
assert.include(rels[0], "/users/1");
yield Zotero.DB.executeTransaction(function* () {
- yield Zotero.Relations.updateUser(2);
+ yield Zotero.Relations.updateUser(1, 2);
});
var rels = item2.getRelationsByPredicate(Zotero.Relations.relatedItemPredicate);
diff --git a/test/tests/syncLocalTest.js b/test/tests/syncLocalTest.js
@@ -94,6 +94,28 @@ describe("Zotero.Sync.Data.Local", function() {
Zotero.DataDirectory.forceChange.restore();
});
+
+ it("should migrate relations using local user key", function* () {
+ yield Zotero.DB.queryAsync("DELETE FROM settings WHERE setting='account'");
+ yield Zotero.Users.init();
+
+ var item1 = yield createDataObject('item');
+ var item2 = createUnsavedDataObject('item');
+ item2.addRelatedItem(item1);
+ yield item2.save();
+
+ var pred = Zotero.Relations.relatedItemPredicate;
+ assert.isTrue(
+ item2.toJSON().relations[pred][0].startsWith('http://zotero.org/users/local/')
+ );
+
+ waitForDialog(false, 'accept', 'chrome://zotero/content/hardConfirmationDialog.xul');
+ yield Zotero.Sync.Data.Local.checkUser(window, 1, "A");
+
+ assert.isTrue(
+ item2.toJSON().relations[pred][0].startsWith('http://zotero.org/users/1/items/')
+ );
+ });
});