db

package
v4.4.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2017 License: AGPL-3.0, AGPL-3.0-or-later Imports: 23 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
	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]uint64, 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 auth.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 CheckThreadNonLive

func CheckThreadNonLive(id uint64) (nonLive bool, err error)

Check, if a thread has live post updates disabled

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 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 DeleteImage

func DeleteImage(id uint64, by string) error

Permanently delete an image from a post

func DeletePost

func DeletePost(id uint64, by string) error

DeletePost marks the target post as deleted

func FindPosition

func FindPosition(board, userID string) (pos auth.ModerationLevel, err error)

FindPosition returns the highest matching position of a user on a certain board. As a special case the admin user will always return "admin".

func GetAllBoardCatalog

func GetAllBoardCatalog() (threads common.Board, err error)

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

func GetAllThreadsIDs

func GetAllThreadsIDs() ([]uint64, error)

Retrieves all threads IDs in bump order

func GetBanInfo

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

GetBanInfo retrieves information about a specific ban

func GetBoardBans

func GetBoardBans(board string) (b []auth.BanRecord, err error)

Get all bans on a specific board. "all" counts as a valid board value.

func GetBoardCatalog

func GetBoardCatalog(board string) (b common.Board, err 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 GetModLog

func GetModLog(board string) (log []auth.ModLogEntry, err error)

Retrieve moderation log for a specific board

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) (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 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 GetReports

func GetReports(board string) (rep []auth.Report, err error)

Read reports for a specific board. Pass "all" for global reports.

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 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 GetThreadIDs

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

Retrieves all threads IDs on the board in bump order with stickies first

func GetThreadMutations

func GetThreadMutations(id uint64) (deleted, banned []uint64, err error)

Retvies mutations, that can happen to posts in a thread, after the post is closed

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 uint64, img common.Image) (err error)

InsertImage insert and image into and existing open post

func InsertPost

func InsertPost(p Post, sage bool) (err error)

InsertPost inserts a post into an existing thread

func InsertThread

func InsertThread(subject string, nonLive bool, 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 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 ModSpoilerImage

func ModSpoilerImage(id uint64, by string) (err error)

Spoiler image as a moderator

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 Report

func Report(id uint64, board, reason, ip string, illegal bool) error

Report a post for rule violations

func RollbackOnError

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

RollbackOnError on error undoes the transaction on error

func SetBanners

func SetBanners(board string, banners []assets.File) (err error)

Overwrite list of banners in the DB, for a specific board

func SetLoadingAnimation

func SetLoadingAnimation(board string, file assets.File) (err error)

Set loading animation for specific board. Nil file.Data means the default animation should be used.

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 SetThreadSticky

func SetThreadSticky(id uint64, sticky bool) error

Set the sticky field on a thread

func SpoilerImage

func SpoilerImage(id uint64) 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 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(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 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, Spoilered 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