smolboard

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: 10 Imported by: 0

Documentation

Index

Constants

View Source
const HashCost = 12

HashCost controls the bcrypt hash cost.

View Source
const MaxTagLen = 128
View Source
const MaxUsernameLen = 128

MaxUsernameLen is the maximum username length.

View Source
const MinimumPassLength = 8

MinimumPassLength defines the minimum length of a password.

View Source
const QueryTagLimit = 1024

QueryTagLimit is the maximum number of tags allowed in a single query.

Variables

View Source
var (
	ErrMissingExt     = httperr.New(400, "file does not have extension")
	ErrPostNotFound   = httperr.New(404, "post not found")
	ErrPageCountLimit = httperr.New(400, "count is over 100 limit")
)
View Source
var (
	ErrEmptyTag        = httperr.New(400, "empty tag not allowed")
	ErrIllegalTag      = httperr.New(400, "tag contains illegal character")
	ErrTagAlreadyAdded = httperr.New(400, "tag is already added")
	ErrTagTooLong      = httperr.New(400, fmt.Sprintf("tag is too long (max %d)", MaxTagLen))
)
View Source
var (
	ErrQueryAlreadyHasUser = httperr.New(400, "search query already has a user filter")
	ErrQueryHasTooMayTags  = httperr.New(400, "search query has too many tags")
)
View Source
var (
	ErrSessionNotFound = httperr.New(401, "session not found")
	ErrSessionExpired  = httperr.New(410, "session expired")
)
View Source
var (
	ErrUnknownToken   = httperr.New(401, "unknown token")
	ErrOverUseLimit   = httperr.New(400, "requested use is over limit")
	ErrZeroNotAllowed = httperr.New(400, "zero use not allowed")
)
View Source
var (
	ErrOwnerAccountStays  = httperr.New(400, "owner account stays")
	ErrActionNotPermitted = httperr.New(403, "action not permitted")
	ErrUserNotFound       = httperr.New(404, "user not found")
	ErrInvalidPassword    = httperr.New(401, "invalid password")
	ErrPasswordTooShort   = httperr.New(400, "password too short")
	ErrUsernameTooLong    = httperr.New(400, "username too long")
	ErrUsernameTaken      = httperr.New(409, "username taken")
	ErrIllegalName        = httperr.New(403, "username contains illegal characters")
)
View Source
var AllPosts = Query{}

AllPosts searches for all posts; it is a zero value instance of PostQuery.

View Source
var ErrInvalidPermission = httperr.New(400, "invalid permission")
View Source
var NoResults = SearchResults{}

NoResults contains no search results; it is a zero value instance of PostQueryResults.

View Source
var NoUsers = UserList{}

NoUsers is a zero-value user list containing no users.

Functions

func EscapeTag

func EscapeTag(name string) string

EscapeTag escapes a tag.

func NameIsLegal

func NameIsLegal(name string) error

NameIsLegal returns whether or not name contains illegal characters. It returns nil if the name does not contain any.

Legal runes include: digits, letters and an underscore.

func TagIsValid

func TagIsValid(tagName string) error

TagIsValid returns nil if the tag is valid else an error. A tag is invalid if it's empty, it's longer than 128 bytes, it's prefixed with an at sign "@" or it contains anything not a graphical character defined by the Unicode standards.

Types

type ErrResponse

type ErrResponse struct {
	Error string `json:"error"`
}

ErrResponse is the structure of the response when the request returns an error.

type Permission

type Permission int8
const (
	// PermissionGuest is the zero-value of permission, which indicates a guest.
	PermissionGuest Permission = iota
	// PermissionUser is a normal user's base permission. It allows uploading.
	PermissionUser
	// PermissionTrusted has access to posts marked as trusted-only. Trusted
	// users can mark a post as trusted.
	PermissionTrusted
	// PermissionAdministrator can create limited use tokens as well as banning
	// and promoting people up to Trusted. This permission inherits all
	// permissions above.
	PermissionAdministrator
	// PermissionOwner indicates the owner of the image board. The owner can
	// create unlimited tokens and inherits all permissions above. They are also
	// the only person that can promote a person to Administrator.
	PermissionOwner // if username == Owner

)

func AllPermissions

func AllPermissions() []Permission

AllPermissions returns all possible permissions in a slice. It starts with the lowest (guest) and ends with the highest (owner).

func (Permission) HasPermOverUser

func (p Permission) HasPermOverUser(min, target Permission, isSelf bool) error

HasPermOverUser returns nil if the current user (self) has permission over the target user with the given lowest permission required. If target is -1, then PermissionAdministrator is assumed. This is done for deleted accounts.

func (Permission) HasPermission

func (p Permission) HasPermission(min Permission, inclusive bool) error

func (Permission) IsUserOrHasPermOver

func (p Permission) IsUserOrHasPermOver(min, target Permission, isSelf bool) error

func (Permission) IsValid

func (p Permission) IsValid() bool

IsValid returns true if the permission is valid.

func (Permission) String

func (p Permission) String() string

func (Permission) StringInt

func (p Permission) StringInt() string

Int returns the permission as a stringed integer enum. This should only be used for form values and the like.

type Post

type Post struct {
	ID          int64         `json:"id"           db:"id"`
	Size        int64         `json:"size"         db:"size"`
	Poster      *string       `json:"poster"       db:"poster"`
	ContentType string        `json:"content_type" db:"contenttype"`
	Permission  Permission    `json:"permission"   db:"permission"`
	Attributes  PostAttribute `json:"attributes"   db:"attributes"`
}

func (Post) CreatedTime

func (p Post) CreatedTime() time.Time

CreatedTime returns the time the post was created. It's milliseconds accurate.

func (Post) Filename

func (p Post) Filename() string

Filename returns the filename that the file should be written to.

func (*Post) GetPoster

func (p *Post) GetPoster() string

GetPoster returns an empty string if the poster is nil, or the poster's name already dereferenced if not.

func (*Post) SetPoster

func (p *Post) SetPoster(poster string)

SetPoster sets the post's poster.

type PostAttribute

type PostAttribute struct {
	Width    int    `json:"w,omitempty"`
	Height   int    `json:"h,omitempty"`
	Blurhash string `json:"blurhash,omitempty"`
}

func (*PostAttribute) Scan

func (a *PostAttribute) Scan(v interface{}) error

func (PostAttribute) Value

func (a PostAttribute) Value() (driver.Value, error)

type PostExtended

type PostExtended struct {
	Post
	// PosterUser is non-nil if the poster is not null.
	PosterUser *UserPart `json:"poster_user"`
	// Tags is manually queried externally.
	Tags []PostTag `json:"tags"`
}

PostExtended is the type for a post with queried tags and the poster user. This struct is returned from /posts/:id.

type PostTag

type PostTag struct {
	PostID  int64  `db:"postid"  json:"post_id,omitempty"`
	TagName string `db:"tagname" json:"tag_name,omitempty"`
	Count   int    `db:"-"       json:"count,omitempty"`
}

func (PostTag) Escaped

func (t PostTag) Escaped() string

Escaped returns the escaped tag string.

type Query

type Query struct {
	Poster string
	Tags   []string
}

Query represents the parsed query string. A zero-value PostQuery searches for nothing and thus will list all posts.

func ParsePostQuery

func ParsePostQuery(q string) (Query, error)

ParsePostQuery parses a search string to query the post gallery. The syntax is space-delimited optionally quoted tags with an optional prefix in front to indicate a post author. A post author search may only appear once. Below is an example:

tag1 "tag with space" 'more spaces' @diamondburned

func (Query) String

func (q Query) String() string

String encodes the parsed PostQuery to a regular string query.

type SearchResults

type SearchResults struct {
	// Posts contains the paginated list of posts.
	Posts []Post `json:"posts"`
	// Total is the total number of posts found. It is zero if Posts is empty.
	Total int `json:"total"`
	// Sizes is the total size of all posts found. It is zero if Posts is empty.
	Sizes int64 `json:"sizes"`
	// User is the user stated in the search query. It is nil if there's no user
	// stated.
	User *UserPart `json:"user,omitempty"`
}

SearchResults is the results returned from the queried posts.

type Session

type Session struct {
	ID       int64  `json:"id"       db:"id"`
	Username string `json:"username" db:"username"`
	// AuthToken is the token stored in the cookies. If the session isn't the
	// current one, then AuthToken will be empty.
	AuthToken string `json:"auth_token,omitempty" db:"authtoken"`
	// Deadline is gradually updated with each Session call, which is per
	// request.
	Deadline int64 `json:"deadline" db:"deadline"`
	// UserAgent is obtained once on login.
	UserAgent string `json:"user_agent" db:"useragent"`
}

func (Session) CreatedAt

func (s Session) CreatedAt() time.Time

func (Session) IsZero

func (s Session) IsZero() bool

IsZero returns true if the session is a guest one.

type Token

type Token struct {
	Token     string `json:"token"     db:"token"`
	Creator   string `json:"creator"   db:"creator"`
	Remaining int    `json:"remaining" db:"remaining"`
}

type TokenList

type TokenList struct {
	Tokens   []Token             `json:"tokens"`
	Creators map[string]UserPart `json:"creators"`
	MaxUses  int                 `json:"max_uses"`
	SelfPerm Permission          `json:"self_perm"`
}

type User

type User struct {
	UserPart
	Passhash []byte `db:"passhash"`
}

type UserList

type UserList struct {
	Users []UserPart `json:"users"`
	Total int        `json:"total"`
}

type UserPart

type UserPart struct {
	Username   string     `db:"username"   json:"username"`
	JoinTime   int64      `db:"jointime"   json:"join_time"`
	Permission Permission `db:"permission" json:"permission"`
}

UserPart contains non-sensitive parts about the user.

func (UserPart) AllowedPermissions

func (u UserPart) AllowedPermissions() []Permission

AllowedPermissions returns the list of allowed permissions this user can set on their post.

func (UserPart) CanChangePost

func (u UserPart) CanChangePost(p Post) error

CanChangePost returns nil if the user can change the given post. This is kept in sync with the backend functions.

func (UserPart) CanSetPostPermission

func (u UserPart) CanSetPostPermission(p PostExtended, target Permission) error

CanSetPostPermission returns nil if the user can change the post's permission to target. This is kept in sync with the backend functions.

func (UserPart) Joined

func (u UserPart) Joined() time.Time

Joined returns the time the user joined.

Jump to

Keyboard shortcuts

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