Documentation
¶
Overview ¶
Package registry implements a SQLite3 twtxt registry back-end.
Index ¶
- Variables
- func FormatTweetsPlain(tweets []Tweet) string
- func FormatUsersPlain(users []User) string
- type DB
- func (d *DB) DeleteUser(ctx context.Context, u *User) (int64, error)
- func (d *DB) DeleteUsers(ctx context.Context, urls []string) (int64, error)
- func (d *DB) FetchTwtxt(twtxtURL, userID string, lastModified time.Time) ([]Tweet, error)
- func (d *DB) GetAllUsers(ctx context.Context) ([]User, error)
- func (d *DB) GetFullUserByURL(ctx context.Context, userURL string) (*User, error)
- func (d *DB) GetMentions(ctx context.Context, page, perPage int, visibilityStatus TweetVisibilityStatus) ([]Tweet, error)
- func (d *DB) GetTags(ctx context.Context, page, perPage int, visibilityStatus TweetVisibilityStatus) ([]Tweet, error)
- func (d *DB) GetTweetCount() uint32
- func (d *DB) GetTweets(ctx context.Context, page, perPage int, visibilityStatus TweetVisibilityStatus) ([]Tweet, error)
- func (d *DB) GetUserCount() uint32
- func (d *DB) GetUsers(ctx context.Context, page, perPage int) ([]User, error)
- func (d *DB) InsertTweets(ctx context.Context, tweets []Tweet) error
- func (d *DB) InsertUser(ctx context.Context, u *User) error
- func (d *DB) InsertUsers(ctx context.Context, users []User) ([]User, error)
- func (d *DB) SearchMentions(ctx context.Context, page, perPage int, searchTerm string, ...) ([]Tweet, error)
- func (d *DB) SearchTags(ctx context.Context, page, perPage int, searchTerm string, ...) ([]Tweet, error)
- func (d *DB) SearchTweets(ctx context.Context, page, perPage int, searchTerm string, ...) ([]Tweet, error)
- func (d *DB) SearchUsers(ctx context.Context, page, perPage int, searchTerm string) ([]User, error)
- func (d *DB) SetTweetCount(ctx context.Context) error
- func (d *DB) SetUserCount(ctx context.Context) error
- func (d *DB) ToggleTweetHiddenStatus(ctx context.Context, userID string, timestamp time.Time, ...) error
- func (d *DB) UpdateUsersSyncTime(ctx context.Context, users []User) error
- type Mention
- type RoundTripperWithHeader
- type Tweet
- type TweetVisibilityStatus
- type User
Constants ¶
This section is empty.
Variables ¶
var ErrIncompleteUserInfo = errors.New("incomplete user info supplied: missing URL and/or nickname and/or passcode")
ErrIncompleteUserInfo is returned when we need more information than we were given.
var ErrNoUsersProvided = errors.New("no user(s) provided")
ErrNoUsersProvided is returned when user(s) were expected and not provided.
var ErrUserURLIsNotTwtxtFile = errors.New("user URL does not point to twtxt.txt")
ErrUserURLIsNotTwtxtFile is returned when the provided user's URL is not a path to a twtxt.txt file.
var RegexIsAlpha = regexp.MustCompile(`\w+`)
RegexIsAlpha matches `[a-zA-Z0-9_]+`
var RegexTweetContainsMentions = regexp.MustCompile(`@<(\w+)\s(\S+)>`)
RegexTweetContainsMentions is used to confirm if a tweet contains mentions and, if so, extract the nicks and URLs out as submatches.
var RegexTweetContainsTags = regexp.MustCompile(`#(\w+)`)
RegexTweetContainsTags is used to confirm if a tweet contains tags and, if so, extract them.
var RegexURLIsTwtxtFile = regexp.MustCompile(`/twtxt\.txt$|/twtxt$|\.txt$`)
RegexURLIsTwtxtFile checks if the URL points to a twtxt.txt file.
Functions ¶
func FormatTweetsPlain ¶
FormatTweetsPlain formats the provided slice of Tweet into plain text, with each LF-terminated line containing the following tab-separated values:
- Nickname
- URL
- Timestamp (RFC3339)
- Body
func FormatUsersPlain ¶
FormatUsersPlain formats the provided slice of User into plain text, with each LF-terminated line containing the following tab-separated values:
- Nickname
- URL
- Timestamp Added (RFC3339)
- Last Sync Time (RFC3339)
Types ¶
type DB ¶
type DB struct { // EntriesPerPageMin specifies the minimum number of users or tweets to display in a single page. EntriesPerPageMin int // EntriesPerPageMax specifies the maximum number of users or tweets to display in a single page. EntriesPerPageMax int // Client is the default HTTP client, which has a 5-second timeout. Client *http.Client // contains filtered or unexported fields }
DB contains the database connection pool and associated settings.
func InitSQLite ¶
func InitSQLite(dbPath string, maxEntriesPerPage, minEntriesPerPage int, httpClient *http.Client, userAgent string, logger *log.Logger) (*DB, error)
InitSQLite initializes the registry's database, creating the appropriate tables if needed.
func (*DB) DeleteUser ¶
DeleteUser removes a user and their tweets. Returns the number of tweets deleted.
func (*DB) DeleteUsers ¶
DeleteUsers removes multiple users and their tweets. Returns the total number of tweets deleted.
func (*DB) FetchTwtxt ¶
FetchTwtxt grabs the twtxt file from the provided URL. The If-Modified-Since header is set to the time provided. Comments and whitespace are stripped from the response. If we receive a 304, return a nil slice and a nil error.
func (*DB) GetAllUsers ¶
GetAllUsers retrieves all users without pagination.
func (*DB) GetFullUserByURL ¶
GetFullUserByURL returns the user's entire row from the database.
func (*DB) GetMentions ¶
func (d *DB) GetMentions(ctx context.Context, page, perPage int, visibilityStatus TweetVisibilityStatus) ([]Tweet, error)
GetMentions retrieves the most recent tweets containing mentions.
func (*DB) GetTags ¶
func (d *DB) GetTags(ctx context.Context, page, perPage int, visibilityStatus TweetVisibilityStatus) ([]Tweet, error)
GetTags returns the most recent tweets containing tags.
func (*DB) GetTweetCount ¶
GetTweetCount retrieves the current tweet count stored in memory.
func (*DB) GetTweets ¶
func (d *DB) GetTweets(ctx context.Context, page, perPage int, visibilityStatus TweetVisibilityStatus) ([]Tweet, error)
GetTweets retrieves a page's worth of tweets in descending order by datetime.
func (*DB) GetUserCount ¶
GetUserCount retrieves the current user count stored in memory.
func (*DB) InsertTweets ¶
InsertTweets adds a collection of tweets to the database.
func (*DB) InsertUser ¶
InsertUser adds a user to the database. The ID field of the provided *User is ignored.
func (*DB) InsertUsers ¶
InsertUsers adds users to the database in bulk.
func (*DB) SearchMentions ¶
func (d *DB) SearchMentions(ctx context.Context, page, perPage int, searchTerm string, visibilityStatus TweetVisibilityStatus) ([]Tweet, error)
SearchMentions searches for a given term in tweet bodies and returns a page worth in descending order by datetime.
func (*DB) SearchTags ¶
func (d *DB) SearchTags(ctx context.Context, page, perPage int, searchTerm string, visibilityStatus TweetVisibilityStatus) ([]Tweet, error)
SearchTags searches for a given term in tweet bodies and returns a page worth in descending order by datetime.
func (*DB) SearchTweets ¶
func (d *DB) SearchTweets(ctx context.Context, page, perPage int, searchTerm string, visibilityStatus TweetVisibilityStatus) ([]Tweet, error)
SearchTweets searches for a given term in tweet bodies and returns a page worth in descending order by datetime.
func (*DB) SearchUsers ¶
SearchUsers returns a paginated list of users whose nicknames or URLs match the query.
func (*DB) SetTweetCount ¶
SetTweetCount counts the tweets in the database and stores it in memory.
func (*DB) SetUserCount ¶
SetUserCount counts the users in the database and stores it in memory.
func (*DB) ToggleTweetHiddenStatus ¶
func (d *DB) ToggleTweetHiddenStatus(ctx context.Context, userID string, timestamp time.Time, status TweetVisibilityStatus) error
ToggleTweetHiddenStatus changes the provided tweet's hidden status.
type RoundTripperWithHeader ¶
func NewRoundTripperWithHeader ¶
func NewRoundTripperWithHeader(rt http.RoundTripper) RoundTripperWithHeader
type Tweet ¶
type Tweet struct { ID string `json:"id"` UserID string `json:"user_id"` Nickname string `json:"nickname"` URL string `json:"url"` DateTime time.Time `json:"datetime"` Body string `json:"body"` Mentions []Mention `json:"mentions"` Tags []string `json:"tags"` Hidden TweetVisibilityStatus `json:"hidden,omitempty"` }
Tweet represents a single entry in a User's twtxt.txt file. Uniqueness must be preserved over (UserID, DateTime, Body).
type TweetVisibilityStatus ¶
type TweetVisibilityStatus int
const ( StatusVisible TweetVisibilityStatus = iota StatusHidden )
type User ¶
type User struct { ID string `json:"id"` URL string `json:"url"` Nick string `json:"nickname"` Passcode string `json:"-"` PasscodeHash []byte `json:"-"` DateTimeAdded time.Time `json:"datetime_added"` LastSync time.Time `json:"last_sync"` }
User represents a single twtxt.txt feed. The URL must be unique, but the Nick doesn't.
func (*User) GeneratePasscode ¶
GeneratePasscode creates a new passcode for a user, then stores it and its bcrypt hash in the User struct. The plaintext passcode is returned on success. Both the ciphertext and the plaintext passcode will be omitted if you serialize the User struct into JSON.