db

package
v0.0.0-...-8577a5a Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2022 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateOwner

func CreateOwner(config DBConfig, password string) error

CreateOwner initializes the database once then creates the owner account.

func NewEmptyPost

func NewEmptyPost(ctype string) smolboard.Post

func NewZeroID

func NewZeroID(t time.Time) int64

func ValidateUser

func ValidateUser(username, password string) error

func VerifyPassword

func VerifyPassword(hash []byte, password string) error

Types

type DBConfig

type DBConfig struct {
	Owner         string `toml:"owner"`
	DatabasePath  string `toml:"databasePath"`
	MaxTokenUses  int    `toml:"maxTokenUses"`
	TokenLifespan string `toml:"tokenLifespan"`
	// contains filtered or unexported fields
}

func NewConfig

func NewConfig() DBConfig

func (*DBConfig) Validate

func (c *DBConfig) Validate() error

type Database

type Database struct {
	*sqlx.DB
	Config DBConfig
}

func NewDatabase

func NewDatabase(config DBConfig) (*Database, error)

func (*Database) Acquire

func (d *Database) Acquire(ctx context.Context, session string, fn TxHandler) error

func (*Database) AcquireGuest

func (d *Database) AcquireGuest(ctx context.Context, fn TxHandler) error

func (*Database) Close

func (d *Database) Close() error

type Transaction

type Transaction struct {
	// use a .Conn instead to allow concurrent transactions.
	*sqlx.Conn

	// As we acquire an entire transaction, it is safe to store our own local
	// session state as long as we keep it up to date on our own calls.
	Session smolboard.Session
	// contains filtered or unexported fields
}

Transaction is a concurrent transaction in SQLite. The transaction is only truly committed once Commit is actually called, which would then lock the database.

func BeginTx

func BeginTx(ctx context.Context, db *Database, session string) (*Transaction, error)

BeginTx starts a new transaction belonging to the given session. If session is empty, then a transaction is not opened.

func (*Transaction) ChangePassword

func (d *Transaction) ChangePassword(password string) error

func (*Transaction) Commit

func (tx *Transaction) Commit() error

Commit commits the changes. It does not close the transaction.

func (*Transaction) CreateToken

func (d *Transaction) CreateToken(uses int) (*smolboard.Token, error)

func (*Transaction) DeleteAllSessions

func (d *Transaction) DeleteAllSessions() error

DeleteAllSessions deletes all sessions except the current one.

func (*Transaction) DeletePost

func (d *Transaction) DeletePost(id int64) error

func (*Transaction) DeleteSessionID

func (d *Transaction) DeleteSessionID(id int64) error

DeleteSessionID deletes the person's own session ID.

func (*Transaction) DeleteToken

func (d *Transaction) DeleteToken(token string) error

func (*Transaction) DeleteUser

func (d *Transaction) DeleteUser(username string) error

func (*Transaction) Exec

func (tx *Transaction) Exec(query string, args ...interface{}) (sql.Result, error)

Exec calls ExecContext.

func (*Transaction) HasPermOverUser

func (d *Transaction) HasPermOverUser(min smolboard.Permission, user string) error

HasPermOverUser checks that the current user has at least the given minimum permission and has a higher permission than the target user.

func (*Transaction) HasPermission

func (d *Transaction) HasPermission(min smolboard.Permission, inclusive bool) error

HasPermission returns nil if the user has the given permission. If inclusive is true, then nil is returned if the user has the same permission as min.

func (*Transaction) IsUserOrHasPermOver

func (d *Transaction) IsUserOrHasPermOver(min smolboard.Permission, user string) error

func (*Transaction) ListTokens

func (d *Transaction) ListTokens() (*smolboard.TokenList, error)

func (*Transaction) Me

func (d *Transaction) Me() (*smolboard.UserPart, error)

func (*Transaction) Permission

func (d *Transaction) Permission() (perm smolboard.Permission, err error)

Permission scans for the permissions, or returns action not permitted if the user does not exist.

func (*Transaction) Post

func (d *Transaction) Post(id int64) (*smolboard.PostExtended, error)

Post returns a single post with the ID. It returns a post not found error if the post is not found or the user does not have permission to see the post.

func (*Transaction) PostQuickGet

func (d *Transaction) PostQuickGet(id int64) (*smolboard.Post, error)

PostQuickGet gets a normal post instance. This function is used primarily internally, but exported for local use.

func (*Transaction) PostSearch

func (d *Transaction) PostSearch(q string, count, page uint) (smolboard.SearchResults, error)

PostSearch parses the query string and returns the searched posts.

func (*Transaction) Posts

func (d *Transaction) Posts(count, page uint) (smolboard.SearchResults, error)

Posts returns the list of posts that's paginated. Count represents the limit for each page and page represents the page offset 0-indexed.

func (*Transaction) PromoteUser

func (d *Transaction) PromoteUser(username string, p smolboard.Permission) error

PromoteUser promotes or demotes someone else.

func (*Transaction) Query

func (tx *Transaction) Query(query string, args ...interface{}) (*sql.Rows, error)

Query calls QueryContext.

func (*Transaction) QueryRow

func (tx *Transaction) QueryRow(query string, args ...interface{}) *sql.Row

QueryRow calls QueryRowContext.

func (*Transaction) QueryRowx

func (tx *Transaction) QueryRowx(query string, args ...interface{}) *sqlx.Row

QueryRowx calls QueryRowxContext.

func (*Transaction) Queryx

func (tx *Transaction) Queryx(query string, args ...interface{}) (*sqlx.Rows, error)

Queryx calls QueryxContext.

func (*Transaction) Rollback

func (tx *Transaction) Rollback() error

Rollback rolls back the transaction and closes it. This method must ALWAYS be called when done.

func (*Transaction) SavePost

func (d *Transaction) SavePost(post *smolboard.Post) error

func (*Transaction) SearchTag

func (d *Transaction) SearchTag(part string) ([]smolboard.PostTag, error)

SearchTag searches for tags and returns at max 25 tags. It returns only the count and name.

func (*Transaction) SearchUsers

func (d *Transaction) SearchUsers(qs string, count, page uint) (smolboard.UserList, error)

func (*Transaction) SessionFromID

func (d *Transaction) SessionFromID(id int64) (s *smolboard.Session, err error)

SessionFromID returns a queried session from the database. It returns the current session state if the ID matches.

func (*Transaction) Sessions

func (d *Transaction) Sessions() ([]smolboard.Session, error)

Sessions returns a list of sessions. The sessions will not have an AuthToken unless it is the current session. The sessions will be sorted from newest to oldest.

func (*Transaction) SetPostPermission

func (d *Transaction) SetPostPermission(id int64, target smolboard.Permission) error

SetPostPermission sets the post's permission. The current user can set the post's permission to as high as their own if this is their post or if the user is an administrator.

func (*Transaction) Signin

func (d *Transaction) Signin(user, pass, UA string) (*smolboard.Session, error)

Signin creates a new session using the given username and password. The UserAgent will be used for listing sessions. This function returns an authenticate token.

func (*Transaction) Signout

func (d *Transaction) Signout() error

func (*Transaction) Signup

func (d *Transaction) Signup(user, pass, token, UA string) (*smolboard.Session, error)

func (*Transaction) TagPost

func (d *Transaction) TagPost(postID int64, tag string) error

func (*Transaction) UntagPost

func (d *Transaction) UntagPost(postID int64, tag string) error

func (*Transaction) User

func (d *Transaction) User(username string) (*smolboard.UserPart, error)

User returns the user WITHOUT the passhash.

func (*Transaction) Users

func (d *Transaction) Users(count, page uint) (smolboard.UserList, error)

type TxHandler

type TxHandler = func(*Transaction) error

Jump to

Keyboard shortcuts

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