mirror of
https://github.com/ankitects/anki.git
synced 2025-09-25 09:16:38 -04:00
Skip importing foreign notes with filtered decks
Were implicitly imported into the default deck before. Also some refactoring to fetch deck ids and names beforehand.
This commit is contained in:
parent
053ce82588
commit
c2244acd01
1 changed files with 42 additions and 18 deletions
|
@ -1,7 +1,12 @@
|
||||||
// Copyright: Ankitects Pty Ltd and contributors
|
// Copyright: Ankitects Pty Ltd and contributors
|
||||||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
|
|
||||||
use std::{borrow::Cow, collections::HashMap, mem, sync::Arc};
|
use std::{
|
||||||
|
borrow::Cow,
|
||||||
|
collections::{HashMap, HashSet},
|
||||||
|
mem,
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
|
||||||
use super::NameOrId;
|
use super::NameOrId;
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -53,7 +58,7 @@ struct Context<'a> {
|
||||||
/// Contains the optional default notetype with the default key.
|
/// Contains the optional default notetype with the default key.
|
||||||
notetypes: HashMap<NameOrId, Option<Arc<Notetype>>>,
|
notetypes: HashMap<NameOrId, Option<Arc<Notetype>>>,
|
||||||
/// Contains the optional default deck id with the default key.
|
/// Contains the optional default deck id with the default key.
|
||||||
deck_ids: HashMap<NameOrId, Option<DeckId>>,
|
deck_ids: DeckIdsByNameOrId,
|
||||||
usn: Usn,
|
usn: Usn,
|
||||||
normalize_notes: bool,
|
normalize_notes: bool,
|
||||||
today: u32,
|
today: u32,
|
||||||
|
@ -62,6 +67,12 @@ struct Context<'a> {
|
||||||
existing_notes: HashMap<(NotetypeId, u32), Vec<NoteId>>,
|
existing_notes: HashMap<(NotetypeId, u32), Vec<NoteId>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct DeckIdsByNameOrId {
|
||||||
|
ids: HashSet<DeckId>,
|
||||||
|
names: HashMap<String, DeckId>,
|
||||||
|
default: Option<DeckId>,
|
||||||
|
}
|
||||||
|
|
||||||
struct NoteContext {
|
struct NoteContext {
|
||||||
note: Note,
|
note: Note,
|
||||||
dupes: Vec<Note>,
|
dupes: Vec<Note>,
|
||||||
|
@ -70,6 +81,33 @@ struct NoteContext {
|
||||||
deck_id: DeckId,
|
deck_id: DeckId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl DeckIdsByNameOrId {
|
||||||
|
fn new(col: &mut Collection, default: &NameOrId) -> Result<Self> {
|
||||||
|
let names: HashMap<String, DeckId> = col
|
||||||
|
.get_all_normal_deck_names()?
|
||||||
|
.into_iter()
|
||||||
|
.map(|(id, name)| (name, id))
|
||||||
|
.collect();
|
||||||
|
let ids = names.values().copied().collect();
|
||||||
|
let mut new = Self {
|
||||||
|
ids,
|
||||||
|
names,
|
||||||
|
default: None,
|
||||||
|
};
|
||||||
|
new.default = new.get(default);
|
||||||
|
|
||||||
|
Ok(new)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get(&self, name_or_id: &NameOrId) -> Option<DeckId> {
|
||||||
|
match name_or_id {
|
||||||
|
_ if *name_or_id == NameOrId::default() => self.default,
|
||||||
|
NameOrId::Id(id) => self.ids.get(&DeckId(*id)).copied(),
|
||||||
|
NameOrId::Name(name) => self.names.get(name).copied(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> Context<'a> {
|
impl<'a> Context<'a> {
|
||||||
fn new(data: &ForeignData, col: &'a mut Collection) -> Result<Self> {
|
fn new(data: &ForeignData, col: &'a mut Collection) -> Result<Self> {
|
||||||
let usn = col.usn()?;
|
let usn = col.usn()?;
|
||||||
|
@ -80,11 +118,7 @@ impl<'a> Context<'a> {
|
||||||
NameOrId::default(),
|
NameOrId::default(),
|
||||||
col.notetype_by_name_or_id(&data.default_notetype)?,
|
col.notetype_by_name_or_id(&data.default_notetype)?,
|
||||||
);
|
);
|
||||||
let mut deck_ids = HashMap::new();
|
let deck_ids = DeckIdsByNameOrId::new(col, &data.default_deck)?;
|
||||||
deck_ids.insert(
|
|
||||||
NameOrId::default(),
|
|
||||||
col.deck_id_by_name_or_id(&data.default_deck)?,
|
|
||||||
);
|
|
||||||
let existing_notes = col.storage.all_notes_by_type_and_checksum()?;
|
let existing_notes = col.storage.all_notes_by_type_and_checksum()?;
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
col,
|
col,
|
||||||
|
@ -119,16 +153,6 @@ impl<'a> Context<'a> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deck_id_for_note(&mut self, note: &ForeignNote) -> Result<Option<DeckId>> {
|
|
||||||
Ok(if let Some(did) = self.deck_ids.get(¬e.deck) {
|
|
||||||
*did
|
|
||||||
} else {
|
|
||||||
let did = self.col.deck_id_by_name_or_id(¬e.deck)?;
|
|
||||||
self.deck_ids.insert(note.deck.clone(), did);
|
|
||||||
did
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn import_foreign_notes(
|
fn import_foreign_notes(
|
||||||
&mut self,
|
&mut self,
|
||||||
notes: Vec<ForeignNote>,
|
notes: Vec<ForeignNote>,
|
||||||
|
@ -145,7 +169,7 @@ impl<'a> Context<'a> {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if let Some(notetype) = self.notetype_for_note(&foreign)? {
|
if let Some(notetype) = self.notetype_for_note(&foreign)? {
|
||||||
if let Some(deck_id) = self.deck_id_for_note(&foreign)? {
|
if let Some(deck_id) = self.deck_ids.get(&foreign.deck) {
|
||||||
let ctx = self.build_note_context(foreign, notetype, deck_id, global_tags)?;
|
let ctx = self.build_note_context(foreign, notetype, deck_id, global_tags)?;
|
||||||
self.import_note(ctx, updated_tags, &mut log)?;
|
self.import_note(ctx, updated_tags, &mut log)?;
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue