Skip to content

Commit

Permalink
Merge pull request #142 from anyproto/go-1621-pb-imported-object-dont…
Browse files Browse the repository at this point in the history
…-have-lastmodifieddate

explicitly set createdDate in the importer
  • Loading branch information
requilence authored Jun 30, 2023
2 parents 5e2e72a + cd8099e commit e9ba62f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions core/block/import/converter/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func GetCommonDetails(sourcePath, name, emoji string) *types.Struct {
bundle.RelationKeyName.String(): pbtypes.String(name),
bundle.RelationKeySourceFilePath.String(): pbtypes.String(sourcePath),
bundle.RelationKeyIconEmoji.String(): pbtypes.String(emoji),
bundle.RelationKeyCreatedDate.String(): pbtypes.Int64(time.Now().Unix()), // this relation will be after used in the tree header
}
return &types.Struct{Fields: fields}
}
Expand Down
14 changes: 13 additions & 1 deletion core/block/import/objectcreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package importer
import (
"context"
"fmt"
"path"
"strings"
"sync"

Expand Down Expand Up @@ -87,7 +88,18 @@ func (oc *ObjectCreator) Create(ctx *session.Context,
st := state.NewDocFromSnapshot(newID, sn.Snapshot).(*state.State)
st.SetRootId(newID)
// explicitly set last modified date, because all local details removed in NewDocFromSnapshot; createdDate covered in the object header
st.SetLastModified(pbtypes.GetInt64(sn.Snapshot.Data.Details, bundle.RelationKeyLastModifiedDate.String()), oc.core.ProfileID())
lastModifiedDate := pbtypes.GetInt64(sn.Snapshot.Data.Details, bundle.RelationKeyLastModifiedDate.String())
createdDate := pbtypes.GetInt64(sn.Snapshot.Data.Details, bundle.RelationKeyCreatedDate.String())
if lastModifiedDate == 0 {
if createdDate != 0 {
lastModifiedDate = createdDate
} else {
// we can't fallback to time.Now() because it will be inconsistent with the time used in object tree header.
// So instead we should EXPLICITLY set creation date to the snapshot in all importers
log.With("objectID", sn.Id).With("ext", path.Ext(sn.FileName)).Warnf("both lastModifiedDate and createdDate are not set in the imported snapshot")
}
}
st.SetLastModified(lastModifiedDate, oc.core.ProfileID())
var filesToDelete []string
defer func() {
// delete file in ipfs if there is error after creation
Expand Down
6 changes: 6 additions & 0 deletions core/block/import/pb/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"path/filepath"
"strings"
"time"

"github.com/anyproto/any-sync/util/slice"
"github.com/gogo/protobuf/jsonpb"
Expand Down Expand Up @@ -293,6 +294,11 @@ func (p *Pb) fillDetails(name string, path string, mo *pb.SnapshotWithType) {
}
sourceDetail := converter.GetSourceDetail(name, path)
mo.Snapshot.Data.Details.Fields[bundle.RelationKeySourceFilePath.String()] = pbtypes.String(sourceDetail)

createdDate := pbtypes.GetInt64(mo.Snapshot.Data.Details, bundle.RelationKeyCreatedDate.String())
if createdDate == 0 {
mo.Snapshot.Data.Details.Fields[bundle.RelationKeyCreatedDate.String()] = pbtypes.Int64(time.Now().Unix())
}
}

func (p *Pb) Name() string {
Expand Down

0 comments on commit e9ba62f

Please sign in to comment.