From 2eef1702e0b0c959fb897ada874a300494ea214f Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 4 May 2017 21:18:44 -0400 Subject: [PATCH] Prevent items in group libraries from being added to My Publications And remove existing group items that have been added --- .../zotero/xpcom/collectionTreeView.js | 41 ++++++++----------- chrome/content/zotero/xpcom/data/item.js | 3 ++ chrome/content/zotero/xpcom/schema.js | 4 ++ resource/schema/userdata.sql | 2 +- test/tests/itemTest.js | 9 ++++ 5 files changed, 35 insertions(+), 24 deletions(-) diff --git a/chrome/content/zotero/xpcom/collectionTreeView.js b/chrome/content/zotero/xpcom/collectionTreeView.js index e3f3121904..a9a0cadc7e 100644 --- a/chrome/content/zotero/xpcom/collectionTreeView.js +++ b/chrome/content/zotero/xpcom/collectionTreeView.js @@ -1639,6 +1639,14 @@ Zotero.CollectionTreeView.prototype.canDropCheck = function (row, orient, dataTr Zotero.debug("FeedItems cannot be added to My Publications"); return false; } + if (item.inPublications) { + Zotero.debug("Item " + item.id + " already exists in My Publications"); + continue; + } + if (treeRow.ref.libraryID != item.libraryID) { + Zotero.debug("Cross-library drag to My Publications not allowed"); + continue; + } skip = false; continue; } @@ -1662,10 +1670,14 @@ Zotero.CollectionTreeView.prototype.canDropCheck = function (row, orient, dataTr return false; } - // Allow drags to collections. Item collection membership is an asynchronous - // check, so we do that on drop() + // Make sure there's at least one item that's not already in this destination if (treeRow.isCollection()) { + if (treeRow.ref.hasItem(item.id)) { + Zotero.debug("Item " + item.id + " already exists in collection"); + continue; + } skip = false; + continue; } } if (skip) { @@ -1785,27 +1797,10 @@ Zotero.CollectionTreeView.prototype.canDropCheckAsync = Zotero.Promise.coroutine continue; } - // Intra-library drag - - // Make sure there's at least one item that's not already in this destination - if (treeRow.isCollection()) { - if (treeRow.ref.hasItem(item.id)) { - Zotero.debug("Item " + item.id + " already exists in collection"); - continue; - } - skip = false; - continue; - } - - // Make sure there's at least one item that's not already in My Publications - if (treeRow.isPublications()) { - if (item.inPublications) { - Zotero.debug("Item " + item.id + " already exists in My Publications"); - continue; - } - skip = false; - continue; - } + // Intra-library drags have already been vetted by canDrop(). This 'break' should be + // changed to a 'continue' if any asynchronous checks that stop the drag are added above + skip = false; + break; } if (skip) { Zotero.debug("Drag skipped"); diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index 84d076e1f8..67fd8b7f03 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -1519,6 +1519,9 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) { if (this.isAttachment() && this.attachmentLinkMode == Zotero.Attachments.LINK_MODE_LINKED_FILE) { throw new Error("Linked-file attachments cannot be added to My Publications"); } + if (Zotero.Libraries.get(this.libraryID).libraryType != 'user') { + throw new Error("Only items in user libraries can be added to My Publications"); + } } // Trashed status diff --git a/chrome/content/zotero/xpcom/schema.js b/chrome/content/zotero/xpcom/schema.js index 8b0c7bedda..8960099c29 100644 --- a/chrome/content/zotero/xpcom/schema.js +++ b/chrome/content/zotero/xpcom/schema.js @@ -2416,6 +2416,10 @@ Zotero.Schema = new function(){ } yield Zotero.DB.queryAsync("DELETE FROM publicationsItems WHERE itemID IN (SELECT itemID FROM items JOIN itemAttachments USING (itemID) WHERE linkMode=2)"); } + + else if (i == 95) { + yield Zotero.DB.queryAsync("DELETE FROM publicationsItems WHERE itemID NOT IN (SELECT itemID FROM items WHERE libraryID=1)"); + } } yield _updateDBVersion('userdata', toVersion); diff --git a/resource/schema/userdata.sql b/resource/schema/userdata.sql index b6f5df10be..74c01eb659 100644 --- a/resource/schema/userdata.sql +++ b/resource/schema/userdata.sql @@ -1,4 +1,4 @@ --- 94 +-- 95 -- Copyright (c) 2009 Center for History and New Media -- George Mason University, Fairfax, Virginia, USA diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js index 643ee82879..9d3ee0d549 100644 --- a/test/tests/itemTest.js +++ b/test/tests/itemTest.js @@ -376,6 +376,15 @@ describe("Zotero.Item", function () { assert.ok(e); assert.include(e.message, "Linked-file attachments cannot be added to My Publications"); }); + + it("should be invalid for group library items", function* () { + var group = yield getGroup(); + var item = yield createDataObject('item', { libraryID: group.libraryID }); + item.inPublications = true; + var e = yield getPromiseError(item.saveTx()); + assert.ok(e); + assert.equal(e.message, "Only items in user libraries can be added to My Publications"); + }); }); describe("#parentID", function () {