Store factory to make store optional

This commit is contained in:
Michal Horejsek
2020-05-25 15:34:18 +02:00
parent 3b0b1a457b
commit 40d8c458d2
16 changed files with 338 additions and 235 deletions

View File

@ -18,8 +18,6 @@
package users
import (
"fmt"
"path/filepath"
"runtime"
"strings"
"sync"
@ -47,9 +45,8 @@ type User struct {
imapUpdatesChannel chan imapBackend.Update
store *store.Store
storeCache *store.Cache
storePath string
storeFactory StoreMaker
store *store.Store
userID string
creds *credentials.Credentials
@ -68,8 +65,7 @@ func newUser(
eventListener listener.Listener,
credStorer CredentialsStorer,
clientManager ClientManager,
storeCache *store.Cache,
storeDir string,
storeFactory StoreMaker,
) (u *User, err error) {
log := log.WithField("user", userID)
log.Debug("Creating or loading user")
@ -85,8 +81,7 @@ func newUser(
listener: eventListener,
credStorer: credStorer,
clientManager: clientManager,
storeCache: storeCache,
storePath: getUserStorePath(storeDir, userID),
storeFactory: storeFactory,
userID: userID,
creds: creds,
}
@ -140,7 +135,7 @@ func (u *User) init(idleUpdates chan imapBackend.Update) (err error) {
}
u.store = nil
}
store, err := store.New(u.panicHandler, u, u.clientManager, u.listener, u.storePath, u.storeCache)
store, err := u.storeFactory.New(u)
if err != nil {
return errors.Wrap(err, "failed to create store")
}
@ -267,7 +262,7 @@ func (u *User) clearStore() error {
}
} else {
u.log.Warn("Store is not initialized: cleaning up store files manually")
if err := store.RemoveStore(u.storeCache, u.storePath, u.userID); err != nil {
if err := u.storeFactory.Remove(u.userID); err != nil {
return errors.Wrap(err, "failed to remove store manually")
}
}
@ -287,12 +282,6 @@ func (u *User) closeStore() error {
return nil
}
// getUserStorePath returns the file path of the store database for the given userID.
func getUserStorePath(storeDir string, userID string) (path string) {
fileName := fmt.Sprintf("mailbox-%v.db", userID)
return filepath.Join(storeDir, fileName)
}
// GetTemporaryPMAPIClient returns an authorised PMAPI client.
// Do not use! It's only for backward compatibility of old SMTP and IMAP implementations.
// After proper refactor of SMTP and IMAP remove this method.