Skip skipped groups when syncing
Except if a library list is passed to Zotero.Sync.Data.Runner.sync() (which isn't done currently but will be in #1053) Follow-up to #1033
This commit is contained in:
parent
b0f3a234d0
commit
9e6565fe00
3 changed files with 90 additions and 1 deletions
|
@ -155,6 +155,32 @@ Zotero.Sync.Data.Local = {
|
|||
}),
|
||||
|
||||
|
||||
getSkippedLibraries: function () {
|
||||
return this._getSkippedLibrariesByPrefix("L");
|
||||
},
|
||||
|
||||
|
||||
getSkippedGroups: function () {
|
||||
return this._getSkippedLibrariesByPrefix("G");
|
||||
},
|
||||
|
||||
|
||||
_getSkippedLibrariesByPrefix: function (prefix) {
|
||||
var pref = 'sync.librariesToSkip';
|
||||
try {
|
||||
var librariesToSkip = JSON.parse(Zotero.Prefs.get(pref) || '[]');
|
||||
return librariesToSkip
|
||||
.filter(id => id.startsWith(prefix))
|
||||
.map(id => parseInt(id.substr(1)));
|
||||
}
|
||||
catch (e) {
|
||||
Zotero.logError(e);
|
||||
Zotero.Prefs.clear(pref);
|
||||
return [];
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* @return {nsILoginInfo|false}
|
||||
*/
|
||||
|
|
|
@ -80,7 +80,8 @@ Zotero.Sync.Runner_Module = function (options = {}) {
|
|||
* @param {Object} [options]
|
||||
* @param {Boolean} [options.background=false] Whether this is a background request, which
|
||||
* prevents some alerts from being shown
|
||||
* @param {Integer[]} [options.libraries] IDs of libraries to sync
|
||||
* @param {Integer[]} [options.libraries] IDs of libraries to sync; skipped libraries must
|
||||
* be removed if unwanted
|
||||
* @param {Function} [options.onError] Function to pass errors to instead of
|
||||
* handling internally (used for testing)
|
||||
*/
|
||||
|
@ -308,6 +309,10 @@ Zotero.Sync.Runner_Module = function (options = {}) {
|
|||
if (syncAllLibraries) {
|
||||
if (access.user && access.user.library) {
|
||||
libraries = [Zotero.Libraries.userLibraryID, Zotero.Libraries.publicationsLibraryID];
|
||||
// Remove skipped libraries
|
||||
libraries = Zotero.Utilities.arrayDiff(
|
||||
libraries, Zotero.Sync.Data.Local.getSkippedLibraries()
|
||||
);
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
@ -342,6 +347,16 @@ Zotero.Sync.Runner_Module = function (options = {}) {
|
|||
let remoteGroupIDs = Object.keys(remoteGroupVersions).map(id => parseInt(id));
|
||||
Zotero.debug(remoteGroupVersions);
|
||||
|
||||
// Remove skipped groups
|
||||
if (syncAllLibraries) {
|
||||
let newGroups = Zotero.Utilities.arrayDiff(
|
||||
remoteGroupIDs, Zotero.Sync.Data.Local.getSkippedGroups()
|
||||
);
|
||||
Zotero.Utilities.arrayDiff(remoteGroupIDs, newGroups)
|
||||
.forEach(id => { delete remoteGroupVersions[id] });
|
||||
remoteGroupIDs = newGroups;
|
||||
}
|
||||
|
||||
for (let id in remoteGroupVersions) {
|
||||
id = parseInt(id);
|
||||
let group = Zotero.Groups.get(id);
|
||||
|
|
|
@ -166,7 +166,13 @@ describe("Zotero.Sync.Runner", function () {
|
|||
})
|
||||
|
||||
describe("#checkLibraries()", function () {
|
||||
beforeEach(function* () {
|
||||
Zotero.Prefs.clear('sync.librariesToSkip');
|
||||
});
|
||||
|
||||
afterEach(function* () {
|
||||
Zotero.Prefs.clear('sync.librariesToSkip');
|
||||
|
||||
var group = Zotero.Groups.get(responses.groups.ownerGroup.json.id);
|
||||
if (group) {
|
||||
yield group.eraseTx();
|
||||
|
@ -243,6 +249,48 @@ describe("Zotero.Sync.Runner", function () {
|
|||
assert.sameMembers(libraries, [group1.libraryID]);
|
||||
})
|
||||
|
||||
it("should filter out skipped libraries if library list not provided", function* () {
|
||||
var unskippedGroupID = responses.groups.ownerGroup.json.id;
|
||||
var skippedGroupID = responses.groups.memberGroup.json.id;
|
||||
Zotero.Prefs.set('sync.librariesToSkip', `["L4", "G${skippedGroupID}"]`);
|
||||
|
||||
setResponse('userGroups.groupVersions');
|
||||
setResponse('groups.ownerGroup');
|
||||
setResponse('groups.memberGroup');
|
||||
var libraries = yield runner.checkLibraries(
|
||||
runner.getAPIClient({ apiKey }),
|
||||
false,
|
||||
responses.keyInfo.fullAccess.json
|
||||
);
|
||||
|
||||
var group = Zotero.Groups.get(unskippedGroupID);
|
||||
assert.lengthOf(libraries, 2);
|
||||
assert.sameMembers(libraries, [userLibraryID, group.libraryID]);
|
||||
});
|
||||
|
||||
it("shouldn't filter out skipped libraries if library list is provided", function* () {
|
||||
var groupData = responses.groups.memberGroup;
|
||||
var group = yield createGroup({
|
||||
id: groupData.json.id,
|
||||
version: groupData.json.version
|
||||
});
|
||||
|
||||
Zotero.Prefs.set('sync.librariesToSkip', `["L4", "G${group.id}"]`);
|
||||
|
||||
setResponse('userGroups.groupVersions');
|
||||
setResponse('groups.ownerGroup');
|
||||
setResponse('groups.memberGroup');
|
||||
var libraries = yield runner.checkLibraries(
|
||||
runner.getAPIClient({ apiKey }),
|
||||
false,
|
||||
responses.keyInfo.fullAccess.json,
|
||||
[userLibraryID, publicationsLibraryID, group.libraryID]
|
||||
);
|
||||
|
||||
assert.lengthOf(libraries, 3);
|
||||
assert.sameMembers(libraries, [userLibraryID, publicationsLibraryID, group.libraryID]);
|
||||
});
|
||||
|
||||
it("should update outdated group metadata", function* () {
|
||||
// Create groups with same id as groups response but earlier versions
|
||||
var groupData1 = responses.groups.ownerGroup;
|
||||
|
|
Loading…
Reference in a new issue