Import/Export backend prep

This commit is contained in:
Michal Horejsek
2020-05-14 15:22:29 +02:00
parent 9d65192ad7
commit b598779c0f
92 changed files with 6983 additions and 188 deletions

View File

@ -52,8 +52,14 @@ type Users struct {
// People are used to that and so we preserve that ordering here.
users []*User
// idleUpdates is a channel which the imap backend listens to and which it uses
// to send idle updates to the mail client (eg thunderbird).
// useOnlyActiveAddresses determines whether credentials keeps only active
// addresses or all of them. Each usage has to be consisteng, e.g., once
// user is added, it saves address list to credentials and next time loads
// as is, without requesting server again.
useOnlyActiveAddresses bool
// idleUpdates is a channel which the imap backend listens to and which it
// uses to send idle updates to the mail client (eg thunderbird).
// The user stores should send idle updates on this channel.
idleUpdates chan imapBackend.Update
@ -70,19 +76,21 @@ func New(
clientManager ClientManager,
credStorer CredentialsStorer,
storeFactory StoreMaker,
useOnlyActiveAddresses bool,
) *Users {
log.Trace("Creating new users")
u := &Users{
config: config,
panicHandler: panicHandler,
events: eventListener,
clientManager: clientManager,
credStorer: credStorer,
storeFactory: storeFactory,
idleUpdates: make(chan imapBackend.Update),
lock: sync.RWMutex{},
stopAll: make(chan struct{}),
config: config,
panicHandler: panicHandler,
events: eventListener,
clientManager: clientManager,
credStorer: credStorer,
storeFactory: storeFactory,
useOnlyActiveAddresses: useOnlyActiveAddresses,
idleUpdates: make(chan imapBackend.Update),
lock: sync.RWMutex{},
stopAll: make(chan struct{}),
}
go func() {
@ -291,9 +299,15 @@ func (u *Users) addNewUser(apiUser *pmapi.User, auth *pmapi.Auth, hashedPassphra
return errors.Wrap(err, "failed to update API user")
}
activeEmails := client.Addresses().ActiveEmails()
emails := []string{}
for _, address := range client.Addresses() {
if u.useOnlyActiveAddresses && address.Receive != pmapi.CanReceive {
continue
}
emails = append(emails, address.Email)
}
if _, err = u.credStorer.Add(apiUser.ID, apiUser.Name, auth.GenToken(), hashedPassphrase, activeEmails); err != nil {
if _, err = u.credStorer.Add(apiUser.ID, apiUser.Name, auth.GenToken(), hashedPassphrase, emails); err != nil {
return errors.Wrap(err, "failed to add user to credentials store")
}