registry

package
v0.0.0-...-722ebc6 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2023 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

Package registry implements a SQLite3 twtxt registry back-end.

Index

Constants

This section is empty.

Variables

View Source
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.

View Source
var ErrNoUsersProvided = errors.New("no user(s) provided")

ErrNoUsersProvided is returned when user(s) were expected and not provided.

View Source
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.

View Source
var RegexIsAlpha = regexp.MustCompile(`\w+`)

RegexIsAlpha matches `[a-zA-Z0-9_]+`

View Source
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.

View Source
var RegexTweetContainsTags = regexp.MustCompile(`#(\w+)`)

RegexTweetContainsTags is used to confirm if a tweet contains tags and, if so, extract them.

View Source
var RegexURLIsTwtxtFile = regexp.MustCompile(`/twtxt\.txt$|/twtxt$|\.txt$`)

RegexURLIsTwtxtFile checks if the URL points to a twtxt.txt file.

Functions

func FormatTweetsPlain

func FormatTweetsPlain(tweets []Tweet) string

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

func FormatUsersPlain(users []User) string

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

func (d *DB) DeleteUser(ctx context.Context, u *User) (int64, error)

DeleteUser removes a user and their tweets. Returns the number of tweets deleted.

func (*DB) DeleteUsers

func (d *DB) DeleteUsers(ctx context.Context, urls []string) (int64, error)

DeleteUsers removes multiple users and their tweets. Returns the total number of tweets deleted.

func (*DB) FetchTwtxt

func (d *DB) FetchTwtxt(twtxtURL, userID string, lastModified time.Time) ([]Tweet, error)

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

func (d *DB) GetAllUsers(ctx context.Context) ([]User, error)

GetAllUsers retrieves all users without pagination.

func (*DB) GetFullUserByURL

func (d *DB) GetFullUserByURL(ctx context.Context, userURL string) (*User, error)

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

func (d *DB) GetTweetCount() uint32

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

func (d *DB) GetUserCount() uint32

GetUserCount retrieves the current user count stored in memory.

func (*DB) GetUsers

func (d *DB) GetUsers(ctx context.Context, page, perPage int) ([]User, error)

GetUsers gets a page's worth of users.

func (*DB) InsertTweets

func (d *DB) InsertTweets(ctx context.Context, tweets []Tweet) error

InsertTweets adds a collection of tweets to the database.

func (*DB) InsertUser

func (d *DB) InsertUser(ctx context.Context, u *User) error

InsertUser adds a user to the database. The ID field of the provided *User is ignored.

func (*DB) InsertUsers

func (d *DB) InsertUsers(ctx context.Context, users []User) ([]User, error)

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

func (d *DB) SearchUsers(ctx context.Context, page, perPage int, searchTerm string) ([]User, error)

SearchUsers returns a paginated list of users whose nicknames or URLs match the query.

func (*DB) SetTweetCount

func (d *DB) SetTweetCount(ctx context.Context) error

SetTweetCount counts the tweets in the database and stores it in memory.

func (*DB) SetUserCount

func (d *DB) SetUserCount(ctx context.Context) error

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.

func (*DB) UpdateUsersSyncTime

func (d *DB) UpdateUsersSyncTime(ctx context.Context, users []User) error

type Mention

type Mention struct {
	Nickname string `json:"nickname"`
	URL      string `json:"url"`
}

Mention represents a single mention of another user within a tweet.

type RoundTripperWithHeader

type RoundTripperWithHeader struct {
	http.Header
	// contains filtered or unexported fields
}

func NewRoundTripperWithHeader

func NewRoundTripperWithHeader(rt http.RoundTripper) RoundTripperWithHeader

func (RoundTripperWithHeader) RoundTrip

func (rth RoundTripperWithHeader) RoundTrip(r *http.Request) (*http.Response, error)

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

func (u *User) GeneratePasscode() (string, error)

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.

Jump to

Keyboard shortcuts

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