getstream

package
v1.0.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 27, 2023 License: MIT Imports: 15 Imported by: 0

README

Feeds Library

This is a Go wrapper for the GetStream API. The GetStream API is a service for building scalable newsfeeds and activity streams. The wrapper is designed to make it easy for Go developers to interact with the GetStream API by providing a simple and easy-to-use interface.

Installation

To use this wrapper, you need to have Go installed on your machine. You can install Go by following the instructions on the official website.

Once you have Go installed, you can install the GetStream wrapper by running the following command:

go get github.com/SimifiniiCTO/simfiny-core-lib/third-party/getstream

Usage

To use the GetStream wrapper, you need to first create a new client by providing your API credentials. You can then use this client to interact with the GetStream API by calling various methods.

Here's an example of how to create a new client:

import (
    "github.com/SimifiniiCTO/simfiny-core-lib/third-party/getstream"
)

func main() {
    opts := []getstream.Options{
        getstream.WithLogger(zap.L()),
        getstream.WithKey("key"),
        getstream.WithSecret("secret"),
        getstream.WithInstrumentationClient(&instrumentation.Client{})
    }
    client, err := getstream.New(opts...)
    if err != nil {
        // handle error
    }
}

You can then use the client to interact with the GetStream API. Here are some examples of what you can do:

Creating a new activity

feedId := "feed-id"
ctx := context.Background()
actor := stream.NewUser("user:1")
object := stream.NewObject("product:1")
verb := "purchased"

activity := stream.NewActivity(actor, verb, object)
if _, err := client.CreateActivity(ctx, feedId, activity); err != nil {
    // handle error
}

Fetching Activities

feedId := "feed-id"
ctx := context.Background()
if activities, err := client.GetTimeline(ctx, feedId); err != nil {
    // handle error
} else {
    // do something with activities
}

Follow Activities

source := "feed-id-source"
target := "feed-id-target"
ctx := context.Background()
if err := client.FollowFeed(ctx, source, target); err != nil {
    ...
}

Testing

To test the GetStream wrapper, you can use the built-in testing framework in Go. The tests are located in the *_test.go files in the repository.

To run the tests, simply run the following command:

go test -v ./...

This will run all the tests in the repository and print out the results.

Contributions

Contributions to the GetStream wrapper are welcome! If you find a bug or have an idea for a new feature, feel free to open an issue or submit a pull request.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidKeyOrSecret           = errors.New("invalid key or secret")
	ErrInvalidInstrumentationClient = errors.New("invalid instrumentation client")
	ErrInvalidLogger                = errors.New("invalid logger")
)

Functions

This section is empty.

Types

type ActivityResponse added in v1.0.5

type ActivityResponse struct {
}

type Client

type Client struct {
	// contains filtered or unexported fields
}

This is defining a struct type called `Client` which will be used to create instances of the GetStream client. The struct contains fields for the GetStream API key, secret, a pointer to a `stream.Client` instance, a logger, and an instrumentation client.

func New

func New(opts ...Option) (*Client, error)

The function creates a new client with optional configuration options.

func (*Client) AddActivity

func (f *Client) AddActivity(ctx context.Context, feedID *string, activity *stream.Activity) (*stream.AddActivityResponse, error)

This is a method of the `Client` struct that adds an activity to a feed specified by `feedID`. It takes in a `context.Context` object, a pointer to a string `feedID`, and a pointer to a `stream.Activity` object. It returns an error if there is an issue with the input arguments or if there is an error adding the activity to the feed. The method first checks if the input arguments are valid, gets the flat feed from the feed ID, adds the activity to the feed, and logs the creation of the activity.

func (*Client) AddActivityToManyFeeds

func (f *Client) AddActivityToManyFeeds(ctx context.Context, activity *stream.Activity, feeds ...stream.Feed) error

`func (f *Client) AddActivityToManyFeeds(ctx context.Context, activity *stream.Activity, feeds ...stream.Feed) error` is a method of the `Client` struct that adds an activity to multiple feeds specified by `feeds`. It takes in a `context.Context` object, a pointer to a `stream.Activity` object, and a variadic argument of type `stream.Feed`. It returns an error if there is an issue with the input arguments or if there is an error adding the activity to the feeds. The method first checks if the input arguments are valid, and then adds the activity to all the specified feeds using the `AddToMany` method of the `stream.Client` object.

func (*Client) ApplyOptions

func (c *Client) ApplyOptions(opts ...Option)

The function ApplyOptions takes a variadic number of Options and applies them to the Client.

func (*Client) CreateAggregateFeed

func (c *Client) CreateAggregateFeed(feedType FeedType, feedSlug string) (*stream.AggregatedFeed, error)

`func (c *Client) CreateAggregateFeed(feedType, feedSlug string) (*stream.AggregatedFeed, error)` is a method of the `Client` struct that creates a new `AggregatedFeed` object in the GetStream API based on the given feed type and feed slug. It takes the feed type and feed slug as input arguments and returns a pointer to the newly created `AggregatedFeed` object and any error that occurred during the API call. The method first checks if the input arguments are valid, and then calls the `AggregatedFeed` method of the `stream.Client` instance stored in the `Client` struct, passing in the feed type and feed slug as arguments.

func (*Client) CreateFlatFeed

func (c *Client) CreateFlatFeed(feedType FeedType, feedSlug string) (*stream.FlatFeed, error)

`func (c *Client) CreateFlatFeed(feedType, feedSlug string) (*stream.FlatFeed, error)` is a method of the `Client` struct that creates a new `FlatFeed` object in the GetStream API based on the given feed type and feed slug. It takes the feed type and feed slug as input arguments and returns a pointer to the newly created `FlatFeed` object and any error that occurred during the API call. The method first checks if the input arguments are valid, and then calls the `FlatFeed` method of the `stream.Client` instance stored in the `Client` struct, passing in the feed type and feed slug as arguments.

func (*Client) CreateNotificationFeed

func (c *Client) CreateNotificationFeed(feedType FeedType, feedSlug string) (*stream.NotificationFeed, error)

`func (c *Client) CreateNotificationFeed(feedType, feedSlug string) (*stream.NotificationFeed, error)` is a method of the `Client` struct that creates a new `NotificationFeed` object in the GetStream API based on the given feed type and feed slug. It takes the feed type and feed slug as input arguments and returns a pointer to the newly created `NotificationFeed` object and any error that occurred during the API call. The method first checks if the input arguments are valid, and then calls the `NotificationFeed` method of the `stream.Client` instance stored in the `Client` struct, passing in the feed type and feed slug as arguments.

func (*Client) DeleteActivity

func (f *Client) DeleteActivity(ctx context.Context, feedID *string, activityForeignID *string) (*stream.RemoveActivityResponse, error)

The `DeleteActivity` function is a method of the `Client` struct that deletes an activity from a feed specified by `feedID` and `activityForeignID`. It takes in a `context.Context` object, a pointer to a string `feedID`, and a pointer to a string `activityForeignID`. It returns an error if there is an issue with the input arguments or if there is an error deleting the activity from the feed. The method first checks if the input arguments are valid, gets the flat feed from the feed ID, removes the activity from the feed using the foreign ID, and logs the removal of the activity.

func (*Client) FollowFeed

func (f *Client) FollowFeed(ctx context.Context, sourceFeedID, targetFeedID string, opts []stream.FollowFeedOption) (*stream.BaseResponse, error)

This is a method defined on a struct type `Client`. The method is called `FollowFeed` and it takes in four arguments: a context, two strings `sourceFeedID` and `targetFeedID`, and a slice of `stream.FollowFeedOption` options. The method returns an error.

func (*Client) GetFlatFeedFromFeedID

func (f *Client) GetFlatFeedFromFeedID(feedID *string) (*stream.FlatFeed, error)

`func (f *Client) GetFlatFeedFromFeedID(feedID *string) (*stream.FlatFeed, error)` is a method of the `Client` struct that takes a pointer to a string as input and returns a pointer to a `stream.FlatFeed` object and an error. It is used to retrieve a `FlatFeed` object from the GetStream API based on a given feed ID. The method first validates the feed ID using the `validateFeedID` method, and then splits the feed ID into its constituent parts (feed type and feed slug) using the `strings.Split` function. Finally, it calls the `FlatFeed` method of the `stream.Client` instance stored in the `Client` struct, passing in the feed type and feed slug as arguments, and returns the resulting `FlatFeed` object and any error that occurred during the API call.

func (*Client) GetNotificationFeedFromFeedID

func (f *Client) GetNotificationFeedFromFeedID(feedID *string) (*stream.NotificationFeed, error)

`func (f *Client) GetNotificationFeedFromFeedID(feedID *string) (*stream.NotificationFeed, error)` is a method of the `Client` struct that takes a pointer to a string as input and returns a pointer to a `stream.NotificationFeed` object and an error. It is used to retrieve a `NotificationFeed` object from the GetStream API based on a given feed ID. The method first validates the feed ID using the `validateFeedID` method, and then splits the feed ID into its constituent parts (feed type and feed slug) using the `strings.Split` function. Finally, it calls the `NotificationFeed` method of the `stream.Client` instance stored in the `Client` struct, passing in the feed type and feed slug as arguments, and returns the resulting `NotificationFeed` object and any error that occurred during the API call.

func (*Client) GetNotificationTimeline

func (f *Client) GetNotificationTimeline(ctx context.Context, feedID *string, opts []stream.GetActivitiesOption) (*stream.NotificationFeedResponse, error)

`func (f *Client) GetNotificationTimeline(ctx context.Context, feedID *string) ([]stream.NotificationFeedResult, error)` is a method defined on the `Client` struct that retrieves a timeline of notification activities from a notification feed identified by the `feedID` parameter. It returns a slice of `stream.NotificationFeedResult` objects and an error if there was a problem retrieving the activities. The method takes a `context.Context` object as the first parameter to allow for cancellation or timeout of the request.

func (*Client) GetTimeline

func (f *Client) GetTimeline(ctx context.Context, feedID *string) (*stream.FlatFeedResponse, error)

This is a method defined on the `Client` struct that retrieves a timeline of activities from a flat feed identified by the `feedID` parameter. It returns a slice of `stream.Activity` objects and an error if there was a problem retrieving the activities. The method takes a `context.Context` object as the first parameter to allow for cancellation or timeout of the request.

func (*Client) GetTimelineNextPage added in v1.0.5

func (f *Client) GetTimelineNextPage(ctx context.Context, feedID *string, opts []stream.GetActivitiesOption) (*stream.FlatFeedResponse, error)

func (*Client) SendFollowRequestNotification

func (f *Client) SendFollowRequestNotification(ctx context.Context, notificationFeedID string, params *FollowRequestActivity) (*stream.AddActivityResponse, error)

This is a method defined on a struct type `Client`. The method is named `SendFollowRequestNotification` and it takes three parameters: a context object `ctx`, a string `notificationFeedID`, and a pointer to a `FollowRequestActivity` struct `params`. The method returns an error.

func (*Client) Validate

func (c *Client) Validate() error

The function WithClient takes a pointer to a stream.Client and returns an Option.

type FeedType

type FeedType string
const (
	PersonalFeed     FeedType = "personal"
	TimelineFeed     FeedType = "timeline"
	NotificationFeed FeedType = "notification"
)

func (FeedType) String

func (f FeedType) String() string

type FollowRequestActivity

type FollowRequestActivity struct {
	SourceActor           string
	ActionName            string
	FollowRequestRecordID string
	Time                  time.Time
	ActivityForeignID     string
}

type IClient added in v1.0.5

type IClient interface {
	// `GetFlatFeedFromFeedID(feedID *string) (*stream.FlatFeed, error)` is a method defined in the
	// `IClient` interface and implemented in the `Client` struct. It takes a pointer to a string `feedID`
	// as input and returns a pointer to a `stream.FlatFeed` and an error. This method is used to retrieve
	// a flat feed from the GetStream API based on the provided `feedID`. A flat feed is a feed that
	// contains all activities in chronological order, without any grouping or aggregation. The method
	// returns a pointer to the retrieved `stream.FlatFeed` object and an error if there was an issue
	// retrieving the feed.
	GetFlatFeedFromFeedID(feedID *string) (*stream.FlatFeed, error)
	// `GetNotificationFeedFromFeedID(feedID *string) (*stream.NotificationFeed, error)` is a method
	// defined in the `IClient` interface and implemented in the `Client` struct. It takes a pointer to a
	// string `feedID` as input and returns a pointer to a `stream.NotificationFeed` and an error. This
	// method is used to retrieve a notification feed from the GetStream API based on the provided
	// `feedID`. A notification feed is a feed that contains activities related to notifications, such as
	// when a user is mentioned or receives a direct message. The method returns a pointer to the retrieved
	// `stream.NotificationFeed` object and an error if there was an issue retrieving the feed.
	GetNotificationFeedFromFeedID(feedID *string) (*stream.NotificationFeed, error)
	// `CreateFlatFeed` is a method defined in the `IClient` interface and implemented in the `Client`
	// struct. It takes a `FeedType` and a `feedSlug` as input and returns a pointer to a `stream.FlatFeed`
	// and an error. This method is used to create a new flat feed on the GetStream API with the specified
	// `feedType` and `feedSlug`. A flat feed is a feed that contains all activities in chronological
	// order, without any grouping or aggregation. The method returns a pointer to the newly created
	// `stream.FlatFeed` object and an error if there was an issue creating the feed.
	CreateFlatFeed(feedType FeedType, feedSlug string) (*stream.FlatFeed, error)
	// `CreateAggregateFeed` is a method defined in the `IClient` interface and implemented in the `Client`
	// struct. It takes a `FeedType` and a `feedSlug` as input and returns a pointer to a
	// `stream.AggregatedFeed` and an error. This method is used to create a new aggregated feed on the
	// GetStream API with the specified `feedType` and `feedSlug`. An aggregated feed is a feed that groups
	// activities based on certain criteria, such as time or user, and presents them in a summarized
	// format. The method returns a pointer to the newly created `stream.AggregatedFeed` object and an
	// error if there was an issue creating the feed.
	CreateAggregateFeed(feedType FeedType, feedSlug string) (*stream.AggregatedFeed, error)
	// `CreateNotificationFeed` is a method defined in the `IClient` interface and implemented in the
	// `Client` struct. It takes a `FeedType` and a `feedSlug` as input and returns a pointer to a
	// `stream.NotificationFeed` and an error. This method is used to create a new notification feed on the
	// GetStream API with the specified `feedType` and `feedSlug`. A notification feed is a feed that
	// contains activities related to notifications, such as when a user is mentioned or receives a direct
	// message. The method returns a pointer to the newly created `stream.NotificationFeed` object and an
	// error if there was an issue creating the feed.
	CreateNotificationFeed(feedType FeedType, feedSlug string) (*stream.NotificationFeed, error)
	// `AddActivity` is a method defined in the `IClient` interface and implemented in the `Client` struct.
	// It takes a context, a pointer to a string `feedID`, and a pointer to a `stream.Activity` as input
	// and returns a pointer to a `stream.AddActivityResponse` and an error. This method is used to add a
	// new activity to the feed specified by the `feedID`. The `stream.Activity` parameter contains the
	// data for the new activity. The method returns a pointer to a `stream.AddActivityResponse` object and
	// an error if there was an issue adding the activity.
	AddActivity(ctx context.Context, feedID *string, activity *stream.Activity) (*stream.AddActivityResponse, error)
	// The `DeleteActivity` method is defined in the `IClient` interface and implemented in the `Client`
	// struct. It takes a context, a pointer to a string `feedID`, and a pointer to a string
	// `activityForeignID` as input and returns a pointer to a `stream.RemoveActivityResponse` and an
	// error. This method is used to remove an activity from the feed specified by the `feedID` and
	// `activityForeignID`. The `activityForeignID` parameter is a unique identifier for the activity that
	// was assigned by the client when the activity was added to the feed. The method returns a pointer to
	// a `stream.RemoveActivityResponse` object and an error if there was an issue removing the activity.
	DeleteActivity(ctx context.Context, feedID *string, activityForeignID *string) (*stream.RemoveActivityResponse, error)
	// `AddActivityToManyFeeds` is a method defined in the `IClient` interface and implemented in the
	// `Client` struct. It takes a context, a pointer to a `stream.Activity`, and a variable number of
	// `stream.Feed` objects as input and returns an error. This method is used to add a new activity to
	// multiple feeds at once. The `stream.Activity` parameter contains the data for the new activity, and
	// the `stream.Feed` parameters specify the feeds to which the activity should be added. The method
	// returns an error if there was an issue adding the activity to any of the specified feeds.
	AddActivityToManyFeeds(ctx context.Context, activity *stream.Activity, feeds ...stream.Feed) error
	// `FollowFeed` is a method defined in the `IClient` interface and implemented in the `Client` struct.
	// It takes a context, a source feed ID, a target feed ID, and an optional list of
	// `stream.FollowFeedOption` objects as input. This method is used to follow a target feed from a
	// source feed. The `sourceFeedID` parameter specifies the ID of the feed that will follow the target
	// feed, and the `targetFeedID` parameter specifies the ID of the feed that will be followed. The
	// `opts` parameter is an optional list of `stream.FollowFeedOption` objects that can be used to
	// specify additional options for the follow operation, such as whether to copy existing activities
	// from the target feed to the source feed. The method returns a pointer to a `stream.BaseResponse`
	// object and an error if there was an issue following the target feed.
	FollowFeed(ctx context.Context, sourceFeedID, targetFeedID string, opts []stream.FollowFeedOption) (*stream.BaseResponse, error)
	// `SendFollowRequestNotification` is a method defined in the `IClient` interface and implemented in
	// the `Client` struct. It takes a context, a string `notificationFeedID`, and a pointer to a
	// `FollowRequestActivity` object as input and returns a pointer to a `stream.AddActivityResponse`
	// object and an error. This method is used to send a follow request notification to the specified
	// `notificationFeedID`. The `FollowRequestActivity` parameter contains the data for the follow request
	// activity. The method returns a pointer to a `stream.AddActivityResponse` object and an error if
	// there was an issue sending the notification.
	SendFollowRequestNotification(ctx context.Context, notificationFeedID string, params *FollowRequestActivity) (*stream.AddActivityResponse, error)
	// `GetTimeline` is a method defined in the `IClient` interface and implemented in the `Client` struct.
	// It takes a context and a pointer to a string `feedID` as input and returns a pointer to a
	// `stream.FlatFeedResponse` and an error. This method is used to retrieve a timeline feed from the
	// GetStream API based on the provided `feedID`. A timeline feed is a feed that contains activities
	// from the user's followers in reverse chronological order. The method returns a pointer to the
	// retrieved `stream.FlatFeedResponse` object and an error if there was an issue retrieving the feed.
	GetTimeline(ctx context.Context, feedID *string) (*stream.FlatFeedResponse, error)
	// `GetNotificationTimeline` is a method defined in the `IClient` interface and implemented in the
	// `Client` struct. It takes a context and a pointer to a string `feedID` as input and returns a
	// pointer to a `stream.NotificationFeedResponse` and an error. This method is used to retrieve a
	// notification timeline feed from the GetStream API based on the provided `feedID`. A notification
	// timeline feed is a feed that contains activities related to notifications, such as when a user is
	// mentioned or receives a direct message, from the user's followers in reverse chronological order.
	// The method returns a pointer to the retrieved `stream.NotificationFeedResponse` object and an error
	// if there was an issue retrieving the feed.
	GetNotificationTimeline(ctx context.Context, feedID *string, opts []stream.GetActivitiesOption) (*stream.NotificationFeedResponse, error)

	// The above code is defining a method called `GetTimelineWithOptions` in the Go programming language.
	// This method takes in a context object, a pointer to a string representing a feed ID, and an array of
	// options for getting activities. It returns a `FlatFeedResponse` object and an error. This method is
	// likely used to retrieve a timeline of activities from a feed with the specified ID and options.
	GetTimelineNextPage(ctx context.Context, feedID *string, opts []stream.GetActivitiesOption) (*stream.FlatFeedResponse, error)
}

`type IClient interface` is defining an interface called `IClient` which specifies a set of methods that a type must implement in order to be considered a valid implementation of the interface. In this case, the `IClient` interface specifies a set of methods that the `Client` struct must implement in order to be considered a valid implementation of the interface. This allows other parts of the code to interact with the `Client` struct through the `IClient` interface, which can make the code more modular and easier to test.

type Option

type Option func(*Client)

func WithInstrumentationClient

func WithInstrumentationClient(instrumentationClient *instrumentation.Client) Option

The function WithInstrumentationClient takes a pointer to a instrumentation.Client and returns an Option.

func WithKey

func WithKey(key string) Option

The function WithKey takes a string and returns an Option.

func WithLogger

func WithLogger(logger *zap.Logger) Option

The function WithLogger takes a pointer to a zap.Logger and returns an Option.

func WithSecret

func WithSecret(secret string) Option

The function WithSecret takes a string and returns an Option.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL