db

package
v4.1.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2017 License: AGPL-3.0, AGPL-3.0-or-later Imports: 19 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 binary_parameters=yes`
)

Variables

View Source
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
)
View Source
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")
)
View Source
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

func AppendBody(id, op uint64, char rune) error

AppendBody appends a character to a post body

func Backspace

func Backspace(id, op uint64) error

Backspace removes one character from the end of the 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

func BoardCounter(board string) (uint64, error)

BoardCounter retrieves the history or "progress" counter of a board

func ChangePassword

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

ChangePassword changes an existing user's login password

func ClearTables

func ClearTables(tables ...string) error

ClearTables deletes the contents of specified DB tables. Only used for tests.

func ClosePost

func ClosePost(id, op uint64, links [][2]uint64, com []common.Command) (
	err error,
)

ClosePost closes an open post and commits any links, backlinks and hash commands

func CreateAdminAccount

func CreateAdminAccount() error

CreateAdminAccount writes a fresh admin account with the default password to the database

func DeleteBoard

func DeleteBoard(board string) error

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

func DeletePosts

func DeletePosts(board string, ids ...uint64) error

DeletePosts marks the target posts as deleted

func FindPosition

func FindPosition(board, userID string) (pos string, err error)

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

func GetAllBoard() (common.Board, error)

GetAllBoard retrieves all threads, complete with the first 5 posts

func GetAllBoardCatalog

func GetAllBoardCatalog() (common.Board, error)

GetAllBoardCatalog retrieves all threads for the "/all/" meta-board

func GetBanInfo

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

GetBanInfo retrieves information about a specific ban

func GetBoard

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

GetBoard retrieves all threads on the board, complete with the first 5 posts

func GetBoardCatalog

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

GetBoardCatalog retrieves all OPs of a single board

func GetBoardConfigs

func GetBoardConfigs(board string) (config.BoardConfigs, error)

GetBoardConfigs retrives the configurations of a specific board. Only used in tests.

func GetConfigs

func GetConfigs() (c config.Configs, err error)

GetConfigs retrieves global configurations. Only used in tests.

func GetIP

func GetIP(id uint64, board string) (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 GetLog

func GetLog(id, from, to uint64) ([][]byte, error)

GetLog retrieves a slice of a thread's replication log

func GetLogTillEnd

func GetLogTillEnd(id, from uint64) ([][]byte, error)

GetLogTillEnd retrieves a slice of the replication log from a certain lower index

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 GetPosition

func GetPosition(account, board string) (pos string, err error)

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 GetPostOP

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

GetPostOP retrieves the parent thread ID of the passed post

func GetPostPassword

func GetPostPassword(id uint64) (p []byte, err error)

GetPostPassword retrieves a post's modification password

func GetPyu

func GetPyu() (c uint64, err error)

GetPyu retrieves current pyu counter

func GetThread

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

GetThread retrieves public thread data from the database

func GetThreadBoard

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

GetThreadBoard retrieves the board of a thread by id

func HasImage

func HasImage(id uint64) (has bool, err error)

HasImage returns, if the post has an image allocated. Only used in tests.

func IncrementPyu

func IncrementPyu() (c uint64, err error)

IncrementPyu increments the pyu counter by one and returns the new counter

func InsertImage

func InsertImage(id, op uint64, img common.Image) (err error)

InsertImage insert and image into and existing open post

func InsertPost

func InsertPost(p Post) error

InsertPost inserts a post into an existing thread

func InsertThread

func InsertThread(subject string, p Post) error

InsertThread inserts a new thread into the database

func IsConflictError

func IsConflictError(err error) bool

IsConflictError returns if an error is a unique key conflict error

func IsLocked

func IsLocked(id uint64) (bool, error)

IsLocked returns if the thread is locked from posting

func IsLoggedIn

func IsLoggedIn(user, session string) (loggedIn bool, err error)

IsLoggedIn check if the user is logged in with the specified session

func Listen

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

Listen starts listening for notification events on a specific channel

func LoadDB

func LoadDB() (err error)

LoadDB establishes connections to RethinkDB and Redis and bootstraps both databases, if not yet done.

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() (id uint64, err error)

NewPostID reserves a new post ID

func PostCounter

func PostCounter() (uint64, error)

PostCounter retrieves the current post counter

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

func SetPostCounter

func SetPostCounter(c uint64) error

SetPostCounter sets the post counter. Should only be used in tests.

func SetPyu

func SetPyu(c uint) error

SetPyu sets the pyu counter. Only used in tests.

func SplicePost

func SplicePost(id, op uint64, msg []byte, body string) error

SplicePost splices the text body of a post. For less load on the DB, supply the entire new body as `body`.

func SpoilerImage

func SpoilerImage(id uint64) (err error)

SpoilerImage spoilers an already allocated image

func StartTransaction

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

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

func ThreadCounter

func ThreadCounter(id uint64) (uint64, error)

ThreadCounter retrieves the progress counter of a thread

func UpdateBoard

func UpdateBoard(c config.BoardConfigs) error

UpdateBoard updates board configurations

func UpdateLog

func UpdateLog(id uint64, msg []byte) error

UpdateLog writes to a thread's replication log. Only used in tests.

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

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

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

func WriteConfigs(c config.Configs) error

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

func WriteLoginSession(account, token string) error

WriteLoginSession writes a new user login session to the DB

func WritePost

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

WritePost writes a post struct to the database. Only used in tests and migrations.

func WriteStaff

func WriteStaff(tx *sql.Tx, board string, staff map[string][]string) error

WriteStaff writes staff positions of a specific board. Old rows are overwritten. tx must not be nil.

func WriteThread

func WriteThread(tx *sql.Tx, t Thread, p Post) (err error)

WriteThread writes a thread and it's OP to the database. Only used for tests and migrations.

Types

type BoardConfigs

type BoardConfigs struct {
	config.BoardConfigs
	Created time.Time
}

BoardConfigs contains extra fields not exposed on database reads

type Post

type Post struct {
	Deleted bool
	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 Thread

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

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