提交 6044d2ae 作者: Łukasz Magiera

coreapi offline: Address review

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 190728f0
...@@ -67,10 +67,8 @@ type CoreAPI struct { ...@@ -67,10 +67,8 @@ type CoreAPI struct {
pubSub *pubsub.PubSub pubSub *pubsub.PubSub
// TODO: this can be generalized to all functions when we implement some checkPublishAllowed func() error
// api based security mechanism checkOnline func(allowOffline bool) error
isPublishAllowed func() error
isOnline func(allowOffline bool) error
// ONLY for re-applying options in WithOptions, DO NOT USE ANYWHERE ELSE // ONLY for re-applying options in WithOptions, DO NOT USE ANYWHERE ELSE
nd *core.IpfsNode nd *core.IpfsNode
...@@ -178,14 +176,14 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e ...@@ -178,14 +176,14 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
parentOpts: settings, parentOpts: settings,
} }
subApi.isOnline = func(allowOffline bool) error { subApi.checkOnline = func(allowOffline bool) error {
if !n.OnlineMode() && !allowOffline { if !n.OnlineMode() && !allowOffline {
return coreiface.ErrOffline return coreiface.ErrOffline
} }
return nil return nil
} }
subApi.isPublishAllowed = func() error { subApi.checkPublishAllowed = func() error {
if n.Mounts.Ipns != nil && n.Mounts.Ipns.IsActive() { if n.Mounts.Ipns != nil && n.Mounts.Ipns.IsActive() {
return errors.New("cannot manually publish while IPNS is mounted") return errors.New("cannot manually publish while IPNS is mounted")
} }
......
...@@ -21,7 +21,7 @@ import ( ...@@ -21,7 +21,7 @@ import (
type DhtAPI CoreAPI type DhtAPI CoreAPI
func (api *DhtAPI) FindPeer(ctx context.Context, p peer.ID) (pstore.PeerInfo, error) { func (api *DhtAPI) FindPeer(ctx context.Context, p peer.ID) (pstore.PeerInfo, error) {
err := api.isOnline(false) err := api.checkOnline(false)
if err != nil { if err != nil {
return pstore.PeerInfo{}, err return pstore.PeerInfo{}, err
} }
...@@ -40,7 +40,7 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p coreiface.Path, opts ... ...@@ -40,7 +40,7 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p coreiface.Path, opts ...
return nil, err return nil, err
} }
err = api.isOnline(false) err = api.checkOnline(false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -65,7 +65,7 @@ func (api *DhtAPI) Provide(ctx context.Context, path coreiface.Path, opts ...cao ...@@ -65,7 +65,7 @@ func (api *DhtAPI) Provide(ctx context.Context, path coreiface.Path, opts ...cao
return err return err
} }
err = api.isOnline(false) err = api.checkOnline(false)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -36,7 +36,7 @@ func (e *ipnsEntry) Value() coreiface.Path { ...@@ -36,7 +36,7 @@ func (e *ipnsEntry) Value() coreiface.Path {
// Publish announces new IPNS name and returns the new IPNS entry. // Publish announces new IPNS name and returns the new IPNS entry.
func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopts.NamePublishOption) (coreiface.IpnsEntry, error) { func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopts.NamePublishOption) (coreiface.IpnsEntry, error) {
if err := api.isPublishAllowed(); err != nil { if err := api.checkPublishAllowed(); err != nil {
return nil, err return nil, err
} }
...@@ -45,7 +45,7 @@ func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopt ...@@ -45,7 +45,7 @@ func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopt
return nil, err return nil, err
} }
err = api.isOnline(options.AllowOffline) err = api.checkOnline(options.AllowOffline)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -87,7 +87,7 @@ func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.Name ...@@ -87,7 +87,7 @@ func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.Name
return nil, err return nil, err
} }
err = api.isOnline(true) err = api.checkOnline(true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -127,7 +127,7 @@ func (api *PubSubAPI) checkNode() (routing.IpfsRouting, error) { ...@@ -127,7 +127,7 @@ func (api *PubSubAPI) checkNode() (routing.IpfsRouting, error) {
return nil, errors.New("experimental pubsub feature not enabled. Run daemon with --enable-pubsub-experiment to use.") return nil, errors.New("experimental pubsub feature not enabled. Run daemon with --enable-pubsub-experiment to use.")
} }
err := api.isOnline(false) err := api.checkOnline(false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论