db

package
v0.0.0-...-ff8e54b Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2020 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`
)

Variables

View Source
var (
	ErrUserNameTaken = errors.New("User name already taken")
	ErrContrast      = errors.New("Color must be distinguishable in both themes. Increase contrast.")
)
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")
)

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 AllocateSmileImage

func AllocateSmileImage(src []byte, smile common.SmileCommon) (err error)

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

func AssertNotReacted

func AssertNotReacted(
	ss *auth.Session,
	postID uint64,
	smileName string,
) (r bool)

AssertNotReacted ensures that user with following ip or accountID haven't reacted to post with following smileName

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

DeletePost deletes post

func DeletePostReaction

func DeletePostReaction(postID uint64, smileName string) (err error)

DeletePostReaction deletes item from post_reacts column

func DeleteSmile

func DeleteSmile(board string, smileName string) error

func DeleteUserReaction

func DeleteUserReaction(ss *auth.Session, reactionID uint64) (err 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)

GetBans gets 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 GetBoardSmiles

func GetBoardSmiles(board string) (smiles []common.SmileCommon, err error)

GetBoardSmiles returns board's smiles.

func GetBoardWithGlobalSmiles

func GetBoardWithGlobalSmiles(board string) []common.SmileCommon

GetBoardWithGlobalSmiles returns concatenated list of board and global smiles. Global should be last, as board is prioritized.

func GetIP

func GetIP(id uint64) (string, error)

GetIP returns an IP of the poster that created a post. Posts older than 30 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)

GetModLog retrieves 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 GetPostReactCount

func GetPostReactCount(id uint64, smileName string) (count uint64)

GetPostReactCount reads a single post reaction from the database.

func GetPostReacts

func GetPostReacts(id uint64) (reacts common.Reacts, err error)

GetPostReacts reads a list of post reactions

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 GetSmile

func GetSmile(smileName string, board string) (s common.SmileCommon, err error)

func GetStaff

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

GetStaff retrieves 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 GetThreadReacts

func GetThreadReacts(id uint64) (reacts common.Reacts, err error)

GetThreadReacts reads a list of thread reactions

func GetThreadUserReacts

func GetThreadUserReacts(ss *auth.Session, threadID uint64) (reacts common.Reacts, err error)

GetThreadUserReacts reads a list of thread reactions

func GetUniqueID

func GetUniqueID(id uint64) (string, error)

GetUniqueID returns post's unique identified. Posts older than 30 days will not have this information

func InsertFiles

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

InsertFiles 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 InsertPostReaction

func InsertPostReaction(postID uint64, smileID uint64) (reactionID uint64, err error)

InsertPostReaction inserts a post's reaction with relation to post.

func InsertThread

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

InsertThread inserts a new thread into the database.

func InsertUserReaction

func InsertUserReaction(ss *auth.Session, reactionID uint64) (err error)

InsertUserReaction inserts a post's reaction with relation to post.

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)

Token operations

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 RenameSmile

func RenameSmile(board string, smileName string, originalName string) error

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)

SetBoardState sets board states...

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

SetThreadSticky sets 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

Unban lifts 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 UpdateReactionCount

func UpdateReactionCount(postID uint64, smileName string, count uint64) (reactionID uint64, err error)

UpdateReactionCount updates count of post_reacts column

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 ValidSmileName

func ValidSmileName(smileName string, board string) (valid bool, 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)

WriteBans sets 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 WriteSmileImage

func WriteSmileImage(tx *sql.Tx, s common.SmileCommon) error

func WriteStaff

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

WriteStaff sets staff of specified board, overwriting the old values.

Types

type AccountSettings

type AccountSettings struct {
	Color string `json:"color,omitempty"`
}

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)

GetBoardState gets board states...

type NullString

type NullString sql.NullString

type Post

type Post struct {
	common.StandalonePost
	Password []byte
	IP       string
	UniqueID 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 String

type String sql.NullString

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