github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/lib/follow.go (about)

     1  package lib
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/qri-io/dataset"
     7  	qhttp "github.com/qri-io/qri/lib/http"
     8  	"github.com/qri-io/qri/registry"
     9  	"github.com/qri-io/qri/repo"
    10  )
    11  
    12  // FollowMethods groups together methods for follows
    13  type FollowMethods struct {
    14  	d dispatcher
    15  }
    16  
    17  // Name returns the name of this method group
    18  func (m FollowMethods) Name() string {
    19  	return "follow"
    20  }
    21  
    22  // Attributes defines attributes for each method
    23  func (m FollowMethods) Attributes() map[string]AttributeSet {
    24  	return map[string]AttributeSet{
    25  		"get":    {Endpoint: qhttp.AERegistryGetFollowing, HTTPVerb: "POST"},
    26  		"follow": {Endpoint: qhttp.AERegistryFollow, HTTPVerb: "POST"},
    27  	}
    28  }
    29  
    30  // Get returns a list of datasets a user follows
    31  func (m FollowMethods) Get(ctx context.Context, p *registry.FollowGetParams) ([]*dataset.Dataset, error) {
    32  	got, _, err := m.d.Dispatch(ctx, dispatchMethodName(m, "get"), p)
    33  	if res, ok := got.([]*dataset.Dataset); ok {
    34  		return res, err
    35  	}
    36  	return nil, dispatchReturnError(got, err)
    37  }
    38  
    39  // Follow updates the follow status of the current user for a given dataset
    40  func (m FollowMethods) Follow(ctx context.Context, p *registry.FollowParams) error {
    41  	_, _, err := m.d.Dispatch(ctx, dispatchMethodName(m, "follow"), p)
    42  	return dispatchReturnError(nil, err)
    43  }
    44  
    45  // followImpl holds the method implementations for follows
    46  type followImpl struct{}
    47  
    48  // Get returns a list of datasets a user follows
    49  func (followImpl) Get(scope scope, p *registry.FollowGetParams) ([]*dataset.Dataset, error) {
    50  	client := scope.RegistryClient()
    51  	if client == nil {
    52  		return nil, repo.ErrNoRegistry
    53  	}
    54  
    55  	regResults, err := client.GetFollowing(scope.Context(), p)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	return regResults, nil
    61  }
    62  
    63  // Follow updates the follow status of the current user for a given dataset
    64  func (m followImpl) Follow(scope scope, p *registry.FollowParams) error {
    65  	client := scope.RegistryClient()
    66  	if client == nil {
    67  		return repo.ErrNoRegistry
    68  	}
    69  
    70  	return client.Follow(scope.Context(), p)
    71  }