db

package
v0.0.0-...-688b8c7 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2022 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidCredentials = errors.New("invalid credentials")

ErrInvalidCredentials is used to define an error thrown by CheckPassword.

View Source
var ErrNotPasswordAuth = errors.New("user is not password authenticated")

ErrNotPasswordAuth is used to define an error thrown by CheckPassword.

Functions

func AddGenericEventHandler

func AddGenericEventHandler(eventName string, f func([]byte)) (deleter func())

AddGenericEventHandler is used to add an event handler for an event.

func AddWatchEvent

func AddWatchEvent[T any](tableName string, f func(T))

AddWatchEvent is used to add an event to watch a table where the events are published. T should be the type that is the same as the JSON.

func AuthenticateUserByPassword

func AuthenticateUserByPassword(ctx context.Context, usernameOrEmail, passwordInput string) (uint64, error)

AuthenticateUserByPassword is used to authenticate a user by their password. If there is an error, it will return either: a) Any other error - ignore the rest of this if this is the case. b) A half-authentication token - this is a special type of token that can be used to authenticate the user with a 2FA code. If this is the case, the error will be of type httperrors.HalfAuthentication and contain more information. c) ErrInvalidCredentials - this is returned if the credentials are invalid. d) ErrNotPasswordAuth - this is returned if the user is not using password authentication. !! PLEASE READ THE ABOVE CAREFULLY - THIS IS AUTHENTICATION, WE CANNOT AFFORD TO MAKE ERRORS !!

func BuildCrossNodeToken

func BuildCrossNodeToken(ctx context.Context, data []byte) (string, error)

BuildCrossNodeToken is used to build a 2 min TTL token which can be used to connect to the gateway.

func BuildHalfToken

func BuildHalfToken(ctx context.Context, userId uint64) (string, error)

BuildHalfToken is used to build a half token which can be used to link to a user ID and prove the first stage of authentication was successful.

func CreatePasswordUser

func CreatePasswordUser(ctx context.Context, opts UserPasswordCreateOpts) (userId uint64, err error)

CreatePasswordUser is used to create a user with a password.

func CreateSession

func CreateSession(ctx context.Context, token string, userId uint64, jobId string) error

CreateSession is used to create a new session for a user ID.

func DeleteTasks

func DeleteTasks(ctx context.Context, taskIds ...string) (deleted bool, err error)

DeleteTasks is used to delete task(s) from the database. Returns true for deleted if they all were.

func EditAndPublish

func EditAndPublish(ctx context.Context, tableName string, f func(context.Context) error, metadata any) error

EditAndPublish is used to push an edit on a table and publish the event. This will block until the events are published, so you can be assured any events are done by the time this is over.

func ExtendTask

func ExtendTask(ctx context.Context, taskId string, in time.Duration) (extended bool, err error)

ExtendTask is used to extend the scheduled time of a task.

func GetCrossNodeToken

func GetCrossNodeToken(ctx context.Context, token string) ([]byte, error)

GetCrossNodeToken is used to get the data from a cross node token and destroy it at the same time.

func GetGuildScanners

func GetGuildScanners(ctx context.Context, guildIds []uint64) (guildScanners []func(any) error, err error)

GetGuildScanners is used to get a guild scanner for every guild ID specified.

func GetHalfToken

func GetHalfToken(ctx context.Context, token string) (uint64, error)

GetHalfToken is used to get the user ID from a half token and destroy it at the same time. If the user doesn't exist, 0 is returned.

func GetMemberGuilds

func GetMemberGuilds(ctx context.Context, hostname string, memberID uint64) (guilds []string, err error)

GetMemberGuilds returns a list of guilds that the member is in.

func GetPGPKey

func GetPGPKey(maker func() (pubKey, privKey string)) (pubKey, privKey string, err error)

GetPGPKey is used to get the PGP key or create it if it doesn't exist.

func HasOwner

func HasOwner(ctx context.Context) bool

HasOwner is used to check if the server has an owner. THIS IS VERY SLOW AND SHOULD ONLY BE RAN DURING SETUP! It intentionally doesn't return an error because errors are picked up later in setup.

func Init

func Init(pgConnectionUrl, redisConnectionUrl string) error

Init is used to initialise the database connection.

func InternallyAddTask

func InternallyAddTask(ctx context.Context, task *Task, in time.Duration) (string, error)

InternallyAddTask is used to add a task in a very low level way to the database. You likely want the scheduler package for this. The task ID is returned.

func InternallyConsumeConfig

func InternallyConsumeConfig(handler func(key string, value json.RawMessage)) error

InternallyConsumeConfig is used to getting the config and handling events relating to it. This will block until the config is initially fetched, but will spin off as a side effect logic to auto-update it. The handler SHOULD be okay with concurrency. Note you probably do not want this function, but instead functions exposed in the config package.

func InternallyDeleteAllUserIDTokens

func InternallyDeleteAllUserIDTokens(ctx context.Context, userId uint64) ([]string, error)

InternallyDeleteAllUserIDTokens is used to delete all tokens for a user ID. Returns the deletion job ID's.

func InternallyDeleteToken

func InternallyDeleteToken(ctx context.Context, token string) (string, error)

InternallyDeleteToken is used to internally delete a token. This is used by the token delete job. Returns the deletion job ID and any errors.

func InternallyGetTokenJobID

func InternallyGetTokenJobID(ctx context.Context, token string) (string, error)

InternallyGetTokenJobID is used to get the job ID of a token. This is used by the token extend job.

func Migrate

func Migrate() error

Migrate runs database migrations.

func PublishGenericEvent

func PublishGenericEvent(eventName string, data []byte) error

PublishGenericEvent is used to publish a generic event.

func ScanUserFromToken

func ScanUserFromToken(ctx context.Context, token string, user any) error

ScanUserFromToken is used to scan a user from a token.

func SetupKey

func SetupKey(ctx context.Context) (string, error)

SetupKey makes the setup key and returns it. Returns an existing key if it was already edited.

func UpdateConfig

func UpdateConfig(ctx context.Context, key string, value any) error

UpdateConfig is used to write a key to the database and also dispatch events for it. Blocks until events are done.

func UseGlobalLock

func UseGlobalLock[T any](ctx context.Context, lockName string, fn func() (T, error)) (val T, err error)

UseGlobalLock uses a global lock until fn is done. The value is passed through from the function and any errors are returned.

Types

type CreatePasswordUserError

type CreatePasswordUserError struct {
	// contains filtered or unexported fields
}

CreatePasswordUserError is used to define an error thrown by CreatePasswordUser.

func (CreatePasswordUserError) Error

func (p CreatePasswordUserError) Error() string

Error implements the error interface.

func (CreatePasswordUserError) POValue

func (p CreatePasswordUserError) POValue() string

POValue is used to get the PO value of the error.

func (CreatePasswordUserError) Unwrap

func (p CreatePasswordUserError) Unwrap() error

Unwrap returns the underlying error.

type Task

type Task struct {
	// TaskID is the ID of the task. This is ignored by InternallyAddTask.
	TaskID string

	// JobHandler is the job handler in charge of running this task.
	JobHandler string

	// JobBody is the msgpack body of the job.
	JobBody []byte

	// SchedulerMetadata is the msgpack metadata for the scheduler.
	SchedulerMetadata []byte
}

Task is used to define an item in the database tasks table.

func DeleteAndReturnNTasks

func DeleteAndReturnNTasks(ctx context.Context, supportedJobs []string, n uint64) ([]*Task, error)

DeleteAndReturnNTasks is used to delete N number of tasks from the DB which are ready to be processed with the supported jobs of this worker and then deletes them from the database.

type UserFlag

type UserFlag uint64

UserFlag is used to define a flag (or set of flags) on a user.

const (
	// UserFlagOwner is used to define a server owner.
	UserFlagOwner UserFlag = 1 << iota
)

type UserPasswordCreateOpts

type UserPasswordCreateOpts struct {
	Username  string
	Email     string
	Password  string
	Confirmed bool
	Flags     UserFlag
}

UserPasswordCreateOpts is the options required to create a user with a password.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL