mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-19 08:37:06 +00:00
GODT-1351: Cache and update of space bytes in user object.
This commit is contained in:
@ -49,6 +49,8 @@ type User struct {
|
||||
userID string
|
||||
creds *credentials.Credentials
|
||||
|
||||
usedBytes, totalBytes int64
|
||||
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
@ -122,6 +124,8 @@ func (u *User) connect(client pmapi.Client, creds *credentials.Credentials) erro
|
||||
u.store.StartWatcher()
|
||||
}
|
||||
|
||||
u.UpdateSpace(nil)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -201,6 +205,40 @@ func (u *User) ID() string {
|
||||
return u.userID
|
||||
}
|
||||
|
||||
// UsedBytes returns number of bytes used on server.
|
||||
func (u *User) UsedBytes() int64 {
|
||||
return u.usedBytes
|
||||
}
|
||||
|
||||
// TotalBytes returns number of bytes available on server.
|
||||
func (u *User) TotalBytes() int64 {
|
||||
return u.totalBytes
|
||||
}
|
||||
|
||||
// UpdateSpace will update TotalBytes and UsedBytes values from API user. If
|
||||
// pointer is nill it will get fresh user from API. API user can come from
|
||||
// update event which means it doesn't contain all data. Therefore only
|
||||
// positive values will be updated.
|
||||
func (u *User) UpdateSpace(apiUser *pmapi.User) {
|
||||
// If missing get latest pmapi.User from API instead of using cached
|
||||
// values from client.CurrentUser()
|
||||
if apiUser == nil {
|
||||
var err error
|
||||
apiUser, err = u.client.GetUser(context.Background())
|
||||
if err != nil {
|
||||
u.log.WithError(err).Warning("Cannot update user space")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if apiUser.UsedSpace != nil {
|
||||
u.usedBytes = *apiUser.UsedSpace
|
||||
}
|
||||
if apiUser.MaxSpace != nil {
|
||||
u.totalBytes = *apiUser.MaxSpace
|
||||
}
|
||||
}
|
||||
|
||||
// Username returns the user's username as found in the user's credentials.
|
||||
func (u *User) Username() string {
|
||||
u.lock.RLock()
|
||||
@ -351,7 +389,7 @@ func (u *User) UpdateUser(ctx context.Context) error {
|
||||
defer u.lock.Unlock()
|
||||
defer u.listener.Emit(events.UserRefreshEvent, u.userID)
|
||||
|
||||
_, err := u.client.UpdateUser(ctx)
|
||||
user, err := u.client.UpdateUser(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -367,6 +405,8 @@ func (u *User) UpdateUser(ctx context.Context) error {
|
||||
|
||||
u.creds = creds
|
||||
|
||||
u.UpdateSpace(user)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user