feed

package
v0.0.0-...-3f8308b Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package feed provides the types and methods to manage feeds in the application using an all-in-memory approach.

Index

Constants

View Source
const (
	TypeXML   = "xml"
	TypeHTML  = "html"
	TypeImage = "img"
)

Variables

This section is empty.

Functions

func ParseParams

func ParseParams(params any, dst FeedParams) error

parseParams takes feed params, unmarshalls them into the dst struct and validates them.

func UID

func UID(input string) string

UID returns a unique ID based on the input string.

Types

type Feed

type Feed struct {
	// Name is the name of the feed as defined by the user.
	Name string `json:"name"`

	// Type is the type of the feed. It can be one of the types defined in this
	// file.
	Type string `json:"type"`

	// URL is the URL from which the feed is fetched. If this is empty, the
	// feed is assumed to be a virtual feed managed by the application.
	URL string `json:"url"`

	// Items is a map of items in the feed. The key is the UID of the item.
	Items map[string]*Item `json:"items"`

	// Params is an object with parameters that are specific to the feed type.
	// It is up to the feed type to define what these parameters are, and they
	// are usually used when parsing or refreshing the feed.
	Params any `json:"params"`

	// LastRefreshedAt is the time when the feed was last fetched.
	LastRefreshedAt int64 `json:"updated_at"`

	// LastRefreshError is the last error that occurred when refreshing the
	// feed.
	LastRefreshError string `json:"error"`
}

Feed represents a feed in the application.

func (*Feed) MarkAllRead

func (f *Feed) MarkAllRead(before int64)

MarkAllRead marks all feed items as read if their timestamp is less than or equal to the given before timestamp.

func (*Feed) Prune

func (f *Feed) Prune(n int, observedRawItems int)

Prune removes items from the feed until the number of items is less than or equal to n. It removes the last items as determined by Feed.SortedItems. If n is less than zero, or if the feeds has less than n items, it does nothing. If n is zero and observedRawItems is greater than zero, it takes into account observedRawItems, trying to reach a balance between a minimum of 100 items and a maximum of 200 items. If the feed's max_items param is set, it is always respected as long as it is greater than zero.

func (*Feed) Refresh

func (f *Feed) Refresh(items []RawItem, ts int64, fetchErr error)

Refresh updates the feed with information coming from raw items or an error coming from a fetcher, and then prunes the feed to the maximum number of items.

func (*Feed) SortedItems

func (f *Feed) SortedItems() []Item

SortedItems returns the items in the feed sorted by timestamp, position and then URL in descending order.

func (*Feed) Summary

func (f *Feed) Summary(withItems bool, itemMapper map[string]*Feed) *FeedSummary

Summary returns a summary of the feed. If withItems is true, it includes the items in the feed. itemMapper should usually be nil; it's only used in special cases when building virtual feeds containing items from other feeds.

func (*Feed) UID

func (f *Feed) UID() string

type FeedParams

type FeedParams interface {
	Validate() error
}

feedParams is to be implemented by specific feed params structs.

type FeedSummary

type FeedSummary struct {
	// UID is the unique identifier of the feed.
	UID string `json:"uid"`

	// URL is the URL from which the feed is fetched.
	URL string `json:"url"`

	// Name is the name of the feed as defined by the user.
	Name string `json:"name"`

	// Items is a list of items in the feed. It is usually empty, unless
	// explicitly requested.
	Items []*ItemSummary `json:"items,omitempty"`

	// LastUpdated is the time when the feed was last fetched.
	LastUpdated int64 `json:"last_updated"`

	// LastError is the last error that occurred when refreshing the feed.
	LastError string `json:"last_error"`

	// ItemCount is the number of items in the feed.
	ItemCount int `json:"item_count"`

	// ReadCount is the number of items in the feed that were marked as read.
	ReadCount int `json:"read_count"`
}

FeedSummary is the external representation of the feed (e.g., for presenting to users).

type Item

type Item struct {
	RawItem

	// FeedUID is the UID of the feed that this item belongs to. This field is
	// populated by the feed itself.
	FeedUID string `json:"feed_uid"`
	// Timestamp is the time when the item was first seen. This field is
	// populated by the feed itself.
	Timestamp int64 `json:"timestamp"`

	// Read is true if the item was marked as read by the user.
	Read bool `json:"read"`
}

Item is the representation of an item in the application, with additional fields that are not present in the raw item.

func (*Item) MarkRead

func (i *Item) MarkRead()

MarkRead marks all feed items as read.

func (*Item) Refresh

func (i *Item) Refresh(r RawItem) bool

Refresh updates the item with certain changeable fields coming from the new raw item r. It returns true if the item was updated, false otherwise.

func (*Item) Summary

func (i *Item) Summary(f *Feed, includeContent bool) *ItemSummary

type ItemSummary

type ItemSummary struct {
	// UID is the unique identifier of the item.
	UID string `json:"uid"`

	// FeedUID is the UID of the feed that this item belongs to.
	FeedUID string `json:"feed_uid"`

	// FeedName is the name of the feed that this item belongs to.
	FeedName string `json:"feed_name"`

	// URL is the URL of the item. It comes directly from the feed and is not
	// sanitized. Do not embed it in the page directly without proper measures.
	URL string `json:"url"`

	// Title is a short title or description of the item. It comes directly
	// from the feed and is not sanitized. Do not embed it in the page directly
	// without proper measures.
	Title string `json:"title"`

	// Timestamp is the time when the item was first seen.
	Timestamp int64 `json:"timestamp"`

	// Authors is a short comma-separated summary of authors of the item. It
	// comes directly from the feed and is not sanitized. Do not embed it in
	// the page directly without proper measures.
	Authors string `json:"authors"`

	// Read is true if the item was marked as read by the user.
	Read bool `json:"read"`

	// Content is the full content of the item, usually a sanitized HTML
	// fragment. It comes directly from the feed and may safely be embedded in
	// the page directly.
	Content string `json:"content,omitempty"`
}

ItemSummary is the external representation of the item (e.g., for presenting to users).

type RawItem

type RawItem struct {
	// URL is the URL of the item.
	URL string `json:"url"`
	// Title is a short title or description of the item.
	Title string `json:"title"`
	// Authors is short summary of authors of the item (usually
	// comma-separated).
	Authors string `json:"authors"`
	// Content is the full content of the item, usually a sanitized HTML
	// fragment.
	Content string `json:"content"`
	// Position is the position of the item in the feed when it was first seen.
	// Assuming two items are first seen at the same time, a lower position
	// typically means a newer item (i.e., that's how blogs are typically laid
	// out).
	Position int `json:"position"`
}

RawItem is the representation of an item as it comes from a feed.

func (*RawItem) IsValid

func (i *RawItem) IsValid() bool

IsValid returns true if the item has a URL and a title, thus being considered minimally valid.

func (*RawItem) UID

func (i *RawItem) UID() string

UID returns a unique identifier for the raw item if it is valid. Otherwise, returns an empty string.

Jump to

Keyboard shortcuts

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