Documentation ¶
Overview ¶
Package registry implements functions and types that assist in the creation and management of a twtxt registry.
Index ¶
- func GetTwtxt(urlKey string, client *http.Client) ([]byte, bool, error)
- func ReduceToPage(page int, data []string) []string
- func SortByTime(tm ...TimeMap) ([]string, error)
- type Registrar
- type Registry
- func (registry *Registry) AddUser(nickname, urlKey string, ipAddress net.IP, statuses TimeMap) error
- func (registry *Registry) CrawlRemoteRegistry(urlKey string) error
- func (registry *Registry) DelUser(urlKey string) error
- func (registry *Registry) DiffTwtxt(urlKey string) (bool, error)
- func (registry *Registry) Get(urlKey string) (*User, error)
- func (registry *Registry) GetStatuses() (TimeMap, error)
- func (registry *Registry) GetUserStatuses(urlKey string) (TimeMap, error)
- func (registry *Registry) Put(user *User) error
- func (registry *Registry) QueryAllStatuses() ([]string, error)
- func (registry *Registry) QueryInStatus(substring string) ([]string, error)
- func (registry *Registry) QueryUser(term string) ([]string, error)
- func (registry *Registry) UpdateUser(urlKey string) error
- type TimeMap
- type TimeSlice
- type User
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetTwtxt ¶
GetTwtxt fetches the raw twtxt file data from the user's provided URL, after validating the URL. If the returned boolean value is false, the fetched URL is a single user's twtxt file. If true, the fetched URL is the output of another registry's /api/plain/tweets. The output of GetTwtxt should be passed to either ParseUserTwtxt or ParseRegistryTwtxt, respectively. Generally, the *http.Client inside a given Registry instance should be passed to GetTwtxt. If the *http.Client passed is nil, Registry will use a preconstructed client with a timeout of 10s and all other values set to default.
func ReduceToPage ¶
ReduceToPage returns the passed 'page' worth of output. One page is twenty items. For example, if 2 is passed, it will return data[20:40]. According to the twtxt registry specification, queries should accept a "page" value.
func SortByTime ¶
SortByTime returns a string slice of the query results, sorted by timestamp in descending order (newest first).
Types ¶
type Registrar ¶
type Registrar interface { Put(user *User) error Get(urlKey string) (*User, error) DelUser(urlKey string) error UpdateUser(urlKey string) error GetUserStatuses(urlKey string) (TimeMap, error) GetStatuses() (TimeMap, error) }
Registrar implements the minimum amount of methods for a functioning Registry.
type Registry ¶
type Registry struct { // Provided to aid in concurrency-safe // reads and writes to a given registry // Users map. Mu sync.RWMutex // The registry's user data is contained // in this map. The functions within this // library expect the key to be the URL of // a given user's twtxt file. Users map[string]*User // The client to use for HTTP requests. // If nil is passed to NewIndex(), a // client with a 10 second timeout // and all other values as default is // used. HTTPClient *http.Client }
Registry enables the bulk of a registry's user data storage and access.
func (*Registry) AddUser ¶
func (registry *Registry) AddUser(nickname, urlKey string, ipAddress net.IP, statuses TimeMap) error
AddUser inserts a new user into the Registry.
func (*Registry) CrawlRemoteRegistry ¶
CrawlRemoteRegistry scrapes all nicknames and user URLs from a provided registry. The urlKey passed to this function must be in the form of https://registry.example.com/api/plain/users
func (*Registry) DiffTwtxt ¶
DiffTwtxt issues a HEAD request on the user's remote twtxt data. It then checks the Content-Length header. If it's different from the stored result of the previous Content-Length header, update the stored value for a given user and return true. Otherwise, return false. In some error conditions, such as the user not being in the registry, it returns true. In other error conditions considered "unrecoverable," such as the supplied URL being invalid, it returns false.
func (*Registry) GetStatuses ¶
GetStatuses returns a TimeMap containing all statuses from all users in the Registry.
func (*Registry) GetUserStatuses ¶
GetUserStatuses returns a TimeMap containing single user's statuses
func (*Registry) Put ¶
Put inserts a given User into an Registry. The User being pushed need only have the URL field filled. All other fields may be empty. This can be destructive: an existing User in the Registry will be overwritten if its User.URL is the same as the User.URL being pushed.
func (*Registry) QueryAllStatuses ¶
QueryAllStatuses returns all statuses in the Registry as a slice of strings sorted by timestamp.
func (*Registry) QueryInStatus ¶
QueryInStatus returns all statuses in the Registry that contain the provided substring (tag, mention URL, etc).
func (*Registry) QueryUser ¶
QueryUser checks the Registry for usernames or user URLs that contain the term provided as an argument. Entries are returned sorted by the date they were added to the Registry. If the argument provided is blank, return all users.
func (*Registry) UpdateUser ¶
UpdateUser scrapes an existing user's remote twtxt.txt file. Any new statuses are added to the user's entry in the Registry. If the remote twtxt data's reported Content-Length does not differ from what is stored, an error is returned.
type TimeMap ¶
TimeMap holds extracted and processed user data as a string. A time.Time value is used as the key.
type TimeSlice ¶
TimeSlice is a slice of time.Time used for sorting a TimeMap by timestamp.
func (TimeSlice) Len ¶
Len returns the length of the TimeSlice to be sorted. This helps satisfy sort.Interface.
type User ¶
type User struct { // Provided to aid in concurrency-safe // reads and writes. In most cases, the // mutex in the associated Index should be // used instead. This mutex is provided // should the library user need to access // a User independently of an Index. Mu sync.RWMutex // Nick is the user-specified nickname. Nick string // The URL of the user's twtxt file URL string // The reported last modification date // of the user's twtxt.txt file. LastModified string // The IP address of the user is optionally // recorded when submitted via POST. IP net.IP // The timestamp, in RFC3339 format, // reflecting when the user was added. Date string // A TimeMap of the user's statuses // from their twtxt file. Status TimeMap }
User holds a given user's information and statuses.
func ParseRegistryTwtxt ¶
ParseRegistryTwtxt takes output from a remote registry and outputs the accessible user data via a slice of Users.
func (*User) FindInStatus ¶
FindInStatus takes a user's statuses and looks for a given substring. Returns the statuses that include the substring as a TimeMap.