2023-09-22 04:44:22 +00:00
|
|
|
package pkg
|
|
|
|
|
|
|
|
import (
|
2023-09-22 13:37:12 +00:00
|
|
|
enteCrypto "cli-go/internal/crypto"
|
2023-09-22 04:44:22 +00:00
|
|
|
"cli-go/pkg/model"
|
2023-09-22 08:20:50 +00:00
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
2023-09-22 04:44:22 +00:00
|
|
|
"fmt"
|
|
|
|
bolt "go.etcd.io/bbolt"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2023-09-22 13:37:12 +00:00
|
|
|
var accountMasterKey = map[string][]byte{}
|
|
|
|
|
2023-09-22 08:20:50 +00:00
|
|
|
func (c *ClICtrl) SyncAccount(account model.Account) error {
|
|
|
|
log.SetPrefix(fmt.Sprintf("[%s] ", account.Email))
|
2023-09-22 13:37:12 +00:00
|
|
|
|
2023-09-22 08:20:50 +00:00
|
|
|
err := createDataBuckets(c.DB, account)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-22 13:37:12 +00:00
|
|
|
token := account.Token.MustDecrypt(c.CliKey)
|
2023-09-22 13:47:24 +00:00
|
|
|
accountMasterKey[account.AccountKey()] = []byte(account.MasterKey.MustDecrypt(c.CliKey))
|
|
|
|
urlEncodedToken := base64.URLEncoding.EncodeToString([]byte(token))
|
2023-09-22 08:20:50 +00:00
|
|
|
c.Client.AddToken(account.AccountKey(), urlEncodedToken)
|
2023-09-22 13:47:24 +00:00
|
|
|
ctx := c.buildRequestContext(context.Background(), account)
|
2023-09-22 08:20:50 +00:00
|
|
|
return c.syncRemoteCollections(ctx, account)
|
|
|
|
}
|
|
|
|
|
2023-09-22 13:47:24 +00:00
|
|
|
func (c *ClICtrl) buildRequestContext(ctx context.Context, account model.Account) context.Context {
|
2023-09-22 08:20:50 +00:00
|
|
|
ctx = context.WithValue(ctx, "app", string(account.App))
|
|
|
|
ctx = context.WithValue(ctx, "account_id", account.AccountKey())
|
|
|
|
return ctx
|
2023-09-22 04:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var dataCategories = []string{"remote-collections", "local-collections", "remote-files", "local-files", "remote-collection-removed", "remote-files-removed"}
|
|
|
|
|
2023-09-22 08:20:50 +00:00
|
|
|
func createDataBuckets(db *bolt.DB, account model.Account) error {
|
2023-09-22 04:44:22 +00:00
|
|
|
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 {
|
2023-09-22 08:20:50 +00:00
|
|
|
_, err := dataBucket.CreateBucketIfNotExists([]byte(fmt.Sprintf(category)))
|
2023-09-22 04:44:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2023-09-22 08:20:50 +00:00
|
|
|
|
|
|
|
func (c *ClICtrl) syncRemoteCollections(ctx context.Context, info model.Account) error {
|
|
|
|
collections, err := c.Client.GetCollections(ctx, 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to get collections: %s\n", err)
|
|
|
|
return err
|
|
|
|
}
|
2023-09-22 13:37:12 +00:00
|
|
|
masterKey := accountMasterKey[info.AccountKey()]
|
2023-09-22 08:20:50 +00:00
|
|
|
for _, collection := range collections {
|
2023-09-22 13:37:12 +00:00
|
|
|
if collection.Owner.ID != info.UserID {
|
|
|
|
fmt.Printf("Skipping collection %d\n", collection.ID)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
collectionKey := collection.GetCollectionKey(masterKey)
|
|
|
|
name, nameErr := enteCrypto.SecretBoxOpenBase64(collection.EncryptedName, collection.NameDecryptionNonce, collectionKey)
|
|
|
|
if nameErr != nil {
|
|
|
|
log.Fatalf("failed to decrypt collection name: %v", nameErr)
|
|
|
|
}
|
|
|
|
fmt.Printf("Collection Name %s\n", string(name))
|
2023-09-22 08:20:50 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|