Enable Undo Retrieve Metadata when translator added note (#4267)

Closes #2977
This commit is contained in:
Abe Jellinek 2024-06-21 12:56:29 -04:00 committed by Dan Stillman
parent a054831a3a
commit d8eae4cd3b
2 changed files with 109 additions and 7 deletions

View file

@ -27,6 +27,7 @@ Zotero.RecognizeDocument = new function () {
const OFFLINE_RECHECK_DELAY = 60 * 1000;
const MAX_PAGES = 5;
const UNRECOGNIZE_TIMEOUT = 86400 * 1000;
const NOTE_EDIT_THRESHOLD = 1000;
const EPUB_MAX_SECTIONS = 5;
let _newItems = new WeakMap();
@ -169,8 +170,7 @@ Zotero.RecognizeDocument = new function () {
if (!dateModified
|| Zotero.Date.sqlToDate(dateModified, true) < new Date() - UNRECOGNIZE_TIMEOUT
|| item.dateModified != dateModified
|| item.numAttachments(true) != 1
|| item.numChildren(true) != 1) {
|| item.numAttachments(true) != 1) {
_newItems.delete(item);
return false;
}
@ -182,6 +182,13 @@ Zotero.RecognizeDocument = new function () {
return false;
}
// Notes must have been modified within one second of the item
var notes = Zotero.Items.get(item.getNotes());
if (notes.some(note => note.dateModified > dateModified + NOTE_EDIT_THRESHOLD)) {
_newItems.delete(item);
return false;
}
return true;
};

View file

@ -309,7 +309,6 @@ describe("Document Recognition", function() {
itemType: 'book',
title: 'The Mania of the Nations on the Planet Mars: ISBN Database Edition',
ISBN: isbn,
attachments: [],
tags: []
}];
});
@ -388,7 +387,6 @@ describe("Document Recognition", function() {
itemType: 'book',
title: 'The Mania of the Nations on the Planet Mars: Bad Metadata Edition',
ISBN: isbnWrong, // Wrong ISBN
attachments: [],
tags: []
}];
});
@ -453,7 +451,6 @@ describe("Document Recognition", function() {
itemType: 'book',
title: 'Building the American Republic, Volume 1, Library Catalog Edition',
ISBN: isbn,
attachments: [],
tags: []
}];
});
@ -490,7 +487,6 @@ describe("Document Recognition", function() {
itemType: 'book',
title: 'Building the American Republic, Volume 1, Library Catalog Edition',
ISBN: isbn,
attachments: [],
tags: []
}];
});
@ -516,4 +512,103 @@ describe("Document Recognition", function() {
translateStub.restore();
});
});
});
describe("canUnrecognize()", function () {
if (Zotero.automatedTest) this.skip(); // TODO: Mock services
async function getRecognizedItem() {
let search;
let itemJSON = {
itemType: 'book',
title: 'The Mania of the Nations on the Planet Mars',
ISBN: '9780656173822',
tags: []
};
let translateStub = sinon.stub(Zotero.Translate.Search.prototype, 'translate')
.callsFake(async function () {
search = this.search;
return [itemJSON];
});
let testDir = getTestDataDirectory();
testDir.append('recognizeEPUB_test_ISBN.epub');
await Zotero.Attachments.importFromFile({
file: testDir,
});
win.ZoteroPane.recognizeSelected();
let addedIDs = await waitForItemEvent('add');
await waitForItemEvent('modify');
// Wait for status to show as complete
var progressWindow = getWindows("chrome://zotero/content/progressQueueDialog.xhtml")[0];
var completeStr = Zotero.getString("general.finished");
while (progressWindow.document.getElementById("label").value != completeStr) {
await Zotero.Promise.delay(20);
}
assert.isTrue(translateStub.calledOnce);
assert.ok(search);
assert.lengthOf(addedIDs, 1);
translateStub.restore();
return Zotero.Items.get(addedIDs[0]);
}
it("should return true for a recognized item with one attachment", async function () {
let item = await getRecognizedItem();
assert.equal(item.numAttachments(), 1);
assert.equal(item.numNotes(), 0);
assert.isTrue(Zotero.RecognizeDocument.canUnrecognize(item));
});
it("should return false for a recognized item with one trashed attachment", async function () {
let item = await getRecognizedItem();
assert.equal(item.numAttachments(), 1);
assert.equal(item.numNotes(), 0);
let attachment = Zotero.Items.get(item.getAttachments()[0]);
attachment.deleted = true;
await attachment.saveTx();
assert.equal(item.numAttachments(), 0);
assert.equal(item.numNotes(), 0);
assert.isFalse(Zotero.RecognizeDocument.canUnrecognize(item));
});
it("should return true for a recognized item with one attachment and a note", async function () {
let item = await getRecognizedItem();
assert.equal(item.numAttachments(), 1);
// Let's pretend this was adding during translation
let note = new Zotero.Item('note');
note.setNote('This is a note');
note.parentItemID = item.id;
await note.saveTx();
assert.equal(item.numNotes(), 1);
assert.isTrue(Zotero.RecognizeDocument.canUnrecognize(item));
});
it("should return false for a recognized item with one attachment and a modified note", async function () {
let item = await getRecognizedItem();
assert.equal(item.numAttachments(), 1);
// Let's pretend this was adding during translation
let note = new Zotero.Item('note');
note.setNote('This is a note');
note.parentItemID = item.id;
await note.saveTx();
await Zotero.Promise.delay(1200);
note.setNote('This is a modified note');
await note.saveTx();
assert.equal(item.numNotes(), 1);
assert.isFalse(Zotero.RecognizeDocument.canUnrecognize(item));
});
});
});