From 6970556dbdd93fb301b1ec38da30afc173722ec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adomas=20Ven=C4=8Dkauskas?= Date: Thu, 30 Nov 2017 11:39:34 +0200 Subject: [PATCH] Clone feed items if translation returns no items. Closes #1377 --- chrome/content/zotero/xpcom/data/feedItem.js | 55 +++++++++++++------- test/tests/feedItemTest.js | 19 ++++++- 2 files changed, 53 insertions(+), 21 deletions(-) diff --git a/chrome/content/zotero/xpcom/data/feedItem.js b/chrome/content/zotero/xpcom/data/feedItem.js index 384e00f41e..dfdc236c86 100644 --- a/chrome/content/zotero/xpcom/data/feedItem.js +++ b/chrome/content/zotero/xpcom/data/feedItem.js @@ -203,8 +203,8 @@ Zotero.FeedItem.prototype.toggleRead = Zotero.Promise.coroutine(function* (state * Uses the item url to translate an existing feed item. * If libraryID empty, overwrites feed item, otherwise saves * in the library - * @param libraryID {int} save item in library - * @param collectionID {int} add item to collection + * @param libraryID {Integer} save item in library + * @param collectionID {Integer} add item to collection * @return {Promise} translated feed item */ Zotero.FeedItem.prototype.translate = Zotero.Promise.coroutine(function* (libraryID, collectionID) { @@ -254,23 +254,7 @@ Zotero.FeedItem.prototype.translate = Zotero.Promise.coroutine(function* (librar if (!translators || !translators.length) { Zotero.debug("No translators detected for feed item " + this.id + " with URL " + this.getField('url') + ' -- cloning item instead', 2); - let dbItem = this.clone(libraryID); - if (collectionID) { - dbItem.addToCollection(collectionID); - } - yield dbItem.saveTx(); - - let item = {title: dbItem.getField('title'), itemType: dbItem.itemType}; - - // Add snapshot - if (Zotero.Libraries.get(libraryID).filesEditable) { - item.attachments = [{title: "Snapshot"}]; - yield Zotero.Attachments.importFromDocument({ - document: doc, - parentItemID: dbItem.id - }); - } - + let item = yield this.clone(libraryID, collectionID, doc); progressWindow.Translation.itemDoneHandler()(null, null, item); progressWindow.Translation.doneHandler(null, true); return; @@ -283,6 +267,12 @@ Zotero.FeedItem.prototype.translate = Zotero.Promise.coroutine(function* (librar let result = yield translate.translate({libraryID, collections: collectionID ? [collectionID] : false}) .then(items => items ? items[0] : false); Zotero.Browser.deleteHiddenBrowser(hiddenBrowser); + if (!result) { + let item = yield this.clone(libraryID, collectionID, doc); + progressWindow.Translation.itemDoneHandler()(null, null, item); + progressWindow.Translation.doneHandler(null, true); + return; + } return result; } @@ -309,3 +299,30 @@ Zotero.FeedItem.prototype.translate = Zotero.Promise.coroutine(function* (librar return this; }); + +/** + * Clones the feed item (usually, when proper translation is unavailable) + * @param libraryID {Integer} save item in library + * @param collectionID {Integer} add item to collection + * @return {Promise} translated feed item + */ +Zotero.FeedItem.prototype.clone = Zotero.Promise.coroutine(function* (libraryID, collectionID, doc) { + let dbItem = Zotero.Item.prototype.clone.call(this, libraryID); + if (collectionID) { + dbItem.addToCollection(collectionID); + } + yield dbItem.saveTx(); + + let item = {title: dbItem.getField('title'), itemType: dbItem.itemType, attachments: []}; + + // Add snapshot + if (Zotero.Libraries.get(libraryID).filesEditable) { + item.attachments = [{title: "Snapshot"}]; + yield Zotero.Attachments.importFromDocument({ + document: doc, + parentItemID: dbItem.id + }); + } + + return item; +}); diff --git a/test/tests/feedItemTest.js b/test/tests/feedItemTest.js index d472c67f3a..054544e348 100644 --- a/test/tests/feedItemTest.js +++ b/test/tests/feedItemTest.js @@ -241,7 +241,7 @@ describe("Zotero.FeedItem", function () { win.close() }); - it('translates and saves items', function* () { + it('should translate and save items', function* () { var feedItem = yield createDataObject('feedItem', {libraryID}); var url = getTestDataUrl('metadata/journalArticle-single.html'); feedItem.setField('url', url); @@ -251,7 +251,7 @@ describe("Zotero.FeedItem", function () { assert.equal(feedItem.getField('title'), 'Scarcity or Abundance? Preserving the Past in a Digital Era'); }); - it('translates and saves items to corresponding library and collection', function* () { + it('should translate and save items to corresponding library and collection', function* () { let group = yield createGroup(); let collection = yield createDataObject('collection', {libraryID: group.libraryID}); @@ -266,5 +266,20 @@ describe("Zotero.FeedItem", function () { assert.equal(item.getField('title'), 'Scarcity or Abundance? Preserving the Past in a Digital Era'); }); + it('should clone the item to corresponding library and collection if no translators available', function* () { + let group = yield createGroup(); + let collection = yield createDataObject('collection', {libraryID: group.libraryID}); + + var feedItem = yield createDataObject('feedItem', {libraryID, title: 'test'}); + var url = getTestDataUrl('test.html'); + feedItem.setField('url', url); + yield feedItem.saveTx(); + + yield feedItem.translate(group.libraryID, collection.id); + + let item = collection.getChildItems(false, false)[0]; + + assert.equal(item.getField('title'), 'test'); + }); }); });