timeline

package
v0.4.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2022 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FilterFunction added in v0.2.0

type FilterFunction func(ctx context.Context, timelineAccountID string, item Timelineable) (shouldIndex bool, err error)

FilterFunction is used by a Timeline to filter whether or not a grabbed item should be indexed.

type GrabFunction added in v0.2.0

type GrabFunction func(ctx context.Context, timelineAccountID string, maxID string, sinceID string, minID string, limit int) (items []Timelineable, stop bool, err error)

GrabFunction is used by a Timeline to grab more items to index.

It should be provided to NewTimeline when the caller is creating a timeline (of statuses, notifications, etc).

timelineAccountID: the owner of the timeline
maxID: the maximum item ID desired.
sinceID: the minimum item ID desired.
minID: see sinceID
limit: the maximum amount of items to be returned

If an error is returned, the timeline will stop processing whatever request called GrabFunction, and return the error. If no error is returned, but stop = true, this indicates to the caller of GrabFunction that there are no more items to return, and processing should continue with the items already grabbed.

type Manager

type Manager interface {
	// Ingest takes one item and indexes it into the timeline for the given account ID.
	//
	// It should already be established before calling this function that the item actually belongs in the timeline!
	//
	// The returned bool indicates whether the item was actually put in the timeline. This could be false in cases where
	// the item is a boosted status, but a boost of the original status or the status itself already exists recently in the timeline.
	Ingest(ctx context.Context, item Timelineable, timelineAccountID string) (bool, error)
	// IngestAndPrepare takes one timelineable and indexes it into the timeline for the given account ID, and then immediately prepares it for serving.
	// This is useful in cases where we know the item will need to be shown at the top of a user's timeline immediately (eg., a new status is created).
	//
	// It should already be established before calling this function that the item actually belongs in the timeline!
	//
	// The returned bool indicates whether the item was actually put in the timeline. This could be false in cases where
	// a status is a boost, but a boost of the original status or the status itself already exists recently in the timeline.
	IngestAndPrepare(ctx context.Context, item Timelineable, timelineAccountID string) (bool, error)
	// GetTimeline returns limit n amount of prepared entries from the timeline of the given account ID, in descending chronological order.
	// If maxID is provided, it will return prepared entries from that maxID onwards, inclusive.
	GetTimeline(ctx context.Context, accountID string, maxID string, sinceID string, minID string, limit int, local bool) ([]Preparable, error)
	// GetIndexedLength returns the amount of items that have been *indexed* for the given account ID.
	GetIndexedLength(ctx context.Context, timelineAccountID string) int
	// GetDesiredIndexLength returns the amount of items that we, ideally, index for each user.
	GetDesiredIndexLength(ctx context.Context) int
	// GetOldestIndexedID returns the id ID for the oldest item that we have indexed for the given account.
	GetOldestIndexedID(ctx context.Context, timelineAccountID string) (string, error)
	// PrepareXFromTop prepares limit n amount of items, based on their indexed representations, from the top of the index.
	PrepareXFromTop(ctx context.Context, timelineAccountID string, limit int) error
	// Remove removes one item from the timeline of the given timelineAccountID
	Remove(ctx context.Context, timelineAccountID string, itemID string) (int, error)
	// WipeItemFromAllTimelines removes one item from the index and prepared items of all timelines
	WipeItemFromAllTimelines(ctx context.Context, itemID string) error
	// WipeStatusesFromAccountID removes all items by the given accountID from the timelineAccountID's timelines.
	WipeItemsFromAccountID(ctx context.Context, timelineAccountID string, accountID string) error
}

Manager abstracts functions for creating timelines for multiple accounts, and adding, removing, and fetching entries from those timelines.

By the time a timelineable hits the manager interface, it should already have been filtered and it should be established that the item indeed belongs in the timeline of the given account ID.

The manager makes a distinction between *indexed* items and *prepared* items.

Indexed items consist of just that item's ID (in the database) and the time it was created. An indexed item takes up very little memory, so it's not a huge priority to keep trimming the indexed items list.

Prepared items consist of the item's database ID, the time it was created, AND the apimodel representation of that item, for quick serialization. Prepared items of course take up more memory than indexed items, so they should be regularly pruned if they're not being actively served.

func NewManager

func NewManager(grabFunction GrabFunction, filterFunction FilterFunction, prepareFunction PrepareFunction, skipInsertFunction SkipInsertFunction) Manager

NewManager returns a new timeline manager.

type Preparable added in v0.2.0

type Preparable interface {
	GetID() string
	GetAccountID() string
	GetBoostOfID() string
	GetBoostOfAccountID() string
}

type PrepareFunction added in v0.2.0

type PrepareFunction func(ctx context.Context, timelineAccountID string, itemID string) (Preparable, error)

PrepareFunction converts a Timelineable into a Preparable.

For example, this might result in the converstion of a *gtsmodel.Status with the given itemID into a serializable *apimodel.Status.

type SkipInsertFunction added in v0.2.0

type SkipInsertFunction func(ctx context.Context,
	newItemID string,
	newItemAccountID string,
	newItemBoostOfID string,
	newItemBoostOfAccountID string,
	nextItemID string,
	nextItemAccountID string,
	nextItemBoostOfID string,
	nextItemBoostOfAccountID string,
	depth int) (bool, error)

SkipInsertFunction indicates whether a new item about to be inserted in the prepared list should be skipped, based on the item itself, the next item in the timeline, and the depth at which nextItem has been found in the list.

This will be called for every item found while iterating through a timeline, so callers should be very careful not to do anything expensive here.

type Timeline

type Timeline interface {

	// Get returns an amount of prepared items with the given parameters.
	// If prepareNext is true, then the next predicted query will be prepared already in a goroutine,
	// to make the next call to Get faster.
	Get(ctx context.Context, amount int, maxID string, sinceID string, minID string, prepareNext bool) ([]Preparable, error)
	// GetXFromTop returns x amount of items from the top of the timeline, from newest to oldest.
	GetXFromTop(ctx context.Context, amount int) ([]Preparable, error)
	// GetXBehindID returns x amount of items from the given id onwards, from newest to oldest.
	// This will NOT include the item with the given ID.
	//
	// This corresponds to an api call to /timelines/home?max_id=WHATEVER
	GetXBehindID(ctx context.Context, amount int, fromID string, attempts *int) ([]Preparable, error)
	// GetXBeforeID returns x amount of items up to the given id, from newest to oldest.
	// This will NOT include the item with the given ID.
	//
	// This corresponds to an api call to /timelines/home?since_id=WHATEVER
	GetXBeforeID(ctx context.Context, amount int, sinceID string, startFromTop bool) ([]Preparable, error)
	// GetXBetweenID returns x amount of items from the given maxID, up to the given id, from newest to oldest.
	// This will NOT include the item with the given IDs.
	//
	// This corresponds to an api call to /timelines/home?since_id=WHATEVER&max_id=WHATEVER_ELSE
	GetXBetweenID(ctx context.Context, amount int, maxID string, sinceID string) ([]Preparable, error)

	// IndexOne puts a item into the timeline at the appropriate place according to its 'createdAt' property.
	//
	// The returned bool indicates whether or not the item was actually inserted into the timeline. This will be false
	// if the item is a boost and the original item or another boost of it already exists < boostReinsertionDepth back in the timeline.
	IndexOne(ctx context.Context, itemID string, boostOfID string, accountID string, boostOfAccountID string) (bool, error)

	// OldestIndexedItemID returns the id of the rearmost (ie., the oldest) indexed item, or an error if something goes wrong.
	// If nothing goes wrong but there's no oldest item, an empty string will be returned so make sure to check for this.
	OldestIndexedItemID(ctx context.Context) (string, error)
	// NewestIndexedItemID returns the id of the frontmost (ie., the newest) indexed item, or an error if something goes wrong.
	// If nothing goes wrong but there's no newest item, an empty string will be returned so make sure to check for this.
	NewestIndexedItemID(ctx context.Context) (string, error)

	IndexBefore(ctx context.Context, itemID string, amount int) error
	IndexBehind(ctx context.Context, itemID string, amount int) error

	// PrepareXFromTop instructs the timeline to prepare x amount of items from the top of the timeline.
	PrepareFromTop(ctx context.Context, amount int) error
	// PrepareBehind instructs the timeline to prepare the next amount of entries for serialization, from position onwards.
	// If include is true, then the given item ID will also be prepared, otherwise only entries behind it will be prepared.
	PrepareBehind(ctx context.Context, itemID string, amount int) error
	// IndexOne puts a item into the timeline at the appropriate place according to its 'createdAt' property,
	// and then immediately prepares it.
	//
	// The returned bool indicates whether or not the item was actually inserted into the timeline. This will be false
	// if the item is a boost and the original item or another boost of it already exists < boostReinsertionDepth back in the timeline.
	IndexAndPrepareOne(ctx context.Context, itemID string, boostOfID string, accountID string, boostOfAccountID string) (bool, error)
	// OldestPreparedItemID returns the id of the rearmost (ie., the oldest) prepared item, or an error if something goes wrong.
	// If nothing goes wrong but there's no oldest item, an empty string will be returned so make sure to check for this.
	OldestPreparedItemID(ctx context.Context) (string, error)

	// ActualPostIndexLength returns the actual length of the item index at this point in time.
	ItemIndexLength(ctx context.Context) int

	// Reset instructs the timeline to reset to its base state -- cache only the minimum amount of items.
	Reset() error
	// Remove removes a item from both the index and prepared items.
	//
	// If a item has multiple entries in a timeline, they will all be removed.
	//
	// The returned int indicates the amount of entries that were removed.
	Remove(ctx context.Context, itemID string) (int, error)
	// RemoveAllBy removes all items by the given accountID, from both the index and prepared items.
	//
	// The returned int indicates the amount of entries that were removed.
	RemoveAllBy(ctx context.Context, accountID string) (int, error)
}

Timeline represents a timeline for one account, and contains indexed and prepared items.

func NewTimeline

func NewTimeline(
	ctx context.Context,
	timelineAccountID string,
	grabFunction GrabFunction,
	filterFunction FilterFunction,
	prepareFunction PrepareFunction,
	skipInsertFunction SkipInsertFunction) (Timeline, error)

NewTimeline returns a new Timeline for the given account ID

type Timelineable added in v0.2.0

type Timelineable interface {
	GetID() string
	GetAccountID() string
	GetBoostOfID() string
	GetBoostOfAccountID() string
}

Timelineable represents any item that can be put in a timeline.

Jump to

Keyboard shortcuts

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