mirror of
https://github.com/mjl-/mox.git
synced 2025-06-28 01:48:15 +03:00
when writing updated word counts to the junk filter, remove entries where both counts are 0
no point in keeping them around. also pass on error when getting a word from database returned an error.
This commit is contained in:
parent
394bdef39d
commit
062c3ac182
@ -288,17 +288,28 @@ func (f *Filter) Save() error {
|
||||
}
|
||||
err := f.db.Write(context.Background(), func(tx *bstore.Tx) error {
|
||||
update := func(w string, ham, spam uint32) error {
|
||||
zeroword := w != "-" && ham == 0 && spam == 0
|
||||
|
||||
if f.isNew {
|
||||
if zeroword {
|
||||
return nil
|
||||
}
|
||||
return tx.Insert(&Wordscore{w, ham, spam})
|
||||
}
|
||||
|
||||
wc := Wordscore{w, 0, 0}
|
||||
err := tx.Get(&wc)
|
||||
if err == bstore.ErrAbsent {
|
||||
if zeroword {
|
||||
return nil
|
||||
}
|
||||
return tx.Insert(&Wordscore{w, ham, spam})
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
if zeroword {
|
||||
return tx.Delete(&Wordscore{w, 0, 0})
|
||||
}
|
||||
return tx.Update(&Wordscore{w, ham, spam})
|
||||
}
|
||||
if err := update("-", f.hams, f.spams); err != nil {
|
||||
@ -334,6 +345,8 @@ func loadWords(ctx context.Context, db *bstore.DB, l []string, dst map[string]wo
|
||||
wc := Wordscore{Word: w}
|
||||
if err := tx.Get(&wc); err == nil {
|
||||
dst[w] = word{wc.Ham, wc.Spam}
|
||||
} else if err != bstore.ErrAbsent {
|
||||
return fmt.Errorf("get word: %v", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
Loading…
x
Reference in New Issue
Block a user