mirror of
https://github.com/mjl-/mox.git
synced 2025-07-10 10:34:40 +03:00
Refactor how messages are added to mailboxes
DeliverMessage() is now MessageAdd(), and it takes a Mailbox object that it modifies but doesn't write to the database (the caller must do it, and plenty of times can do it more efficiently by doing it once for multiple messages). The new AddOpts let the caller influence how many checks and how much of the work MessageAdd() does. The zero-value AddOpts enable all checks and all the work, but callers can take responsibility of some of the checks/work if it can do it more efficiently itself. This simplifies the code in most places, and makes it more efficient. The checks to update per-mailbox keywords is a bit simpler too now. We are also more careful to close the junk filter without saving it in case of errors. Still part of more upcoming changes.
This commit is contained in:
@ -50,6 +50,18 @@ func (a *Account) OpenJunkFilter(ctx context.Context, log mlog.Log) (*junk.Filte
|
||||
return f, jf, err
|
||||
}
|
||||
|
||||
func (a *Account) ensureJunkFilter(ctx context.Context, log mlog.Log, jfOpt *junk.Filter) (jf *junk.Filter, opened bool, err error) {
|
||||
if jfOpt != nil {
|
||||
return jfOpt, false, nil
|
||||
}
|
||||
|
||||
jf, _, err = a.OpenJunkFilter(ctx, log)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("open junk filter: %v", err)
|
||||
}
|
||||
return jf, true, nil
|
||||
}
|
||||
|
||||
// RetrainMessages (un)trains messages, if relevant given their flags. Updates
|
||||
// m.TrainedJunk after retraining.
|
||||
func (a *Account) RetrainMessages(ctx context.Context, log mlog.Log, tx *bstore.Tx, msgs []Message) (rerr error) {
|
||||
@ -75,11 +87,11 @@ func (a *Account) RetrainMessages(ctx context.Context, log mlog.Log, tx *bstore.
|
||||
return fmt.Errorf("open junk filter: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
if jf != nil {
|
||||
err := jf.Close()
|
||||
if rerr == nil {
|
||||
rerr = err
|
||||
}
|
||||
if rerr != nil {
|
||||
err := jf.CloseDiscard()
|
||||
log.Check(err, "close junk filter without saving")
|
||||
} else {
|
||||
rerr = jf.Close()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
Reference in New Issue
Block a user