1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-08 05:57:03 +09:00
any-sync/commonspace/settings/settingsstate/changefactory.go
2023-05-23 14:47:24 +02:00

69 lines
1.9 KiB
Go

package settingsstate
import (
"github.com/anyproto/any-sync/commonspace/spacesyncproto"
)
type ChangeFactory interface {
CreateObjectDeleteChange(id string, state *State, isSnapshot bool) (res []byte, err error)
CreateSpaceDeleteChange(peerId string, state *State, isSnapshot bool) (res []byte, err error)
}
func NewChangeFactory() ChangeFactory {
return &changeFactory{}
}
type changeFactory struct {
}
func (c *changeFactory) CreateObjectDeleteChange(id string, state *State, isSnapshot bool) (res []byte, err error) {
content := &spacesyncproto.SpaceSettingsContent_ObjectDelete{
ObjectDelete: &spacesyncproto.ObjectDelete{Id: id},
}
change := &spacesyncproto.SettingsData{
Content: []*spacesyncproto.SpaceSettingsContent{
{Value: content},
},
}
if isSnapshot {
change.Snapshot = c.makeSnapshot(state, id, "")
}
res, err = change.Marshal()
return
}
func (c *changeFactory) CreateSpaceDeleteChange(peerId string, state *State, isSnapshot bool) (res []byte, err error) {
content := &spacesyncproto.SpaceSettingsContent_SpaceDelete{
SpaceDelete: &spacesyncproto.SpaceDelete{DeleterPeerId: peerId},
}
change := &spacesyncproto.SettingsData{
Content: []*spacesyncproto.SpaceSettingsContent{
{Value: content},
},
}
if isSnapshot {
change.Snapshot = c.makeSnapshot(state, "", peerId)
}
res, err = change.Marshal()
return
}
func (c *changeFactory) makeSnapshot(state *State, objectId, deleterPeer string) *spacesyncproto.SpaceSettingsSnapshot {
var (
deletedIds = make([]string, 0, len(state.DeletedIds)+1)
deleterId = state.DeleterId
)
if objectId != "" {
deletedIds = append(deletedIds, objectId)
}
if deleterPeer != "" {
deleterId = deleterPeer
}
for id := range state.DeletedIds {
deletedIds = append(deletedIds, id)
}
return &spacesyncproto.SpaceSettingsSnapshot{
DeletedIds: deletedIds,
DeleterPeerId: deleterId,
}
}