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