-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.go
125 lines (110 loc) · 3.49 KB
/
agent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"context"
"log"
"time"
"github.com/anyproto/any-sync/app"
"github.com/anyproto/anytype-heart/core/application"
bimport "github.com/anyproto/anytype-heart/core/block/import"
"github.com/anyproto/anytype-heart/core/block/process"
"github.com/anyproto/anytype-heart/core/event"
"github.com/anyproto/anytype-heart/pb"
"github.com/anyproto/anytype-heart/pkg/lib/bundle"
"github.com/anyproto/anytype-heart/pkg/lib/database"
"github.com/anyproto/anytype-heart/pkg/lib/localstore/objectstore"
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
"github.com/anyproto/anytype-heart/space"
"github.com/anyproto/anytype-heart/space/clientspace"
"github.com/anyproto/anytype-heart/util/pbtypes"
)
type AgentConfig struct {
Storagepath string
Mnemonic string
Accountid string
}
type agent struct {
AgentConfig
idle *idledetector
instance *application.Service
spaceService space.Service
personalSpace clientspace.Space
importer bimport.Importer
objectstoreservice objectstore.ObjectStore
}
func New(config AgentConfig) *agent {
return &agent{
AgentConfig: config,
}
}
func (a *agent) Init() {
a.idle = &idledetector{
duration: time.Second,
}
a.instance = application.New()
}
func (a *agent) Stop() {
a.instance.Stop()
}
func (a *agent) ImportSnapshots(snapshots []*pb.RpcObjectImportRequestSnapshot) error {
_, _, err := a.importer.Import(context.Background(), &pb.RpcObjectImportRequest{
SpaceId: a.personalSpace.Id(),
UpdateExistingObjects: true,
Type: model.Import_External,
Mode: pb.RpcObjectImportRequest_IGNORE_ERRORS,
NoProgress: true,
IsMigration: false,
IsNewSpace: false,
Snapshots: snapshots,
}, model.ObjectOrigin_none, process.NewNoOp())
return err
}
func (a *agent) IsObjectExistBySource(source string) bool {
records, total, _ := a.objectstoreservice.Query(database.Query{
Filters: []*model.BlockContentDataviewFilter{
{
RelationKey: bundle.RelationKeySource.String(),
Condition: model.BlockContentDataviewFilter_Equal,
Value: pbtypes.String(source),
},
},
})
return total > 0 || len(records) > 0
}
func (a *agent) Start() {
a.instance.SetClientVersion("win32cli", "0.0.0")
a.instance.SetEventSender(event.NewCallbackSender(
func(event *pb.Event) {
a.idle.NotIdle()
}))
log.Println("WalletRecover")
err := a.instance.WalletRecover(&pb.RpcWalletRecoverRequest{
RootPath: a.Storagepath,
Mnemonic: a.Mnemonic,
})
if err != nil {
panic(err)
}
log.Println("WalletCreateSession")
token, err := a.instance.CreateSession(&pb.RpcWalletCreateSessionRequest{
Mnemonic: a.Mnemonic,
})
if err != nil {
panic(err)
}
log.Println(token)
log.Println("AccountSelect")
_, err = a.instance.AccountSelect(context.Background(), &pb.RpcAccountSelectRequest{
Id: a.Accountid,
RootPath: a.Storagepath,
DisableLocalNetworkSync: false,
})
if err != nil {
panic(err)
}
// Wait sync to stabilize (no events for a second?)
a.idle.Wait()
a.spaceService = app.MustComponent[space.Service](a.instance.GetApp())
a.personalSpace, _ = a.spaceService.GetPersonalSpace(context.Background())
a.importer = app.MustComponent[bimport.Importer](a.instance.GetApp())
a.objectstoreservice = app.MustComponent[objectstore.ObjectStore](a.instance.GetApp())
}