ente/pkg/collections.go

46 lines
1.1 KiB
Go
Raw Normal View History

2023-09-23 10:45:10 +00:00
package pkg
import (
debuglog "cli-go/pkg/log"
2023-09-23 10:45:10 +00:00
"cli-go/pkg/model"
"context"
"fmt"
"strconv"
2023-09-23 10:45:10 +00:00
)
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
}
}
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)
}
maxUpdated := lastSyncTime
2023-09-23 10:45:10 +00:00
for _, collection := range collections {
album, err2 := c.mapCollectionToAlbum(ctx, collection)
if err2 != nil {
return err2
2023-09-23 10:45:10 +00:00
}
if album.LastUpdatedAt > maxUpdated {
maxUpdated = album.LastUpdatedAt
}
debuglog.PrintAlbum(album)
2023-09-23 10:45:10 +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
}