ente/pkg/remote_sync.go
Neeraj Gupta 02ff452c09 Refactor
2023-09-23 16:15:10 +05:30

51 lines
1.5 KiB
Go

package pkg
import (
"cli-go/pkg/model"
"context"
"encoding/base64"
"fmt"
bolt "go.etcd.io/bbolt"
"log"
)
func (c *ClICtrl) SyncAccount(account model.Account) error {
log.SetPrefix(fmt.Sprintf("[%s] ", account.Email))
secretInfo, err := c.KeyHolder.LoadSecrets(account, c.CliKey)
if err != nil {
return err
}
ctx := c.buildRequestContext(context.Background(), account)
err = createDataBuckets(c.DB, account)
if err != nil {
return err
}
c.Client.AddToken(account.AccountKey(), base64.URLEncoding.EncodeToString(secretInfo.Token))
return c.syncRemoteCollections(ctx, account)
}
func (c *ClICtrl) buildRequestContext(ctx context.Context, account model.Account) context.Context {
ctx = context.WithValue(ctx, "app", string(account.App))
ctx = context.WithValue(ctx, "account_id", account.AccountKey())
ctx = context.WithValue(ctx, "user_id", account.UserID)
return ctx
}
var dataCategories = []string{"remote-collections", "local-collections", "remote-files", "local-files", "remote-collection-removed", "remote-files-removed"}
func createDataBuckets(db *bolt.DB, account model.Account) error {
return db.Update(func(tx *bolt.Tx) error {
dataBucket, err := tx.CreateBucketIfNotExists([]byte(account.DataBucket()))
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
for _, category := range dataCategories {
_, err := dataBucket.CreateBucketIfNotExists([]byte(fmt.Sprintf(category)))
if err != nil {
return err
}
}
return nil
})
}