Clone feed items if translation returns no items. Closes #1377

This commit is contained in:
Adomas Venčkauskas 2017-11-30 11:39:34 +02:00
parent 9b9af65f8a
commit 6970556dbd
2 changed files with 53 additions and 21 deletions

View file

@ -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<FeedItem|Item>} 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<FeedItem|Item>} 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;
});

View file

@ -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');
});
});
});