db

package
v0.0.0-...-2031ce5 Latest Latest
Warning

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

Go to latest
Published: May 8, 2020 License: AGPL-3.0, AGPL-3.0-or-later Imports: 17 Imported by: 0

Documentation

Overview

Package db handles all core database interactions of the server

Index

Constants

View Source
const (
	// TestConnArgs contains ConnArgs used for tests
	TestConnArgs = `user=meguca password=meguca dbname=meguca_test sslmode=disable`
)

Variables

View Source
var (

	// ConnArgs specifies the PostgreSQL connection arguments
	ConnArgs string

	// IsTest can be overridden to not launch several infinite loops
	// during tests
	IsTest bool
)
View Source
var (
	// ErrInvalidToken occurs, when trying to retrieve post or image with
	// an non-existent token. The token might have expired or the client
	// could have provided an invalid token to begin with.
	ErrInvalidToken = errors.New("invalid token")

	// Occurs when client tries to retrieve too much tokens.
	ErrTokenForbidden = errors.New("token forbidden")
)
View Source
var (
	ErrUserNameTaken = errors.New("user name already taken")
)

Functions

func AllBoardCounter

func AllBoardCounter() (uint64, error)

AllBoardCounter retrieves the progress counter of the /all/ board

func AllocateImage

func AllocateImage(src, thumb []byte, img common.ImageCommon) (err error)

AllocateImage allocates an image's file resources to their respective served directories and write its data to the database.

func Ban

func Ban(board, reason, by string, expires time.Time, ids ...uint64) (
	ips map[string]uint64, err error,
)

Ban IPs from accessing a specific board. Need to target posts. Returns all banned IPs.

func BeginTx

func BeginTx() (tx *sql.Tx, err error)

Initiate a new DB transaction. It is the responsibility of the caller to commit or rollback the transaction.

func BoardCounter

func BoardCounter(board string) (uint64, error)

BoardCounter retrieves the progress counter of a board

func CanCreatePost

func CanCreatePost(ip string) bool

func CanCreateThread

func CanCreateThread(ip string) bool

func ChangePassword

func ChangePassword(account string, hash []byte) error

ChangePassword changes an existing user's login password

func DeleteBoard

func DeleteBoard(board string) error

DeleteBoard deletes a board and all of its contained threads and posts.

func DeleteImageToken

func DeleteImageToken(token string) (err error)

func DeletePost

func DeletePost(id uint64, by string) error

func EndTx

func EndTx(tx *sql.Tx, err *error)

Commit/rollback transaction depending on the error state.

func GetAllBoardCatalog

func GetAllBoardCatalog() (common.Board, error)

GetAllBoardCatalog retrieves all OPs for the "/all/" meta-board.

func GetAllThreadsIDs

func GetAllThreadsIDs() ([]uint64, error)

Retrieves all threads IDs in bump order with stickies first.

func GetBanInfo

func GetBanInfo(ip, board string) (b auth.BanRecord, err error)

GetBanInfo retrieves information about a specific ban

func GetBans

func GetBans(tx *sql.Tx, boards []string) (bans auth.BanRecords, err error)

Get bans for the specified boards. TODO(Kagami): Get from cache? TODO(Kagami): Pagination.

func GetBoardCatalog

func GetBoardCatalog(board string) (common.Board, error)

GetBoardCatalog retrieves all OPs of a single board.

func GetBoardConfig

func GetBoardConfig(tx *sql.Tx, board string) (config.BoardConfig, error)

func GetIP

func GetIP(id uint64) (string, error)

GetIP returns an IP of the poster that created a post. Posts older than 7 days will not have this information.

func GetImage

func GetImage(SHA1 string) (common.ImageCommon, error)

GetImage retrieves a thumbnailed image record from the DB.

func GetModLog

func GetModLog(boards []string) (log auth.ModLogRecords, err error)

Retrieve moderation log for the specified boards. TODO(Kagami): Pagination.

func GetNews

func GetNews() (news []common.NewsEntry, err error)

Retrieve latest news.

func GetOwnedBoards

func GetOwnedBoards(account string) (boards []string, err error)

GetOwnedBoards returns boards the account holder owns

func GetPassword

func GetPassword(id string) (hash []byte, err error)

GetPassword retrieves the login password hash of the registered user account

func GetPost

func GetPost(id uint64) (p common.StandalonePost, err error)

GetPost reads a single post from the database.

func GetPostBoard

func GetPostBoard(id uint64) (board string, err error)

GetPostBoard retrieves the board of a post by ID

func GetPostOP

func GetPostOP(id uint64) (op uint64, err error)

GetPostOP retrieves the parent thread ID of the passed post

func GetPostParenthood

func GetPostParenthood(id uint64) (board string, op uint64, err error)

Retrieve the board and OP of a post

func GetSameIPPosts

func GetSameIPPosts(id uint64, board string) (
	posts []common.StandalonePost, err error,
)

GetSameIPPosts returns posts with the same IP and on the same board as the target post

func GetSession

func GetSession(board, token string) (ss *auth.Session, err error)

Get user's session by token.

func GetStaff

func GetStaff(tx *sql.Tx, boards []string) (staff auth.Staff, err error)

Retrieve staff positions for the specificied boards. TODO(Kagami): Get from cache? TODO(Kagami): Pagination.

func GetThread

func GetThread(id uint64, lastN int) (t common.Thread, err error)

GetThread retrieves public thread data from the database.

func GetThreadIDs

func GetThreadIDs(board string) ([]uint64, error)

Retrieves threads IDs on the board.

func InsertFiles

func InsertFiles(tx *sql.Tx, p Post) (err error)

Write post files in current transaction.

func InsertPost

func InsertPost(tx *sql.Tx, p Post) (err error)

InsertPost inserts a post into an existing thread.

func InsertThread

func InsertThread(tx *sql.Tx, p Post, subject string) (err error)

InsertThread inserts a new thread into the database.

func IsConflictError deprecated

func IsConflictError(err error) bool

Deprecated: Use IsUniqueViolation instead.

func IsForeignKeyViolationError

func IsForeignKeyViolationError(err error) bool

func IsUniqueViolationError

func IsUniqueViolationError(err error) bool

func Listen

func Listen(event string) (*pq.Listener, error)

Listen starts listening for notification events on a specific channel

func LogOut

func LogOut(account, token string) error

LogOut logs the account out of one specific session

func LogOutAll

func LogOutAll(account string) error

LogOutAll logs an account out of all user sessions

func NewImageToken

func NewImageToken(SHA1 string) (token string, err error)

NewImageToken inserts a new image allocation token into the DB and returns it's ID.

func NewPostID

func NewPostID(tx *sql.Tx) (id uint64, err error)

NewPostID reserves a new post ID

func NewPostToken

func NewPostToken(ip string) (token string, err error)

func PostCounter

func PostCounter() (uint64, error)

PostCounter retrieves the current post counter

func RefreshBanCache

func RefreshBanCache() (err error)

RefreshBanCache loads up to date bans from the database and caches them in memory

func RegisterAccount

func RegisterAccount(ID string, hash []byte) error

RegisterAccount writes the ID and password hash of a new user account to the database

func RollbackOnError

func RollbackOnError(tx *sql.Tx, err *error)

RollbackOnError on error undoes the transaction on error. Deprecated: move to EndTx instead.

func SetAccountSettings

func SetAccountSettings(userID string, as auth.AccountSettings) (err error)

func SetBoardState

func SetBoardState(tx *sql.Tx, state BoardState, by string) (err error)

func SetReadOnly

func SetReadOnly(tx *sql.Tx) error

func SetRepeatableRead

func SetRepeatableRead(tx *sql.Tx) (err error)

func SetServerConfig

func SetServerConfig(c config.ServerConfig) error

func SetThreadSticky

func SetThreadSticky(id uint64, sticky bool) error

Set the sticky field on a thread

func StartDB

func StartDB() (err error)

func StartTransaction deprecated

func StartTransaction() (*sql.Tx, error)

Deprecated: use BeginTx instead.

func ThreadCounter

func ThreadCounter(id uint64) (uint64, error)

ThreadCounter retrieves the progress counter of a thread

func Unban

func Unban(board string, id uint64, by string) error

Lift a ban from a specific post on a specific board

func UpdateBoard

func UpdateBoard(tx *sql.Tx, c config.BoardConfig, by string) (err error)

func UpsertIdolPreview

func UpsertIdolPreview(idolId string, imageId string) (err error)

Update or insert preview for the idol.

func UseImageToken

func UseImageToken(tx *sql.Tx, token string) (img common.ImageCommon, err error)

UseImageToken deletes an image allocation token and returns the matching processed image. If no token exists, returns ErrInvalidToken.

func UsePostToken

func UsePostToken(token string) (err error)

func ValidateOP

func ValidateOP(id uint64, board string) (valid bool, err error)

ValidateOP confirms the specified thread exists on specific board

func WriteBans

func WriteBans(tx *sql.Tx, board string, bans auth.BanRecords) (err error)

Set bans of specified board, overwriting the old values.

func WriteBoard

func WriteBoard(tx *sql.Tx, c config.BoardConfig) (err error)

func WriteImage

func WriteImage(tx *sql.Tx, i common.ImageCommon) error

WriteImage writes a processed image record to the DB.

func WriteLoginSession

func WriteLoginSession(account, token string) error

WriteLoginSession writes a new user login session to the DB

func WriteStaff

func WriteStaff(tx *sql.Tx, board string, staff auth.Staff) (err error)

Set staff of specified board, overwriting the old values.

Types

type BoardState

type BoardState struct {
	Settings config.BoardConfig `json:"settings"`
	Staff    auth.Staff         `json:"staff"`
	Bans     auth.BanRecords    `json:"bans"`
}

func GetBoardState

func GetBoardState(tx *sql.Tx, board string) (state BoardState, err error)

type Post

type Post struct {
	common.StandalonePost
	Password []byte
	IP       string
}

Post is for writing new posts to a database. It contains the Password field, which is never exposed publically through Post.

type PostStats

type PostStats struct {
	ID   uint64
	Time int64
	Body []byte
}

PostStats contains post open status, body and creation time.

func GetRecentPosts

func GetRecentPosts(op uint64) (posts []PostStats, err error)

GetRecentPosts retrieves recent posts created in the thread.

type Thread

type Thread struct {
	ID                  uint64
	PostCtr, ImageCtr   uint32
	ReplyTime, BumpTime int64
	Subject, Board      string
}

Thread is a template for writing new threads to the database

Jump to

Keyboard shortcuts

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