diff --git a/core/block/import/converter/common.go b/core/block/import/converter/common.go index 59549dd6aa..fdb5bc208d 100644 --- a/core/block/import/converter/common.go +++ b/core/block/import/converter/common.go @@ -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} } diff --git a/core/block/import/objectcreator.go b/core/block/import/objectcreator.go index 00de8ff53b..eb44373d02 100644 --- a/core/block/import/objectcreator.go +++ b/core/block/import/objectcreator.go @@ -3,6 +3,7 @@ package importer import ( "context" "fmt" + "path" "strings" "sync" @@ -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 diff --git a/core/block/import/pb/converter.go b/core/block/import/pb/converter.go index a2ebf651a8..758b295545 100644 --- a/core/block/import/pb/converter.go +++ b/core/block/import/pb/converter.go @@ -5,6 +5,7 @@ import ( "io" "path/filepath" "strings" + "time" "github.com/anyproto/any-sync/util/slice" "github.com/gogo/protobuf/jsonpb" @@ -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 {