db

package
v4.2.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2017 License: AGPL-3.0, AGPL-3.0-or-later Imports: 21 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 AllBoardCounter

func AllBoardCounter() (uint64, error)

AllBoardCounter retrieves the progress counter of the /all/ board

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 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 progress counter of a board

func CanPerform

func CanPerform(account, board string, action ModerationLevel) (
	can bool, err error,
)

CanPerform returns, if the account can perform an action of ModerationLevel 'action' on the target 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, body string, 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 DeletePost

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

DeletePost marks the target post 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

func GetConfigs

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

GetConfigs retrieves global configurations. Only used in tests.

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 GetOpenBody

func GetOpenBody(id uint64) (body string, err error)

GetOpenBody retrieves an open body of a post

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) (res common.StandalonePost, err error)

GetPost reads a single post from the database

func GetPostBoard

func GetPostBoard(id uint64) (string, 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 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 GetStaff

func GetStaff(board string) (staff map[string][]string, err error)

GetStaff retrieves all staff positions of a specific board

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

InsertPost inserts a post into an existing thread

func InsertThread

func InsertThread(subject string, p Post) (err 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 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 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

func SetOpenBody

func SetOpenBody(id uint64, body []byte) error

SetOpenBody sets the open body of a post

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 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 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) (err 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 ModerationLevel

type ModerationLevel int8

ModerationLevel defines the level required to perform an action

const (
	NotStaff ModerationLevel = iota - 1
	Janitor
	Moderator
	BoardOwner
	Admin
)

All available moderation levels

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 PostStats

type PostStats struct {
	Editing, HasImage bool
	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 posts created in the thread in the last 15 minutes. Posts that are being editted also have their Body property set.

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