Add controller which calls different api methods
This commit is contained in:
parent
9692ebe887
commit
aa0d6ea155
@ -1,40 +1,79 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/client/clientspace"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/client/document"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/client/storage"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/account"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/keys/symmetric"
|
||||
"golang.org/x/exp/rand"
|
||||
)
|
||||
|
||||
type Controller interface {
|
||||
// DeriveSpace derives the space from current account
|
||||
DeriveSpace() (id string, err error)
|
||||
// CreateSpace creates new space with random data
|
||||
CreateSpace() (id string, err error)
|
||||
GetAllSpacesIds() (ids []string, err error)
|
||||
// AllSpaceIds returns ids of all spaces
|
||||
AllSpaceIds(spaceId string) (ids []string, err error)
|
||||
// LoadSpace asks node to load a particular space
|
||||
LoadSpace(id string) (err error)
|
||||
|
||||
// CreateDocument creates new document in space
|
||||
CreateDocument(spaceId string) (id string, err error)
|
||||
GetAllDocumentIds(spaceId string) (ids []string, err error)
|
||||
AddText(documentId, text string) (err error)
|
||||
DumpDocumentTree(documentId string) (err error)
|
||||
// AllDocumentIds gets all ids of documents in space
|
||||
AllDocumentIds(spaceId string) (ids []string, err error)
|
||||
// AddText adds text to space document
|
||||
AddText(spaceId, documentId, text string) (err error)
|
||||
// DumpDocumentTree dumps the tree data into string
|
||||
DumpDocumentTree(spaceId, documentId string) (dump string, err error)
|
||||
|
||||
GetValidInvites(spaceId string) (invites []string, err error)
|
||||
ValidInvites(spaceId string) (invites []string, err error)
|
||||
GenerateInvite(spaceId string) (invite string, err error)
|
||||
JoinSpace(invite string) (err error)
|
||||
}
|
||||
|
||||
type controller struct {
|
||||
spaceService clientspace.Service
|
||||
storageService storage.ClientStorage
|
||||
docService document.Service
|
||||
account account.Service
|
||||
}
|
||||
|
||||
func (c *controller) DeriveSpace() (id string, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
sp, err := c.spaceService.DeriveSpace(context.Background(), commonspace.SpaceDerivePayload{
|
||||
SigningKey: c.account.Account().SignKey,
|
||||
EncryptionKey: c.account.Account().EncKey,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
id = sp.Id()
|
||||
return
|
||||
}
|
||||
|
||||
func (c *controller) CreateSpace() (id string, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
key, err := symmetric.NewRandom()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
sp, err := c.spaceService.CreateSpace(context.Background(), commonspace.SpaceCreatePayload{
|
||||
SigningKey: c.account.Account().SignKey,
|
||||
EncryptionKey: c.account.Account().EncKey,
|
||||
ReadKey: key.Bytes(),
|
||||
ReplicationKey: rand.Uint64(),
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
id = sp.Id()
|
||||
return
|
||||
}
|
||||
|
||||
func (c *controller) GetAllSpacesIds() (ids []string, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
func (c *controller) AllSpaceIds(spaceId string) (ids []string, err error) {
|
||||
return c.storageService.AllSpaceIds()
|
||||
}
|
||||
|
||||
func (c *controller) LoadSpace(id string) (err error) {
|
||||
@ -43,26 +82,22 @@ func (c *controller) LoadSpace(id string) (err error) {
|
||||
}
|
||||
|
||||
func (c *controller) CreateDocument(spaceId string) (id string, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
return c.docService.CreateDocument(spaceId)
|
||||
}
|
||||
|
||||
func (c *controller) GetAllDocumentIds(spaceId string) (ids []string, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
func (c *controller) AllDocumentIds(spaceId string) (ids []string, err error) {
|
||||
return c.docService.AllDocumentIds(spaceId)
|
||||
}
|
||||
|
||||
func (c *controller) AddText(documentId, text string) (err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
func (c *controller) AddText(spaceId, documentId, text string) (err error) {
|
||||
return c.docService.AddText(spaceId, documentId, text)
|
||||
}
|
||||
|
||||
func (c *controller) DumpDocumentTree(documentId string) (err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
func (c *controller) DumpDocumentTree(spaceId, documentId string) (dump string, err error) {
|
||||
return c.docService.DumpDocumentTree(spaceId, documentId)
|
||||
}
|
||||
|
||||
func (c *controller) GetValidInvites(spaceId string) (invites []string, err error) {
|
||||
func (c *controller) ValidInvites(spaceId string) (invites []string, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
@ -55,10 +55,6 @@ func (s *service) Name() (name string) {
|
||||
}
|
||||
|
||||
func (s *service) Run(ctx context.Context) (err error) {
|
||||
go func() {
|
||||
time.Sleep(time.Second * 5)
|
||||
_, _ = s.GetSpace(ctx, "testDSpace")
|
||||
}()
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@ -17,7 +17,7 @@ type Service interface {
|
||||
app.Component
|
||||
updatelistener.UpdateListener
|
||||
CreateDocument(spaceId string) (id string, err error)
|
||||
GetAllDocumentIds(spaceId string) (ids []string, err error)
|
||||
AllDocumentIds(spaceId string) (ids []string, err error)
|
||||
AddText(spaceId, documentId, text string) (err error)
|
||||
DumpDocumentTree(spaceId, documentId string) (dump string, err error)
|
||||
}
|
||||
@ -60,7 +60,7 @@ func (s *service) CreateDocument(spaceId string) (id string, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (s *service) GetAllDocumentIds(spaceId string) (ids []string, err error) {
|
||||
func (s *service) AllDocumentIds(spaceId string) (ids []string, err error) {
|
||||
space, err := s.spaceService.GetSpace(context.Background(), spaceId)
|
||||
if err != nil {
|
||||
return
|
||||
|
||||
@ -58,7 +58,7 @@ func (t *textDocument) Tree() tree.ObjectTree {
|
||||
}
|
||||
|
||||
func (t *textDocument) AddText(text string) (err error) {
|
||||
content := &testchanges.TextContentValueOfTextAppend{
|
||||
content := &testchanges.TextContent_TextAppend{
|
||||
TextAppend: &testchanges.TextAppend{Text: text},
|
||||
}
|
||||
change := &testchanges.TextData{
|
||||
|
||||
@ -65,7 +65,7 @@ type spaceKeys struct {
|
||||
|
||||
func newSpaceKeys(spaceId string) spaceKeys {
|
||||
return spaceKeys{
|
||||
headerKey: storage.JoinStringsToBytes("space", spaceId),
|
||||
headerKey: storage.JoinStringsToBytes("space", "header", spaceId),
|
||||
treePrefixKey: storage.JoinStringsToBytes("space", spaceId, "t", "rootId"),
|
||||
}
|
||||
}
|
||||
@ -77,3 +77,15 @@ func (s spaceKeys) HeaderKey() []byte {
|
||||
func (s spaceKeys) TreeRootPrefix() []byte {
|
||||
return s.treePrefixKey
|
||||
}
|
||||
|
||||
type storageServiceKeys struct {
|
||||
spacePrefix []byte
|
||||
}
|
||||
|
||||
func newStorageServiceKeys() storageServiceKeys {
|
||||
return storageServiceKeys{spacePrefix: []byte("space/header")}
|
||||
}
|
||||
|
||||
func (s storageServiceKeys) SpacePrefix() []byte {
|
||||
return s.spacePrefix
|
||||
}
|
||||
|
||||
@ -1,23 +1,30 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/client/badgerprovider"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/storage"
|
||||
"github.com/dgraph-io/badger/v3"
|
||||
)
|
||||
|
||||
type storageService struct {
|
||||
db *badger.DB
|
||||
keys storageServiceKeys
|
||||
db *badger.DB
|
||||
}
|
||||
|
||||
func New() storage.SpaceStorageProvider {
|
||||
type ClientStorage interface {
|
||||
storage.SpaceStorageProvider
|
||||
AllSpaceIds() (ids []string, err error)
|
||||
}
|
||||
|
||||
func New() ClientStorage {
|
||||
return &storageService{}
|
||||
}
|
||||
|
||||
func (s *storageService) Init(a *app.App) (err error) {
|
||||
provider := a.MustComponent(badgerprovider.CName).(badgerprovider.BadgerProvider)
|
||||
s.db = provider.Badger()
|
||||
s.keys = newStorageServiceKeys()
|
||||
return
|
||||
}
|
||||
|
||||
@ -32,3 +39,26 @@ func (s *storageService) SpaceStorage(id string) (storage.SpaceStorage, error) {
|
||||
func (s *storageService) CreateSpaceStorage(payload storage.SpaceStorageCreatePayload) (storage.SpaceStorage, error) {
|
||||
return createSpaceStorage(s.db, payload)
|
||||
}
|
||||
|
||||
func (s *storageService) AllSpaceIds() (ids []string, err error) {
|
||||
err = s.db.View(func(txn *badger.Txn) error {
|
||||
opts := badger.DefaultIteratorOptions
|
||||
opts.PrefetchValues = false
|
||||
opts.Prefix = s.keys.SpacePrefix()
|
||||
|
||||
it := txn.NewIterator(opts)
|
||||
defer it.Close()
|
||||
|
||||
for it.Rewind(); it.Valid(); it.Next() {
|
||||
item := it.Item()
|
||||
id := item.Key()
|
||||
if len(id) <= len(s.keys.SpacePrefix())+1 {
|
||||
continue
|
||||
}
|
||||
id = id[len(s.keys.SpacePrefix())+1:]
|
||||
ids = append(ids, string(id))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user