mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-09 17:44:59 +09:00
GO-1084: Rearrange packages
This commit is contained in:
parent
19eb080911
commit
f86e9808a8
10 changed files with 165 additions and 204 deletions
|
@ -1,96 +0,0 @@
|
|||
package change
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/anytypeio/go-anytype-middleware/core/block/editor/state"
|
||||
)
|
||||
|
||||
func NewStateCache() *stateCache {
|
||||
return &stateCache{
|
||||
states: make(map[string]struct {
|
||||
refs int
|
||||
state *state.State
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
type stateCache struct {
|
||||
states map[string]struct {
|
||||
refs int
|
||||
state *state.State
|
||||
}
|
||||
}
|
||||
|
||||
func (sc *stateCache) Set(id string, s *state.State, refs int) {
|
||||
sc.states[id] = struct {
|
||||
refs int
|
||||
state *state.State
|
||||
}{refs: refs, state: s}
|
||||
}
|
||||
|
||||
func (sc *stateCache) Get(id string) *state.State {
|
||||
item := sc.states[id]
|
||||
item.refs--
|
||||
if item.refs == 0 {
|
||||
delete(sc.states, id)
|
||||
} else {
|
||||
sc.states[id] = item
|
||||
}
|
||||
return item.state
|
||||
}
|
||||
|
||||
// Simple implementation hopes for CRDT and ignores errors. No merge
|
||||
func BuildStateSimpleCRDT(root *state.State, t *Tree) (s *state.State, changesApplied int, err error) {
|
||||
var (
|
||||
startId string
|
||||
applyRoot bool
|
||||
st = time.Now()
|
||||
lastChange *Change
|
||||
)
|
||||
if startId = root.ChangeId(); startId == "" {
|
||||
startId = t.RootId()
|
||||
applyRoot = true
|
||||
}
|
||||
|
||||
var lastMigrationVersion uint32
|
||||
|
||||
t.Iterate(startId, func(c *Change) (isContinue bool) {
|
||||
if c.Version > lastMigrationVersion {
|
||||
lastMigrationVersion = c.Version
|
||||
}
|
||||
changesApplied++
|
||||
lastChange = c
|
||||
if startId == c.Id {
|
||||
s = root.NewState()
|
||||
if applyRoot {
|
||||
s.ApplyChangeIgnoreErr(c.Change.Content...)
|
||||
s.SetChangeId(c.Id)
|
||||
s.AddFileKeys(c.FileKeys...)
|
||||
}
|
||||
return true
|
||||
}
|
||||
ns := s.NewState()
|
||||
ns.ApplyChangeIgnoreErr(c.Change.Content...)
|
||||
ns.SetChangeId(c.Id)
|
||||
ns.AddFileKeys(c.FileKeys...)
|
||||
_, _, err = state.ApplyStateFastOne(ns)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
if err != nil {
|
||||
return nil, changesApplied, err
|
||||
}
|
||||
if lastChange != nil {
|
||||
s.SetLastModified(lastChange.Timestamp, lastChange.Account)
|
||||
}
|
||||
|
||||
fmt.Println("SET MIG VERSION", s.RootId(), lastMigrationVersion)
|
||||
s.SetMigrationVersion(lastMigrationVersion)
|
||||
|
||||
log.Infof("build state (crdt): changes: %d; dur: %v;", changesApplied, time.Since(st))
|
||||
return s, changesApplied, err
|
||||
}
|
138
core/account.go
138
core/account.go
|
@ -19,33 +19,29 @@ import (
|
|||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/anytypeio/go-anytype-middleware/core/block/editor/state"
|
||||
"github.com/anytypeio/go-anytype-middleware/core/configfetcher"
|
||||
"github.com/anytypeio/go-anytype-middleware/util/builtinobjects"
|
||||
|
||||
"github.com/anytypeio/any-sync/util/crypto"
|
||||
"github.com/libp2p/go-libp2p/core/peer"
|
||||
|
||||
"github.com/anytypeio/any-sync/app"
|
||||
"github.com/anytypeio/any-sync/commonspace/object/treemanager"
|
||||
"github.com/anytypeio/any-sync/commonspace/spacesyncproto"
|
||||
"github.com/anytypeio/any-sync/util/crypto"
|
||||
"github.com/libp2p/go-libp2p/core/peer"
|
||||
cp "github.com/otiai10/copy"
|
||||
|
||||
"github.com/anytypeio/go-anytype-middleware/core/anytype"
|
||||
"github.com/anytypeio/go-anytype-middleware/core/anytype/config"
|
||||
"github.com/anytypeio/go-anytype-middleware/core/block"
|
||||
"github.com/anytypeio/go-anytype-middleware/core/configfetcher"
|
||||
"github.com/anytypeio/go-anytype-middleware/core/filestorage"
|
||||
walletComp "github.com/anytypeio/go-anytype-middleware/core/wallet"
|
||||
"github.com/anytypeio/go-anytype-middleware/metrics"
|
||||
"github.com/anytypeio/go-anytype-middleware/pb"
|
||||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/bundle"
|
||||
cafePb "github.com/anytypeio/go-anytype-middleware/pkg/lib/cafe/pb"
|
||||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/core"
|
||||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/gateway"
|
||||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/localstore/addr"
|
||||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/pb/model"
|
||||
"github.com/anytypeio/go-anytype-middleware/space"
|
||||
"github.com/anytypeio/go-anytype-middleware/util/constant"
|
||||
"github.com/anytypeio/go-anytype-middleware/util/files"
|
||||
"github.com/anytypeio/go-anytype-middleware/util/pbtypes"
|
||||
)
|
||||
|
||||
|
@ -77,7 +73,7 @@ func checkInviteCode(cfg *config.Config, code string, account string) (errorCode
|
|||
|
||||
// TODO: here we always using the default cafe address, because we want to check invite code only on our server
|
||||
// this code should be removed with a public release
|
||||
req, err := http.NewRequest("POST", cfg.InviteServerURL+"/alpha-invite", bytes.NewBuffer(jsonStr))
|
||||
req, err := http.NewRequest("POST", cfg.CafeUrl()+"/alpha-invite", bytes.NewBuffer(jsonStr))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
checkNetError := func(err error) (netOpError bool, dnsError bool, offline bool) {
|
||||
|
@ -145,7 +141,7 @@ func checkInviteCode(cfg *config.Config, code string, account string) (errorCode
|
|||
if err != nil {
|
||||
return pb.RpcAccountCreateResponseError_UNKNOWN_ERROR, fmt.Errorf("failed to decode response json: %s", err.Error())
|
||||
}
|
||||
peerID, err := peer.Decode(cfg.InviteServerPublicKey)
|
||||
peerID, err := peer.Decode(cfg.CafePeerId)
|
||||
if err != nil {
|
||||
return pb.RpcAccountCreateResponseError_UNKNOWN_ERROR, fmt.Errorf("failed to decode cafe pubkey: %s", err.Error())
|
||||
}
|
||||
|
@ -168,50 +164,32 @@ func checkInviteCode(cfg *config.Config, code string, account string) (errorCode
|
|||
return pb.RpcAccountCreateResponseError_NULL, nil
|
||||
}
|
||||
|
||||
func (mw *Middleware) refreshRemoteAccountState() {
|
||||
func (mw *Middleware) getCafeAccount() *cafePb.AccountState {
|
||||
fetcher := mw.app.MustComponent(configfetcher.CName).(configfetcher.ConfigFetcher)
|
||||
|
||||
return fetcher.GetAccountState()
|
||||
}
|
||||
|
||||
func (mw *Middleware) refetch() {
|
||||
fetcher := mw.app.MustComponent(configfetcher.CName).(configfetcher.ConfigFetcher)
|
||||
|
||||
fetcher.Refetch()
|
||||
}
|
||||
|
||||
func (mw *Middleware) getAnalyticsId(bs *block.Service, accountId string) (string, error) {
|
||||
conf := mw.app.MustComponent(config.CName).(*config.Config)
|
||||
if conf.AnalyticsId != "" {
|
||||
return conf.AnalyticsId, nil
|
||||
}
|
||||
var analyticsId string
|
||||
sb, err := bs.PickBlock(context.Background(), accountId)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
s := sb.NewState().GetSetting(state.SettingsAnalyticsId)
|
||||
if s == nil {
|
||||
log.Errorf("analytics id not found")
|
||||
} else {
|
||||
analyticsId = s.GetStringValue()
|
||||
}
|
||||
|
||||
return analyticsId, err
|
||||
}
|
||||
|
||||
func (mw *Middleware) getInfo(bs *block.Service) *model.AccountInfo {
|
||||
func (mw *Middleware) getInfo() *model.AccountInfo {
|
||||
at := mw.app.MustComponent(core.CName).(core.Service)
|
||||
gwAddr := mw.app.MustComponent(gateway.CName).(gateway.Gateway).Addr()
|
||||
wallet := mw.app.MustComponent(walletComp.CName).(walletComp.Wallet)
|
||||
deviceKey := wallet.GetDevicePrivkey()
|
||||
deviceId := deviceKey.GetPublic().Account()
|
||||
|
||||
analyticsId, err := mw.getAnalyticsId(bs, at.PredefinedBlocks().Account)
|
||||
if err != nil {
|
||||
log.Errorf("failed to get analytics id: %s", err.Error())
|
||||
}
|
||||
|
||||
if gwAddr != "" {
|
||||
gwAddr = "http://" + gwAddr
|
||||
}
|
||||
|
||||
cfg := config.ConfigRequired{}
|
||||
err = files.GetFileConfig(filepath.Join(wallet.RepoPath(), config.ConfigFileName), &cfg)
|
||||
if err != nil || cfg.CustomFileStorePath == "" {
|
||||
config.GetFileConfig(filepath.Join(wallet.RepoPath(), config.ConfigFileName), &cfg)
|
||||
if cfg.CustomFileStorePath == "" {
|
||||
cfg.CustomFileStorePath = wallet.RepoPath()
|
||||
}
|
||||
|
||||
|
@ -227,7 +205,6 @@ func (mw *Middleware) getInfo(bs *block.Service) *model.AccountInfo {
|
|||
DeviceId: deviceId,
|
||||
LocalStoragePath: cfg.CustomFileStorePath,
|
||||
TimeZone: cfg.TimeZone,
|
||||
AnalyticsId: analyticsId,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -238,6 +215,12 @@ func (mw *Middleware) AccountCreate(cctx context.Context, req *pb.RpcAccountCrea
|
|||
defer mw.m.Unlock()
|
||||
response := func(account *model.Account, code pb.RpcAccountCreateResponseErrorCode, err error) *pb.RpcAccountCreateResponse {
|
||||
var clientConfig *pb.RpcAccountConfig
|
||||
if account != nil && err == nil {
|
||||
cafeAccount := mw.getCafeAccount()
|
||||
|
||||
clientConfig = convertToRpcAccountConfig(cafeAccount.Config) // to support deprecated clients
|
||||
enrichWithCafeAccount(account, cafeAccount)
|
||||
}
|
||||
m := &pb.RpcAccountCreateResponse{Config: clientConfig, Account: account, Error: &pb.RpcAccountCreateResponseError{Code: code}}
|
||||
if err != nil {
|
||||
m.Error.Description = err.Error()
|
||||
|
@ -251,7 +234,6 @@ func (mw *Middleware) AccountCreate(cctx context.Context, req *pb.RpcAccountCrea
|
|||
}
|
||||
|
||||
cfg := anytype.BootstrapConfig(true, os.Getenv("ANYTYPE_STAGING") == "1", true, true)
|
||||
|
||||
derivationResult, err := core.WalletAccountAt(mw.mnemonic, 0)
|
||||
if err != nil {
|
||||
return response(nil, pb.RpcAccountCreateResponseError_UNKNOWN_ERROR, err)
|
||||
|
@ -271,7 +253,7 @@ func (mw *Middleware) AccountCreate(cctx context.Context, req *pb.RpcAccountCrea
|
|||
if err != nil {
|
||||
return response(nil, pb.RpcAccountCreateResponseError_FAILED_TO_CREATE_LOCAL_REPO, err)
|
||||
}
|
||||
if err := files.WriteJsonConfig(configPath, config.ConfigRequired{CustomFileStorePath: storePath}); err != nil {
|
||||
if err := config.WriteJsonConfig(configPath, config.ConfigRequired{CustomFileStorePath: storePath}); err != nil {
|
||||
return response(nil, pb.RpcAccountCreateResponseError_FAILED_TO_WRITE_CONFIG, err)
|
||||
}
|
||||
}
|
||||
|
@ -319,7 +301,7 @@ func (mw *Middleware) AccountCreate(cctx context.Context, req *pb.RpcAccountCrea
|
|||
}
|
||||
|
||||
newAcc.Name = req.Name
|
||||
newAcc.Info = mw.getInfo(bs)
|
||||
newAcc.Info = mw.getInfo()
|
||||
|
||||
coreService := mw.app.MustComponent(core.CName).(core.Service)
|
||||
if err = bs.SetDetails(nil, pb.RpcObjectSetDetailsRequest{
|
||||
|
@ -372,6 +354,12 @@ func (mw *Middleware) AccountRecover(cctx context.Context, _ *pb.RpcAccountRecov
|
|||
func (mw *Middleware) AccountSelect(cctx context.Context, req *pb.RpcAccountSelectRequest) *pb.RpcAccountSelectResponse {
|
||||
response := func(account *model.Account, code pb.RpcAccountSelectResponseErrorCode, err error) *pb.RpcAccountSelectResponse {
|
||||
var clientConfig *pb.RpcAccountConfig
|
||||
if account != nil {
|
||||
cafeAccount := mw.getCafeAccount()
|
||||
|
||||
clientConfig = convertToRpcAccountConfig(cafeAccount.Config) // to support deprecated clients
|
||||
enrichWithCafeAccount(account, cafeAccount)
|
||||
}
|
||||
m := &pb.RpcAccountSelectResponse{Config: clientConfig, Account: account, Error: &pb.RpcAccountSelectResponseError{Code: code}}
|
||||
if err != nil {
|
||||
m.Error.Description = err.Error()
|
||||
|
@ -392,10 +380,9 @@ func (mw *Middleware) AccountSelect(cctx context.Context, req *pb.RpcAccountSele
|
|||
|
||||
// we already have this account running, lets just stop events
|
||||
if mw.app != nil && req.Id == mw.app.MustComponent(walletComp.CName).(walletComp.Wallet).GetAccountPrivkey().GetPublic().Account() {
|
||||
bs := mw.app.MustComponent(treemanager.CName).(*block.Service)
|
||||
bs.CloseBlocks()
|
||||
mw.app.MustComponent(treemanager.CName).(*block.Service).CloseBlocks()
|
||||
acc := &model.Account{Id: req.Id}
|
||||
acc.Info = mw.getInfo(bs)
|
||||
acc.Info = mw.getInfo()
|
||||
return response(acc, pb.RpcAccountSelectResponseError_NULL, nil)
|
||||
}
|
||||
|
||||
|
@ -448,7 +435,7 @@ func (mw *Middleware) AccountSelect(cctx context.Context, req *pb.RpcAccountSele
|
|||
}
|
||||
|
||||
acc := &model.Account{Id: req.Id}
|
||||
acc.Info = mw.getInfo(mw.app.MustComponent(block.CName).(*block.Service))
|
||||
acc.Info = mw.getInfo()
|
||||
return response(acc, pb.RpcAccountSelectResponseError_NULL, nil)
|
||||
}
|
||||
|
||||
|
@ -513,7 +500,7 @@ func (mw *Middleware) AccountMove(cctx context.Context, req *pb.RpcAccountMoveRe
|
|||
configPath := conf.GetConfigPath()
|
||||
srcPath := conf.RepoPath
|
||||
fileConf := config.ConfigRequired{}
|
||||
if err := files.GetFileConfig(configPath, &fileConf); err != nil {
|
||||
if err := config.GetFileConfig(configPath, &fileConf); err != nil {
|
||||
return response(pb.RpcAccountMoveResponseError_FAILED_TO_GET_CONFIG, err)
|
||||
}
|
||||
if fileConf.CustomFileStorePath != "" {
|
||||
|
@ -555,7 +542,7 @@ func (mw *Middleware) AccountMove(cctx context.Context, req *pb.RpcAccountMoveRe
|
|||
}
|
||||
}
|
||||
|
||||
err = files.WriteJsonConfig(configPath, config.ConfigRequired{CustomFileStorePath: destination})
|
||||
err = config.WriteJsonConfig(configPath, config.ConfigRequired{CustomFileStorePath: destination})
|
||||
if err != nil {
|
||||
return response(pb.RpcAccountMoveResponseError_FAILED_TO_WRITE_CONFIG, err)
|
||||
}
|
||||
|
@ -598,9 +585,7 @@ func (mw *Middleware) AccountDelete(cctx context.Context, req *pb.RpcAccountDele
|
|||
return
|
||||
})
|
||||
|
||||
// so we will receive updated account status
|
||||
mw.refreshRemoteAccountState()
|
||||
|
||||
mw.refetch()
|
||||
if err == nil {
|
||||
return response(st, pb.RpcAccountDeleteResponseError_NULL, nil)
|
||||
}
|
||||
|
@ -635,7 +620,7 @@ func (mw *Middleware) AccountConfigUpdate(_ context.Context, req *pb.RpcAccountC
|
|||
cfg := config.ConfigRequired{}
|
||||
cfg.TimeZone = req.TimeZone
|
||||
cfg.CustomFileStorePath = req.IPFSStorageAddr
|
||||
err := files.WriteJsonConfig(conf.GetConfigPath(), cfg)
|
||||
err := config.WriteJsonConfig(conf.GetConfigPath(), cfg)
|
||||
if err != nil {
|
||||
return response(pb.RpcAccountConfigUpdateResponseError_FAILED_TO_WRITE_CONFIG, err)
|
||||
}
|
||||
|
@ -649,7 +634,7 @@ func (mw *Middleware) AccountRemoveLocalData() error {
|
|||
|
||||
configPath := conf.GetConfigPath()
|
||||
fileConf := config.ConfigRequired{}
|
||||
if err := files.GetFileConfig(configPath, &fileConf); err != nil {
|
||||
if err := config.GetFileConfig(configPath, &fileConf); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -751,12 +736,6 @@ func (mw *Middleware) createAccountFromExport(profile *pb.Profile, req *pb.RpcAc
|
|||
return "", pb.RpcAccountRecoverFromLegacyExportResponseError_UNKNOWN_ERROR, err
|
||||
}
|
||||
|
||||
if profile.AnalyticsId != "" {
|
||||
cfg.AnalyticsId = profile.AnalyticsId
|
||||
} else {
|
||||
cfg.AnalyticsId = metrics.GenerateAnalyticsId()
|
||||
}
|
||||
|
||||
err = mw.startApp(cfg, res, err)
|
||||
if err != nil {
|
||||
return "", pb.RpcAccountRecoverFromLegacyExportResponseError_UNKNOWN_ERROR, err
|
||||
|
@ -767,10 +746,6 @@ func (mw *Middleware) createAccountFromExport(profile *pb.Profile, req *pb.RpcAc
|
|||
return "", pb.RpcAccountRecoverFromLegacyExportResponseError_UNKNOWN_ERROR, err
|
||||
}
|
||||
|
||||
if err = mw.app.MustComponent(builtinobjects.CName).(builtinobjects.BuiltinObjects).InjectMigrationDashboard(); err != nil {
|
||||
return "", pb.RpcAccountRecoverFromLegacyExportResponseError_BAD_INPUT, err
|
||||
}
|
||||
|
||||
return address, pb.RpcAccountRecoverFromLegacyExportResponseError_NULL, nil
|
||||
}
|
||||
|
||||
|
@ -829,17 +804,15 @@ func buildDetails(profile *pb.Profile, icon int64) (
|
|||
profileDetails = []*pb.RpcObjectSetDetailsDetail{{
|
||||
Key: bundle.RelationKeyName.String(),
|
||||
Value: pbtypes.String(profile.Name),
|
||||
}, {
|
||||
Key: bundle.RelationKeyIconImage.String(),
|
||||
Value: pbtypes.String(profile.Avatar),
|
||||
}}
|
||||
if profile.Avatar == "" {
|
||||
profileDetails = append(profileDetails, &pb.RpcObjectSetDetailsDetail{
|
||||
Key: bundle.RelationKeyIconOption.String(),
|
||||
Value: pbtypes.Int64(icon),
|
||||
})
|
||||
} else {
|
||||
profileDetails = append(profileDetails, &pb.RpcObjectSetDetailsDetail{
|
||||
Key: bundle.RelationKeyIconImage.String(),
|
||||
Value: pbtypes.String(profile.Avatar),
|
||||
})
|
||||
}
|
||||
accountDetails = []*pb.RpcObjectSetDetailsDetail{{
|
||||
Key: bundle.RelationKeyIconOption.String(),
|
||||
|
@ -873,3 +846,30 @@ func (mw *Middleware) isAccountExistsOnDisk(account string) bool {
|
|||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func convertToRpcAccountConfig(cfg *cafePb.Config) *pb.RpcAccountConfig {
|
||||
return &pb.RpcAccountConfig{
|
||||
EnableDataview: cfg.EnableDataview,
|
||||
EnableDebug: cfg.EnableDebug,
|
||||
EnablePrereleaseChannel: cfg.EnablePrereleaseChannel,
|
||||
Extra: cfg.Extra,
|
||||
EnableSpaces: cfg.EnableSpaces,
|
||||
}
|
||||
}
|
||||
|
||||
func enrichWithCafeAccount(acc *model.Account, cafeAcc *cafePb.AccountState) {
|
||||
cfg := cafeAcc.Config
|
||||
acc.Config = &model.AccountConfig{
|
||||
EnableDataview: cfg.EnableDataview,
|
||||
EnableDebug: cfg.EnableDebug,
|
||||
EnablePrereleaseChannel: cfg.EnablePrereleaseChannel,
|
||||
Extra: cfg.Extra,
|
||||
EnableSpaces: cfg.EnableSpaces,
|
||||
}
|
||||
|
||||
st := cafeAcc.Status
|
||||
acc.Status = &model.AccountStatus{
|
||||
StatusType: model.AccountStatusType(st.Status),
|
||||
DeletionDate: st.DeletionDate,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"net"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/anytypeio/any-sync/app"
|
||||
|
@ -15,10 +16,10 @@ import (
|
|||
"gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/anytypeio/go-anytype-middleware/core/wallet"
|
||||
"github.com/anytypeio/go-anytype-middleware/metrics"
|
||||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/datastore/clientds"
|
||||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/ipfs"
|
||||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/logging"
|
||||
"github.com/anytypeio/go-anytype-middleware/util/files"
|
||||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/threads"
|
||||
)
|
||||
|
||||
var log = logging.Logger("anytype-config")
|
||||
|
@ -45,15 +46,25 @@ type Config struct {
|
|||
Offline bool
|
||||
DisableThreadsSyncEvents bool
|
||||
|
||||
RepoPath string
|
||||
AnalyticsId string
|
||||
RepoPath string
|
||||
|
||||
PrivateNetworkSecret string
|
||||
|
||||
SwarmLowWater int
|
||||
SwarmHighWater int
|
||||
BootstrapNodes []string
|
||||
RelayNodes []string
|
||||
|
||||
CafeAddr string
|
||||
CafeGrpcPort int
|
||||
CafeP2PPort int
|
||||
CafePeerId string
|
||||
CafeAPIInsecure bool
|
||||
|
||||
DebugAddr string
|
||||
LocalServerAddr string
|
||||
|
||||
InviteServerURL string
|
||||
InviteServerPublicKey string
|
||||
|
||||
Threads threads.Config
|
||||
DS clientds.Config
|
||||
FS FSConfig
|
||||
DisableFileConfig bool `ignored:"true"` // set in order to skip reading/writing config from/to file
|
||||
|
@ -75,19 +86,41 @@ const (
|
|||
)
|
||||
|
||||
var DefaultConfig = Config{
|
||||
Offline: false,
|
||||
Offline: false,
|
||||
SwarmLowWater: 10,
|
||||
SwarmHighWater: 50,
|
||||
LocalServerAddr: ":0",
|
||||
PrivateNetworkSecret: ipfs.IpfsPrivateNetworkKey,
|
||||
BootstrapNodes: []string{
|
||||
"/ip4/54.93.109.23/tcp/4001/p2p/QmZ4P1Q8HhtKpMshHorM2HDg4iVGZdhZ7YN7WeWDWFH3Hi", // fra1
|
||||
"/dns4/bootstrap2.anytype.io/tcp/4001/p2p/QmSxuiczQTjgj5agSoNtp4esSsj64RisDyKt2MCZQsKZUx", // sfo1
|
||||
"/dns4/bootstrap3.anytype.io/tcp/4001/p2p/QmUdDTWzgdcf4cM4aHeihoYSUfQJJbLVLTZFZvm1b46NNT", // sgp1
|
||||
},
|
||||
RelayNodes: []string{
|
||||
"/dns4/relay2.anytype.io/tcp/4101/p2p/12D3KooWMLuW43JqNzUHbXMJH2Ted5Nf26sxv1VMcZAxXV3d3YtB",
|
||||
"/dns4/relay1.anytype.io/tcp/4101/p2p/12D3KooWNPqCu4BC5WMBuHmqdiNWwAHGTNKbNy6JP5W1DML2psg1",
|
||||
},
|
||||
CafeAPIInsecure: false,
|
||||
CafeAddr: "cafe1.anytype.io",
|
||||
CafeP2PPort: 4001,
|
||||
CafeGrpcPort: 3006,
|
||||
CafePeerId: "12D3KooWKwPC165PptjnzYzGrEs7NSjsF5vvMmxmuqpA2VfaBbLw",
|
||||
|
||||
LocalServerAddr: ":0",
|
||||
DS: clientds.DefaultConfig,
|
||||
InviteServerPublicKey: "12D3KooWKwPC165PptjnzYzGrEs7NSjsF5vvMmxmuqpA2VfaBbLw",
|
||||
InviteServerURL: "https://cafe1.anytype.io",
|
||||
DS: clientds.DefaultConfig,
|
||||
Threads: threads.DefaultConfig,
|
||||
}
|
||||
|
||||
func WithNewAccount(isNewAccount bool) func(*Config) {
|
||||
return func(c *Config) {
|
||||
c.NewAccount = isNewAccount
|
||||
if isNewAccount {
|
||||
c.AnalyticsId = metrics.GenerateAnalyticsId()
|
||||
}
|
||||
}
|
||||
|
||||
func WithStagingCafe(isStaging bool) func(*Config) {
|
||||
return func(c *Config) {
|
||||
if isStaging {
|
||||
c.CafeAddr = "cafe-staging.anytype.io"
|
||||
c.CafePeerId = "12D3KooWPGR6LQyTEtBzFnJ7fGEMe6hKiQKeNof29zLH4bGq2djR"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -127,9 +160,35 @@ func New(options ...func(*Config)) *Config {
|
|||
for _, opt := range options {
|
||||
opt(&cfg)
|
||||
}
|
||||
cfg.Threads.CafeP2PAddr = cfg.CafeP2PFullAddr()
|
||||
cfg.Threads.CafePID = cfg.CafePeerId
|
||||
|
||||
return &cfg
|
||||
}
|
||||
|
||||
func (c *Config) CafeNodeGrpcAddr() string {
|
||||
return c.CafeAddr + ":" + strconv.Itoa(c.CafeGrpcPort)
|
||||
}
|
||||
|
||||
func (c *Config) CafeUrl() string {
|
||||
if net.ParseIP(c.CafeAddr) != nil {
|
||||
return c.CafeAddr
|
||||
}
|
||||
prefix := "https://"
|
||||
if c.CafeAPIInsecure {
|
||||
prefix = "http://"
|
||||
}
|
||||
return prefix + c.CafeAddr
|
||||
}
|
||||
|
||||
func (c *Config) CafeP2PFullAddr() string {
|
||||
prefix := "dns4"
|
||||
if net.ParseIP(c.CafeAddr) != nil {
|
||||
prefix = "ip4"
|
||||
}
|
||||
return fmt.Sprintf("/%s/%s/tcp/%d/p2p/%s", prefix, c.CafeAddr, c.CafeP2PPort, c.CafePeerId)
|
||||
}
|
||||
|
||||
func (c *Config) Init(a *app.App) (err error) {
|
||||
repoPath := a.MustComponent(wallet.CName).(wallet.Wallet).RepoPath()
|
||||
if err = c.initFromFileAndEnv(repoPath); err != nil {
|
||||
|
@ -140,22 +199,19 @@ func (c *Config) Init(a *app.App) (err error) {
|
|||
}
|
||||
|
||||
func (c *Config) initFromFileAndEnv(repoPath string) error {
|
||||
if repoPath == "" {
|
||||
return fmt.Errorf("repo is missing")
|
||||
}
|
||||
c.RepoPath = repoPath
|
||||
|
||||
if !c.DisableFileConfig {
|
||||
var confRequired ConfigRequired
|
||||
err := files.GetFileConfig(c.GetConfigPath(), &confRequired)
|
||||
err := GetFileConfig(c.GetConfigPath(), &confRequired)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get config from file: %s", err.Error())
|
||||
}
|
||||
|
||||
writeConfig := func() error {
|
||||
err = files.WriteJsonConfig(c.GetConfigPath(), c.ConfigRequired)
|
||||
err = WriteJsonConfig(c.GetConfigPath(), c.ConfigRequired)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to save required configuration to the cfg file: %s", err.Error())
|
||||
return fmt.Errorf("failed to save port to the cfg file: %s", err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -229,7 +285,7 @@ func (c *Config) DSConfig() clientds.Config {
|
|||
|
||||
func (c *Config) FSConfig() (FSConfig, error) {
|
||||
res := ConfigRequired{}
|
||||
err := files.GetFileConfig(c.GetConfigPath(), &res)
|
||||
err := GetFileConfig(c.GetConfigPath(), &res)
|
||||
if err != nil {
|
||||
return FSConfig{}, err
|
||||
}
|
||||
|
@ -237,6 +293,10 @@ func (c *Config) FSConfig() (FSConfig, error) {
|
|||
return FSConfig{IPFSStorageAddr: res.CustomFileStorePath}, nil
|
||||
}
|
||||
|
||||
func (c *Config) ThreadsConfig() threads.Config {
|
||||
return c.Threads
|
||||
}
|
||||
|
||||
func (c *Config) GetConfigPath() string {
|
||||
return filepath.Join(c.RepoPath, ConfigFileName)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package files
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
@ -37,7 +37,7 @@ func GetFileConfig(configPath string, cfg interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
//WriteJsonConfig - overwrites params in file only specified params which passed in cfg
|
||||
// WriteJsonConfig - overwrites params in file only specified params which passed in cfg
|
||||
// `json:",omitempty"` - is required tag for every field in cfg !!!
|
||||
func WriteJsonConfig(configPath string, cfg interface{}) error {
|
||||
oldCfg := make(map[string]interface{})
|
|
@ -1,14 +1,15 @@
|
|||
package files
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type testConfigOmitEmpty struct {
|
||||
One string `json:",omitempty"`
|
||||
Two int `json:",omitempty"`
|
||||
Two int `json:",omitempty"`
|
||||
}
|
||||
|
||||
type testConfig struct {
|
||||
|
@ -53,5 +54,3 @@ func TestFileConfig_WriteFileConfig(t *testing.T) {
|
|||
require.EqualValues(t, testConfig{Two: 2}, res)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -11,7 +11,6 @@ import (
|
|||
"github.com/anytypeio/go-anytype-middleware/core/block/process"
|
||||
files2 "github.com/anytypeio/go-anytype-middleware/core/files"
|
||||
"github.com/anytypeio/go-anytype-middleware/pb"
|
||||
"github.com/anytypeio/go-anytype-middleware/util/files"
|
||||
)
|
||||
|
||||
// TODO Move residual file methods here
|
||||
|
@ -70,7 +69,7 @@ func (s *Service) DownloadFile(req *pb.RpcFileDownloadRequest) (string, error) {
|
|||
fileName = f.Info().Name
|
||||
}
|
||||
|
||||
path, err := files.WriteReaderIntoFileReuseSameExistingFile(req.Path+string(os.PathSeparator)+fileName, countReader)
|
||||
path, err := files2.WriteReaderIntoFileReuseSameExistingFile(req.Path+string(os.PathSeparator)+fileName, countReader)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("save file: %w", err)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package anyblocks
|
||||
package anymark
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
@ -6,7 +6,7 @@ import (
|
|||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/pb/model"
|
||||
)
|
||||
|
||||
func PreprocessBlocks(blocks []*model.Block) (blocksOut []*model.Block) {
|
||||
func preprocessBlocks(blocks []*model.Block) (blocksOut []*model.Block) {
|
||||
|
||||
blocksOut = []*model.Block{}
|
||||
accum := []*model.Block{}
|
||||
|
@ -16,7 +16,7 @@ func PreprocessBlocks(blocks []*model.Block) (blocksOut []*model.Block) {
|
|||
accum = append(accum, b)
|
||||
} else {
|
||||
if len(accum) > 0 {
|
||||
blocksOut = append(blocksOut, CombineCodeBlocks(accum))
|
||||
blocksOut = append(blocksOut, combineCodeBlocks(accum))
|
||||
accum = []*model.Block{}
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,7 @@ func PreprocessBlocks(blocks []*model.Block) (blocksOut []*model.Block) {
|
|||
}
|
||||
|
||||
if len(accum) > 0 {
|
||||
blocksOut = append(blocksOut, CombineCodeBlocks(accum))
|
||||
blocksOut = append(blocksOut, combineCodeBlocks(accum))
|
||||
}
|
||||
|
||||
for _, b := range blocks {
|
||||
|
@ -40,7 +40,7 @@ func PreprocessBlocks(blocks []*model.Block) (blocksOut []*model.Block) {
|
|||
return blocksOut
|
||||
}
|
||||
|
||||
func CombineCodeBlocks(accum []*model.Block) (res *model.Block) {
|
||||
func combineCodeBlocks(accum []*model.Block) (res *model.Block) {
|
||||
var textArr []string
|
||||
|
||||
for _, b := range accum {
|
|
@ -11,7 +11,6 @@ import (
|
|||
"github.com/google/uuid"
|
||||
|
||||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/pb/model"
|
||||
"github.com/anytypeio/go-anytype-middleware/util/anyblocks"
|
||||
"github.com/anytypeio/go-anytype-middleware/util/text"
|
||||
)
|
||||
|
||||
|
@ -133,7 +132,7 @@ func (r *blocksRenderer) OpenNewTextBlock(style model.BlockContentTextStyle) {
|
|||
}
|
||||
|
||||
func (r *blocksRenderer) GetBlocks() []*model.Block {
|
||||
r.blocks = anyblocks.PreprocessBlocks(r.blocks)
|
||||
r.blocks = preprocessBlocks(r.blocks)
|
||||
return r.blocks
|
||||
}
|
||||
|
||||
|
|
|
@ -2,12 +2,12 @@ package files
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/stretchr/testify/require"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestWriteReaderIntoFileIgnoreSameExistingFile(t *testing.T) {
|
||||
t.Run("expect same path", func(t *testing.T) {
|
Loading…
Add table
Add a link
Reference in a new issue