db

package
v0.0.0-...-db5c55e Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2022 License: AGPL-3.0-or-later Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Props        = Properties{}
	BuiltInRoles = map[UUID]*Role{
		"o": &Role{
			0, "o", 0, "Owner", "", pa, pa, false, pa, pa, Time(etc.Epoch), pa, pa,
		},
	}
)
View Source
var (
	// ResourceTables is the list of db table names that represent the various resources in Mantle
	ResourceTables = []string{cTableUsers, cTableChannels, cTableRoles, cTableInvites, cTableAudits}
)

Functions

func ActionLen

func ActionLen() int

ActionLen returns the amount of actions

func Close

func Close()

Close db

func Init

func Init()

Init sets up db tables and properties

Types

type Action

type Action int16

Action is a custom sql/driver type to handle Audit Log actions

const (
	ActionSettingUpdate Action
	ActionUserUpdate
	ActionChannelCreate
	ActionChannelUpdate
	ActionChannelDelete
	ActionRoleCreate
	ActionRoleUpdate
	ActionRoleDelete
	ActionInviteCreate
	ActionInviteUpdate
	ActionInviteDelete
	ActionInviteUse
)

Actions Enum

func (*Action) Scan

func (p *Action) Scan(value interface{}) error

Scan - Implement the database/sql Scanner interface

func (Action) String

func (p Action) String() string

String - Implement the fmt Stringer interface

func (Action) Value

func (p Action) Value() (driver.Value, error)

Value - Implement the database/sql Valuer interface

type Audit

type Audit struct {
	ID        int64  `json:"id"`
	UUID      UUID   `json:"uuid" dbsorm:"1"`
	CreatedOn Time   `json:"created_on" dbsorm:"1"`
	Action    Action `json:"action" dbsorm:"1"`
	Agent     UUID   `json:"agent" dbsorm:"1"`
	Affected  UUID   `json:"affected" dbsorm:"1"`
	Key       string `json:"a_key" dbsorm:"1"`
	Value     string `json:"a_value" dbsorm:"1"`
}

func CreateAudit

func CreateAudit(ac Action, agent *User, aff UUID, key, val string) *Audit

func (Audit) All

func (Audit) All() []*Audit

All returns an array of all channels sorted by their position

func (Audit) Scan

func (v Audit) Scan(rows *sql.Rows) dbstorage.Scannable

Scan implements dbstorage.Scannable

type Channel

type Channel struct {
	ID          int64  `json:"id"`
	UUID        UUID   `json:"uuid" dbsorm:"1"`
	Position    int    `json:"position" dbsorm:"1"`
	Name        string `json:"name" dbsorm:"1"`
	Description string `json:"description" dbsorm:"1"`
	HistoryOff  bool   `json:"history_off" dbsorm:"1"`
	LatestMsg   UUID   `json:"latest_message" dbsorm:"1"`
	CreatedOn   Time   `json:"created_on" dbsorm:"1"`
}

func CreateChannel

func CreateChannel(name string) *Channel

func QueryChannelByUUID

func QueryChannelByUUID(uid UUID) (*Channel, bool)

func (Channel) All

func (v Channel) All() []*Channel

All returns an array of all channels sorted by their position

func (*Channel) AssertMessageTableExists

func (c *Channel) AssertMessageTableExists()

func (*Channel) Delete

func (v *Channel) Delete()

Delete removes this item from the database

func (*Channel) EnableHistory

func (v *Channel) EnableHistory(b bool)

EnableHistory sets position

func (*Channel) MessageCount

func (v *Channel) MessageCount() int64

func (*Channel) MoveTo

func (v *Channel) MoveTo(n int)

MoveTo sets position cleanly

func (*Channel) QueryMsgAfterUID

func (c *Channel) QueryMsgAfterUID(uid UUID, limit int) []*Message

QueryMsgAfterUID runs 'select * from messages where uuid < ? order by uuid desc limit 50'

func (Channel) Scan

func (v Channel) Scan(rows *sql.Rows) dbstorage.Scannable

Scan implements dbstorage.Scannable

func (*Channel) SetDescription

func (v *Channel) SetDescription(s string)

SetDescription sets description

func (*Channel) SetName

func (v *Channel) SetName(s string)

SetName sets name

func (*Channel) SetPosition

func (v *Channel) SetPosition(n int)

SetPosition sets position

type Invite

type Invite struct {
	ID         int64    `json:"id"`
	UUID       UUID     `json:"uuid" dbsorm:"1"`
	CreatedOn  Time     `json:"created_on" dbsorm:"1"`
	Code       string   `json:"name" dbsorm:"1"`
	Uses       int64    `json:"uses" dbsorm:"1"`
	MaxUses    int64    `json:"max_uses" dbsorm:"1"`
	Mode       int      `json:"mode" dbsorm:"1"`
	ExpiresIn  Duration `json:"expires_in" dbsorm:"1"`
	ExpiresOn  Time     `json:"expires_on" dbsorm:"1"`
	IsFrozen   bool     `json:"is_frozen" dbsorm:"1"`
	GivenRoles List     `json:"given_roles" dbsorm:"1"`
}

func CreateInvite

func CreateInvite() *Invite

CreateInvite creates a new permanent invite and returns it

func QueryInviteByCode

func QueryInviteByCode(c string) (*Invite, bool)

QueryInviteByCode does exactly that

func QueryInviteByUID

func QueryInviteByUID(uid UUID) (*Invite, bool)

QueryInviteByUID does exactly that

func (Invite) All

func (v Invite) All() []*Invite

All queries database for all currently existing Invites

func (*Invite) Delete

func (v *Invite) Delete()

Delete removes this item from the database

func (Invite) Scan

func (v Invite) Scan(rows *sql.Rows) dbstorage.Scannable

Scan implements dbstorage.Scannable

func (*Invite) SetExpIn

func (v *Invite) SetExpIn(x [2]int)

SetExpIn does

func (*Invite) SetExpOn

func (v *Invite) SetExpOn(t time.Time)

SetExpOn does

func (*Invite) SetMaxUses

func (v *Invite) SetMaxUses(p int64)

SetMaxUses sets

func (*Invite) SetMode

func (v *Invite) SetMode(x int)

SetMode does

func (*Invite) Use

func (v *Invite) Use(u *User)

Use increments Uses by 1

type List

type List []string

List is a custom sql/driver type to handle list columns

func (*List) Scan

func (a *List) Scan(value interface{}) error

Scan - Implement the database/sql/driver Scanner interface

func (*List) String

func (a *List) String() string

func (List) Value

func (a List) Value() (driver.Value, error)

Value - Implement the database/sql/driver Valuer interface

type Message

type Message struct {
	ID   int64  `json:"id"`
	UUID UUID   `json:"uuid" dbsorm:"1"`
	At   Time   `json:"time" dbsorm:"1"`
	By   UUID   `json:"author" dbsorm:"1"`
	Body string `json:"body" dbsorm:"1"`
}

func CreateMessage

func CreateMessage(user *User, channel *Channel, body string) *Message

func (Message) Scan

func (v Message) Scan(rows *sql.Rows) dbstorage.Scannable

Scan implements dbstorage.Scannable

type Perm

type Perm uint8

Perm is a custom sql/driver type to handle 3-state permissions

const (
	PermDeny Perm = iota
	PermIgnore
	PermAllow
)

func (*Perm) Scan

func (p *Perm) Scan(value interface{}) error

Scan - Implement the database/sql Scanner interface

func (Perm) ToBool

func (p Perm) ToBool() bool

ToBool returns true if and only if this Perm is equal to PermAllow

func (Perm) Value

func (p Perm) Value() (driver.Value, error)

Value - Implement the database/sql Valuer interface

type Properties

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

Properties is a KV-store abstraction around a cached db table

func (*Properties) Decrement

func (p *Properties) Decrement(key string)

Decrement subtracts 1 from key's value if it is an integer

func (*Properties) Get

func (p *Properties) Get(key string) string

Get retrieves the value of a single key

func (*Properties) GetAll

func (p *Properties) GetAll() map[string]string

GetAll returns entire store in a map structure

func (*Properties) GetInt64

func (p *Properties) GetInt64(key string) int64

GetInt64 returns key's value as an int64

func (*Properties) GetSome

func (p *Properties) GetSome(ks ...string) map[string]string

GetSome returns a subset of the store in a map structure

func (*Properties) Has

func (p *Properties) Has(key string) bool

Has tests whether this Properties contains a certain key

func (*Properties) Increment

func (p *Properties) Increment(key string)

Increment adds 1 to key's value if it is an integer

func (*Properties) Init

func (p *Properties) Init()

Init is called at all SetDefaults and loads cache from db table

func (*Properties) Set

func (p *Properties) Set(key string, val string) bool

Set sets the value of a single key

func (*Properties) SetDefault

func (p *Properties) SetDefault(key string, value string)

SetDefault sets a key's value only if that key has never been set

func (*Properties) SetDefaultInt64

func (p *Properties) SetDefaultInt64(key string, value int64)

SetDefaultInt64 sets key's value to value if never set before

func (*Properties) SetInt64

func (p *Properties) SetInt64(key string, value int64)

SetInt64 sets key's value to value

type Role

type Role struct {
	ID                 int64  `json:"id"`
	UUID               UUID   `json:"uuid" dbsorm:"1"`
	Position           int    `json:"position" dbsorm:"1"`
	Name               string `json:"name" dbsorm:"1"`
	Color              string `json:"color" dbsorm:"1"`
	PermManageChannels Perm   `json:"perm_manage_channels" dbsorm:"1"`
	PermManageRoles    Perm   `json:"perm_manage_roles" dbsorm:"1"`
	Distinguish        bool   `json:"distinguish" dbsorm:"1"`
	PermManageServer   Perm   `json:"perm_manage_server" dbsorm:"1"`
	PermManageInvites  Perm   `json:"perm_manage_invites" dbsorm:"1"`
	CreatedOn          Time   `json:"created_on" dbsorm:"1"`
	PermViewAudits     Perm   `json:"perm_view_audits" dbsorm:"1"`
	PermManageBans     Perm   `json:"perm_manage_bans" dbsorm:"1"`
}

func CreateRole

func CreateRole(name string) *Role

func QueryRoleByUID

func QueryRoleByUID(uid UUID) (*Role, bool)

QueryRoleByUID finds a Role with the specified uid

func (Role) All

func (v Role) All() []*Role

All queries database for all currently existing Roles

func (Role) AllSorted

func (v Role) AllSorted() []*Role

AllSorted is the same as All but ordered by position

func (*Role) Delete

func (v *Role) Delete() []*User

Delete removes this item from the database and returns the list of users that got the role removed

func (*Role) MoveTo

func (v *Role) MoveTo(n int)

MoveTo sets position cleanly

func (Role) Scan

func (v Role) Scan(rows *sql.Rows) dbstorage.Scannable

Scan implements dbstorage.Scannable

func (*Role) SetColor

func (v *Role) SetColor(s string)

SetColor sets color

func (*Role) SetDistinguish

func (v *Role) SetDistinguish(b bool)

SetDistinguish sets position

func (*Role) SetName

func (v *Role) SetName(s string)

SetName sets name

func (*Role) SetPermMngBans

func (v *Role) SetPermMngBans(p Perm)

SetPermMngBans sets

func (*Role) SetPermMngChannels

func (v *Role) SetPermMngChannels(p Perm)

SetPermMngChannels sets

func (*Role) SetPermMngInvites

func (v *Role) SetPermMngInvites(p Perm)

SetPermMngInvites sets

func (*Role) SetPermMngRoles

func (v *Role) SetPermMngRoles(p Perm)

SetPermMngRoles sets

func (*Role) SetPermMngServer

func (v *Role) SetPermMngServer(p Perm)

SetPermMngServer sets

func (*Role) SetPermViewAudits

func (v *Role) SetPermViewAudits(p Perm)

SetPermViewAudits sets

func (*Role) SetPosition

func (v *Role) SetPosition(n int)

SetPosition sets position

type Setting

type Setting struct {
	ID    int64  `json:"id"`
	Key   string `json:"key" dbsorm:"1"`
	Value string `json:"value" dbsorm:"1"`
}

func QuerySettingByKey

func QuerySettingByKey(key string) (*Setting, bool)

func (Setting) All

func (v Setting) All() []*Setting

func (Setting) Scan

func (v Setting) Scan(rows *sql.Rows) dbstorage.Scannable

Scan implements dbstorage.Scannable

type User

type User struct {
	ID         int64  `json:"id"`
	Provider   string `json:"provider" dbsorm:"1"`
	Snowflake  string `json:"snowflake" dbsorm:"1"`
	UUID       UUID   `json:"uuid" dbsorm:"1"`
	IsMember   bool   `json:"is_member" dbsorm:"1"`
	IsBanned   bool   `json:"is_banned" dbsorm:"1"`
	Name       string `json:"name" dbsorm:"1"`
	Nickname   string `json:"nickname" dbsorm:"1"`
	JoindedOn  Time   `json:"joined_on" dbsorm:"1"`
	LastActive Time   `json:"last_active" dbsorm:"1"`
	Roles      List   `json:"roles" dbsorm:"1"`
}

func QueryUserBySnowflake

func QueryUserBySnowflake(provider string, flake string, name string) *User

func QueryUserByUUID

func QueryUserByUUID(uid UUID) (*User, bool)

func (*User) AddRole

func (u *User) AddRole(role UUID)

func (User) Count

func (v User) Count() int64

Count returns the total number of Users

func (*User) DeleteMessage

func (u *User) DeleteMessage(c *Channel, uid UUID)

DeleteMessage attempts to delete a UID from this Channel's associated message table. If the UID is not a message in this Channel, nothing happens.

func (*User) GetRoles

func (u *User) GetRoles() []*Role

func (*User) GetRolesSorted

func (u *User) GetRolesSorted() []*Role

func (*User) HasRole

func (u *User) HasRole(role UUID) bool

func (User) MemberCount

func (v User) MemberCount() int64

func (*User) RemoveRole

func (u *User) RemoveRole(role UUID)

func (*User) ResetUID

func (u *User) ResetUID()

func (User) Scan

func (v User) Scan(rows *sql.Rows) dbstorage.Scannable

Scan implements dbstorage.Scannable

func (*User) SetAsMember

func (u *User) SetAsMember(b bool)

func (*User) SetName

func (u *User) SetName(s string)

func (*User) SetNickname

func (u *User) SetNickname(s string)

func (*User) SetUID

func (u *User) SetUID(uid UUID)

Jump to

Keyboard shortcuts

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