Documentation
¶
Overview ¶
Package db handles all core database interactions of the server
Index ¶
- Constants
- Variables
- func AllocateImage(src, thumb []byte, img common.ImageCommon) error
- func AppendBody(id, op uint64, char rune, body string) error
- func Backspace(id, op uint64, body string) error
- func Ban(board, reason, by string, expires time.Time, ids ...uint64) (ips map[string]bool, err error)
- func BoardCounter(board string) (uint64, error)
- func ChangePassword(account string, hash []byte) error
- func ClearTables(tables ...string) error
- func ClosePost(id, op uint64, links [][2]uint64, com []common.Command) (err error)
- func CreateAdminAccount() error
- func DeleteBoard(board string) error
- func DeletePosts(board string, ids ...uint64) error
- func FindPosition(board, userID string) (pos string, err error)
- func GetAllBoard() (common.Board, error)
- func GetBanInfo(ip, board string) (b auth.BanRecord, err error)
- func GetBoard(board string) (common.Board, error)
- func GetBoardConfigs(board string) (config.BoardConfigs, error)
- func GetConfigs() (c config.Configs, err error)
- func GetIP(id uint64, board string) (string, error)
- func GetImage(SHA1 string) (common.ImageCommon, error)
- func GetLog(id, from, to uint64) ([][]byte, error)
- func GetLogTillEnd(id, from uint64) ([][]byte, error)
- func GetOwnedBoards(account string) (boards []string, err error)
- func GetPassword(id string) (hash []byte, err error)
- func GetPosition(account, board string) (pos string, err error)
- func GetPost(id uint64) (res common.StandalonePost, err error)
- func GetPostOP(id uint64) (op uint64, err error)
- func GetPostPassword(id uint64) (p []byte, err error)
- func GetPyu() (c int, err error)
- func GetThread(id uint64, lastN int) (t common.Thread, err error)
- func GetThreadBoard(id uint64) (board string, err error)
- func HasImage(id uint64) (has bool, err error)
- func IncrementPyu() (c int, err error)
- func InsertImage(id, op uint64, img common.Image) (err error)
- func InsertPost(p Post) error
- func InsertThread(subject string, p Post) error
- func IsConflictError(err error) bool
- func IsLocked(id uint64) (bool, error)
- func IsLoggedIn(user, session string) (loggedIn bool, err error)
- func Listen(event string) (*pq.Listener, error)
- func LoadDB() (err error)
- func LockForWrite(tx *sql.Tx, tables ...string) error
- func LogOut(account, token string) error
- func LogOutAll(account string) error
- func NewImageToken(SHA1 string) (token string, err error)
- func NewPostID() (id uint64, err error)
- func PostCounter() (uint64, error)
- func RegisterAccount(ID string, hash []byte) error
- func RollbackOnError(tx *sql.Tx, err *error)
- func SetPostCounter(c uint64) error
- func SetPyu(c uint) error
- func SplicePost(id, op uint64, msg []byte, body string)
- func SpoilerImage(id uint64) (err error)
- func StartTransaction() (*sql.Tx, error)
- func ThreadCounter(id uint64) (uint64, error)
- func UpdateBoard(c config.BoardConfigs) error
- func UpdateLog(id uint64, msg []byte) error
- func UseImageToken(token string) (img common.ImageCommon, err error)
- func ValidateOP(id uint64, board string) (valid bool, err error)
- func WriteBoard(tx *sql.Tx, c BoardConfigs) error
- func WriteConfigs(c config.Configs) error
- func WriteImage(tx *sql.Tx, i common.ImageCommon) error
- func WriteLoginSession(account, token string) error
- func WritePost(tx *sql.Tx, p Post) error
- func WriteStaff(tx *sql.Tx, board string, staff map[string][]string) error
- func WriteThread(tx *sql.Tx, t Thread, p Post) (err error)
- type BoardConfigs
- type Post
- type Thread
Constants ¶
const (
// TestConnArgs contains ConnArgs used for tests
TestConnArgs = `user=meguca password=meguca dbname=meguca_test sslmode=disable binary_parameters=yes`
)
Variables ¶
var ( // ConnArgs specifies the PostgreSQL connection arguments ConnArgs = `user=meguca password=meguca dbname=meguca sslmode=disable binary_parameters=yes` // IsTest can be overridden to not launch several infinite loops during tests // or check DB version IsTest bool )
var ( // ErrInvalidToken occurs, when trying to retrieve an image with an // non-existent token. The token might have expired (60 to 119 seconds) or // the client could have provided an invalid token to begin with. ErrInvalidToken = errors.New("invalid image token") )
var (
ErrUserNameTaken = errors.New("user name already taken")
)
Common errors
Functions ¶
func AllocateImage ¶
func AllocateImage(src, thumb []byte, img common.ImageCommon) error
AllocateImage allocates an image's file resources to their respective served directories and write its data to the database
func AppendBody ¶
AppendBody appends a character to a post body
func Ban ¶
func Ban(board, reason, by string, expires time.Time, ids ...uint64) ( ips map[string]bool, err error, )
Ban IPs from accessing a specific board. Need to target posts. Returns all banned IPs.
func BoardCounter ¶
BoardCounter retrieves the history or "progress" counter of a board
func ChangePassword ¶
ChangePassword changes an existing user's login password
func ClearTables ¶
ClearTables deletes the contents of specified DB tables. Only used for tests.
func CreateAdminAccount ¶
func CreateAdminAccount() error
CreateAdminAccount writes a fresh admin account with the default password to the database
func DeleteBoard ¶
DeleteBoard deletes a board and all of its contained threads and posts
func DeletePosts ¶
DeletePosts marks the target posts as deleted
func FindPosition ¶
FindPosition returns the first matching position of a user on a certain board. As a special case the admin user will always return "admin". If none found, returns empty string
func GetAllBoard ¶
GetAllBoard retrieves all threads for the "/all/" meta-board
func GetBanInfo ¶
GetBanInfo retrieves information about a specific ban
func GetBoardConfigs ¶
func GetBoardConfigs(board string) (config.BoardConfigs, error)
GetBoardConfigs retrives the configurations of a specific board. Only used in tests.
func GetConfigs ¶
GetConfigs retrieves global configurations. Only used in tests.
func GetIP ¶
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 GetLogTillEnd ¶
GetLogTillEnd retrieves a slice of the replication log from a certain lower index
func GetOwnedBoards ¶
GetOwnedBoards returns boards the account holder owns
func GetPassword ¶
GetPassword retrieves the login password hash of the registered user account
func GetPosition ¶
GetPosition returns the staff position a user is holding on a board
func GetPost ¶
func GetPost(id uint64) (res common.StandalonePost, err error)
GetPost reads a single post from the database
func GetPostPassword ¶
GetPostPassword retrieves a post's modification password
func GetThreadBoard ¶
GetThreadBoard retrieves the board of a thread by id
func IncrementPyu ¶
IncrementPyu increments the pyu counter by one and returns the new counter
func InsertImage ¶
InsertImage insert and image into and existing open post
func InsertThread ¶
InsertThread inserts a new thread into the database
func IsConflictError ¶
IsConflictError returns if an error is a unique key conflict error
func IsLoggedIn ¶
IsLoggedIn check if the user is logged in with the specified session
func LoadDB ¶
func LoadDB() (err error)
LoadDB establishes connections to RethinkDB and Redis and bootstraps both databases, if not yet done.
func LockForWrite ¶
LockForWrite locks tables for writing ahead of time. Prevents deadlocks.
func NewImageToken ¶
NewImageToken inserts a new image allocation token into the DB and returns it's ID
func PostCounter ¶
PostCounter retrieves the current post counter
func RegisterAccount ¶
RegisterAccount writes the ID and password hash of a new user account to the database
func RollbackOnError ¶
RollbackOnError on error undoes the transaction on error
func SetPostCounter ¶
SetPostCounter sets the post counter. Should only be used in tests.
func SplicePost ¶
SplicePost splices the text body of a post. For less load on the DB, supply the entire new body as `body`.
func SpoilerImage ¶
SpoilerImage spoilers an already allocated image
func StartTransaction ¶
StartTransaction initiates a new DB transaction. It is the responsibility of the caller to commit or rollback the transaction.
func ThreadCounter ¶
ThreadCounter retrieves the progress counter of a thread
func UpdateBoard ¶
func UpdateBoard(c config.BoardConfigs) error
UpdateBoard updates board configurations
func UseImageToken ¶
func UseImageToken(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 ValidateOP ¶
ValidateOP confirms the specified thread exists on specific board
func WriteBoard ¶
func WriteBoard(tx *sql.Tx, c BoardConfigs) error
WriteBoard writes a board complete with configurations to the database
func WriteConfigs ¶
WriteConfigs writes new global configurations to the database
func WriteImage ¶
func WriteImage(tx *sql.Tx, i common.ImageCommon) error
WriteImage writes a processed image record to the DB
func WriteLoginSession ¶
WriteLoginSession writes a new user login session to the DB
func WriteStaff ¶
WriteStaff writes staff positions of a specific board. Old rows are overwritten. tx must not be nil.
Types ¶
type BoardConfigs ¶
type BoardConfigs struct { config.BoardConfigs Created time.Time }
BoardConfigs contains extra fields not exposed on database reads