2023-09-23 10:45:10 +00:00
|
|
|
package pkg
|
|
|
|
|
|
|
|
import (
|
2023-09-24 05:34:51 +00:00
|
|
|
debuglog "cli-go/pkg/log"
|
2023-09-23 10:45:10 +00:00
|
|
|
"cli-go/pkg/model"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-09-24 20:21:11 +00:00
|
|
|
"strconv"
|
2023-09-23 10:45:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *ClICtrl) syncRemoteCollections(ctx context.Context, info model.Account) error {
|
2023-09-24 20:21:11 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
collections, err := c.Client.GetCollections(ctx, lastSyncTime)
|
2023-09-23 10:45:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get collections: %s", err)
|
|
|
|
}
|
2023-09-24 20:21:11 +00:00
|
|
|
maxUpdated := lastSyncTime
|
2023-09-23 10:45:10 +00:00
|
|
|
for _, collection := range collections {
|
2023-09-24 05:34:51 +00:00
|
|
|
album, err2 := c.mapCollectionToAlbum(ctx, collection)
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
2023-09-23 10:45:10 +00:00
|
|
|
}
|
2023-09-24 20:21:11 +00:00
|
|
|
if album.LastUpdatedAt > maxUpdated {
|
|
|
|
maxUpdated = album.LastUpdatedAt
|
|
|
|
}
|
2023-09-24 05:34:51 +00:00
|
|
|
debuglog.PrintAlbum(album)
|
2023-09-23 10:45:10 +00:00
|
|
|
}
|
2023-09-24 20:21:11 +00:00
|
|
|
if maxUpdated > lastSyncTime {
|
|
|
|
err = c.PutConfigValue(ctx, model.CollectionsSyncKey, []byte(strconv.FormatInt(maxUpdated, 10)))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to update last sync time: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2023-09-23 10:45:10 +00:00
|
|
|
return nil
|
|
|
|
}
|