Neeraj Gupta 1 year ago
parent
commit
f91a424e44
3 changed files with 27 additions and 18 deletions
  1. 10 17
      pkg/collections.go
  2. 1 1
      pkg/remote_sync.go
  3. 16 0
      pkg/store.go

+ 10 - 17
pkg/collections.go

@@ -9,17 +9,10 @@ import (
 	"strconv"
 )
 
-func (c *ClICtrl) syncRemoteCollections(ctx context.Context, info model.Account) error {
-	valueBytes, err := c.GetConfigValue(ctx, model.CollectionsSyncKey)
-	if err != nil {
-		return fmt.Errorf("failed to get last sync time: %s", err)
-	}
-	var lastSyncTime int64
-	if valueBytes != nil {
-		lastSyncTime, err = strconv.ParseInt(string(valueBytes), 10, 64)
-		if err != nil {
-			return err
-		}
+func (c *ClICtrl) fetchRemoteCollections(ctx context.Context, info model.Account) error {
+	lastSyncTime, err2 := c.GetInt64ConfigValue(ctx, model.CollectionsSyncKey)
+	if err2 != nil {
+		return err2
 	}
 	collections, err := c.Client.GetCollections(ctx, lastSyncTime)
 	if err != nil {
@@ -30,17 +23,17 @@ func (c *ClICtrl) syncRemoteCollections(ctx context.Context, info model.Account)
 		if lastSyncTime == 0 && collection.IsDeleted {
 			continue
 		}
-		album, err2 := c.mapCollectionToAlbum(ctx, collection)
-		if err2 != nil {
-			return err2
+		album, mapErr := c.mapCollectionToAlbum(ctx, collection)
+		if mapErr != nil {
+			return mapErr
 		}
 		if album.LastUpdatedAt > maxUpdated {
 			maxUpdated = album.LastUpdatedAt
 		}
 		albumJson := encoding.MustMarshalJSON(album)
-		err := c.PutValue(ctx, model.RemoteAlbums, []byte(strconv.FormatInt(album.ID, 10)), albumJson)
-		if err != nil {
-			return err
+		putErr := c.PutValue(ctx, model.RemoteAlbums, []byte(strconv.FormatInt(album.ID, 10)), albumJson)
+		if putErr != nil {
+			return putErr
 		}
 		debuglog.PrintAlbum(album)
 	}

+ 1 - 1
pkg/remote_sync.go

@@ -21,7 +21,7 @@ func (c *ClICtrl) SyncAccount(account model.Account) error {
 		return err
 	}
 	c.Client.AddToken(account.AccountKey(), base64.URLEncoding.EncodeToString(secretInfo.Token))
-	return c.syncRemoteCollections(ctx, account)
+	return c.fetchRemoteCollections(ctx, account)
 }
 
 func (c *ClICtrl) buildRequestContext(ctx context.Context, account model.Account) context.Context {

+ 16 - 0
pkg/store.go

@@ -5,6 +5,7 @@ import (
 	"context"
 	"fmt"
 	"log"
+	"strconv"
 	"time"
 
 	bolt "go.etcd.io/bbolt"
@@ -31,6 +32,21 @@ func (c *ClICtrl) GetConfigValue(ctx context.Context, key string) ([]byte, error
 	return value, err
 }
 
+func (c *ClICtrl) GetInt64ConfigValue(ctx context.Context, key string) (int64, error) {
+	value, err := c.GetConfigValue(ctx, key)
+	if err != nil {
+		return 0, err
+	}
+	var result int64
+	if value != nil {
+		result, err = strconv.ParseInt(string(value), 10, 64)
+		if err != nil {
+			return 0, err
+		}
+	}
+	return result, nil
+}
+
 func (c *ClICtrl) PutConfigValue(ctx context.Context, key string, value []byte) error {
 	return c.DB.Update(func(tx *bolt.Tx) error {
 		kvBucket, err := getAccountStore(ctx, tx, model.KVConfig)