summaryrefslogtreecommitdiff
path: root/ext/js/background
diff options
context:
space:
mode:
Diffstat (limited to 'ext/js/background')
-rw-r--r--ext/js/background/backend.js29
1 files changed, 8 insertions, 21 deletions
diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js
index 881cd241..b0c0a36d 100644
--- a/ext/js/background/backend.js
+++ b/ext/js/background/backend.js
@@ -537,24 +537,16 @@ export class Backend {
/**
* @param {import('anki').Note[]} notes
- * @returns {Promise<({ canAdd: true; } | { canAdd: false; error: string; })[]>}
+ * @returns {Promise<import('backend').CanAddResults>}
*/
- async detectDuplicateNotes(notes) {
+ async partitionAddibleNotes(notes) {
// `allowDuplicate` is on for all notes by default, so we temporarily set it to false
// to check which notes are duplicates.
const notesNoDuplicatesAllowed = notes.map((note) => ({...note, options: {...note.options, allowDuplicate: false}}));
- return await this._anki.canAddNotesWithErrorDetail(notesNoDuplicatesAllowed);
- }
-
- /**
- * Partitions notes between those that can / cannot be added.
- * It further sets the `isDuplicate` strings for notes that have a duplicate.
- * @param {import('anki').Note[]} notes
- * @returns {Promise<import('backend').CanAddResults>}
- */
- async partitionAddibleNotes(notes) {
- const canAddResults = await this.detectDuplicateNotes(notes);
+ // If only older AnkiConnect available, use `canAddNotes`.
+ const withDuplicatesAllowed = await this._anki.canAddNotes(notes);
+ const noDuplicatesAllowed = await this._anki.canAddNotes(notesNoDuplicatesAllowed);
/** @type {{ note: import('anki').Note, isDuplicate: boolean }[]} */
const canAddArray = [];
@@ -562,16 +554,11 @@ export class Backend {
/** @type {import('anki').Note[]} */
const cannotAddArray = [];
- for (let i = 0; i < canAddResults.length; i++) {
- const result = canAddResults[i];
-
- // If the note is a duplicate, the error is "cannot create note because it is a duplicate".
- if (result.canAdd) {
+ for (let i = 0; i < withDuplicatesAllowed.length; i++) {
+ if (withDuplicatesAllowed[i] === noDuplicatesAllowed[i]) {
canAddArray.push({note: notes[i], isDuplicate: false});
- } else if (result.error.endsWith('duplicate')) {
- canAddArray.push({note: notes[i], isDuplicate: true});
} else {
- cannotAddArray.push(notes[i]);
+ canAddArray.push({note: notes[i], isDuplicate: true});
}
}