Skip to content

Commit

Permalink
coreapi offline: Address review
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Dec 20, 2018
1 parent 190728f commit 6044d2a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
10 changes: 4 additions & 6 deletions core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ type CoreAPI struct {

pubSub *pubsub.PubSub

// TODO: this can be generalized to all functions when we implement some
// api based security mechanism
isPublishAllowed func() error
isOnline func(allowOffline bool) error
checkPublishAllowed func() error
checkOnline func(allowOffline bool) error

// ONLY for re-applying options in WithOptions, DO NOT USE ANYWHERE ELSE
nd *core.IpfsNode
Expand Down Expand Up @@ -178,14 +176,14 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
parentOpts: settings,
}

subApi.isOnline = func(allowOffline bool) error {
subApi.checkOnline = func(allowOffline bool) error {
if !n.OnlineMode() && !allowOffline {
return coreiface.ErrOffline
}
return nil
}

subApi.isPublishAllowed = func() error {
subApi.checkPublishAllowed = func() error {
if n.Mounts.Ipns != nil && n.Mounts.Ipns.IsActive() {
return errors.New("cannot manually publish while IPNS is mounted")
}
Expand Down
6 changes: 3 additions & 3 deletions core/coreapi/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
type DhtAPI CoreAPI

func (api *DhtAPI) FindPeer(ctx context.Context, p peer.ID) (pstore.PeerInfo, error) {
err := api.isOnline(false)
err := api.checkOnline(false)
if err != nil {
return pstore.PeerInfo{}, err
}
Expand All @@ -40,7 +40,7 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p coreiface.Path, opts ...
return nil, err
}

err = api.isOnline(false)
err = api.checkOnline(false)
if err != nil {
return nil, err
}
Expand All @@ -65,7 +65,7 @@ func (api *DhtAPI) Provide(ctx context.Context, path coreiface.Path, opts ...cao
return err
}

err = api.isOnline(false)
err = api.checkOnline(false)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions core/coreapi/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (e *ipnsEntry) Value() coreiface.Path {

// 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) {
if err := api.isPublishAllowed(); err != nil {
if err := api.checkPublishAllowed(); err != nil {
return nil, err
}

Expand All @@ -45,7 +45,7 @@ func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopt
return nil, err
}

err = api.isOnline(options.AllowOffline)
err = api.checkOnline(options.AllowOffline)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.Name
return nil, err
}

err = api.isOnline(true)
err = api.checkOnline(true)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion core/coreapi/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}

err := api.isOnline(false)
err := api.checkOnline(false)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 6044d2a

Please sign in to comment.