db

package
v1.8.4 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: AGPL-3.0 Imports: 33 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SettingDisableSignup          = "disable-signup"
	SettingRequireLogin           = "require-login"
	SettingAllowGistsWithoutLogin = "allow-gists-without-login"
	SettingDisableLoginForm       = "disable-login-form"
	SettingDisableGravatar        = "disable-gravatar"
)
View Source
const (
	SQLite databaseType = iota
	PostgreSQL
	MySQL
)

Variables

View Source
var DatabaseInfo *databaseInfo

Functions

func Close

func Close() error

func CountAll

func CountAll(table interface{}) (int64, error)

func CountAllGistsForkedByUser

func CountAllGistsForkedByUser(fromUserId uint, currentUserId uint) (int64, error)

func CountAllGistsFromUser

func CountAllGistsFromUser(fromUserId uint, currentUserId uint) (int64, error)

func CountAllGistsLikedByUser

func CountAllGistsLikedByUser(fromUserId uint, currentUserId uint) (int64, error)

func DeprecationDBFilename added in v1.8.0

func DeprecationDBFilename()

func GetAllGistsVisibleByUser added in v1.6.0

func GetAllGistsVisibleByUser(userId uint) ([]uint, error)

func GetAllWACredentialsForUser added in v1.8.0

func GetAllWACredentialsForUser(userID uint) ([]webauthn.Credential, error)

func GetSetting

func GetSetting(key string) (string, error)

func GetSettings

func GetSettings() (map[string]string, error)

func GetUsersFromEmails

func GetUsersFromEmails(emailsSet map[string]struct{}) (map[string]*User, error)

func InvitationCodeExists added in v1.7.0

func InvitationCodeExists(code string) (bool, error)

func IsUniqueConstraintViolation

func IsUniqueConstraintViolation(err error) bool

func Ping added in v1.6.0

func Ping() error

func SSHKeyDoesExists

func SSHKeyDoesExists(sshKeyContent string) (bool, error)

func SSHKeyLastUsedNow

func SSHKeyLastUsedNow(sshKeyContent string) error

func Setup

func Setup(dbUri string, sharedCache bool) error

func TruncateDatabase added in v1.8.0

func TruncateDatabase() error

func UpdateSetting

func UpdateSetting(key string, value string) error

func UserExists

func UserExists(username string) (bool, error)

Types

type AdminSetting

type AdminSetting struct {
	Key   string `gorm:"index:,unique"`
	Value string
}

type AuthInfo added in v1.8.0

type AuthInfo struct{}

func (AuthInfo) AllowGistsWithoutLogin added in v1.8.0

func (auth AuthInfo) AllowGistsWithoutLogin() (bool, error)

func (AuthInfo) RequireLogin added in v1.8.0

func (auth AuthInfo) RequireLogin() (bool, error)

type CrendentialDTO added in v1.8.0

type CrendentialDTO struct {
	PasskeyName string `json:"passkeyname" validate:"max=50"`
}

type FileDTO

type FileDTO struct {
	Filename string `validate:"excludes=\x2f,excludes=\x5c,max=255"`
	Content  string `validate:"required"`
}

type Gist

type Gist struct {
	ID              uint `gorm:"primaryKey"`
	Uuid            string
	Title           string
	URL             string
	Preview         string
	PreviewFilename string
	Description     string
	Private         Visibility // 0: public, 1: unlisted, 2: private
	UserID          uint
	User            User
	NbFiles         int
	NbLikes         int
	NbForks         int
	CreatedAt       int64
	UpdatedAt       int64

	Likes    []User `gorm:"many2many:likes;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
	Forked   *Gist  `gorm:"foreignKey:ForkedID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
	ForkedID uint
}

func GetAllGists

func GetAllGists(offset int) ([]*Gist, error)

func GetAllGistsByIds added in v1.6.0

func GetAllGistsByIds(ids []uint) ([]*Gist, error)

func GetAllGistsForCurrentUser

func GetAllGistsForCurrentUser(currentUserId uint, offset int, sort string, order string) ([]*Gist, error)

func GetAllGistsForkedByUser

func GetAllGistsForkedByUser(fromUserId uint, currentUserId uint, offset int, sort string, order string) ([]*Gist, error)

func GetAllGistsFromSearch

func GetAllGistsFromSearch(currentUserId uint, query string, offset int, sort string, order string) ([]*Gist, error)

func GetAllGistsFromUser

func GetAllGistsFromUser(fromUserId uint, currentUserId uint, offset int, sort string, order string) ([]*Gist, error)

func GetAllGistsLikedByUser

func GetAllGistsLikedByUser(fromUserId uint, currentUserId uint, offset int, sort string, order string) ([]*Gist, error)

func GetAllGistsRows

func GetAllGistsRows() ([]*Gist, error)

func GetGist

func GetGist(user string, gistUuid string) (*Gist, error)

func GetGistByID

func GetGistByID(gistId string) (*Gist, error)

func (*Gist) AddAndCommitFile added in v1.6.0

func (gist *Gist) AddAndCommitFile(file *FileDTO) error

func (*Gist) AddAndCommitFiles

func (gist *Gist) AddAndCommitFiles(files *[]FileDTO) error

func (*Gist) AddInIndex added in v1.6.0

func (gist *Gist) AddInIndex()

func (*Gist) AppendUserLike

func (gist *Gist) AppendUserLike(user *User) error

func (*Gist) BeforeDelete

func (gist *Gist) BeforeDelete(tx *gorm.DB) error

func (*Gist) CanWrite

func (gist *Gist) CanWrite(user *User) bool

func (*Gist) Create

func (gist *Gist) Create() error

func (*Gist) CreateForked

func (gist *Gist) CreateForked() error

func (*Gist) Delete

func (gist *Gist) Delete() error

func (*Gist) DeleteRepository

func (gist *Gist) DeleteRepository() error

func (*Gist) File

func (gist *Gist) File(revision string, filename string, truncate bool) (*git.File, error)

func (*Gist) FileNames added in v1.6.0

func (gist *Gist) FileNames(revision string) ([]string, error)

func (*Gist) Files

func (gist *Gist) Files(revision string, truncate bool) ([]*git.File, error)

func (*Gist) ForkClone

func (gist *Gist) ForkClone(username string, uuid string) error

func (*Gist) GetForkParent

func (gist *Gist) GetForkParent(user *User) (*Gist, error)

func (*Gist) GetForks

func (gist *Gist) GetForks(currentUserId uint, offset int) ([]*Gist, error)

func (*Gist) GetLanguagesFromFiles added in v1.6.0

func (gist *Gist) GetLanguagesFromFiles() ([]string, error)

func (*Gist) GetUsersLikes

func (gist *Gist) GetUsersLikes(offset int) ([]*User, error)

func (*Gist) Identifier added in v1.6.0

func (gist *Gist) Identifier() string

func (*Gist) IncrementForkCount

func (gist *Gist) IncrementForkCount() error

func (*Gist) InitRepository

func (gist *Gist) InitRepository() error

func (*Gist) Log

func (gist *Gist) Log(skip int) ([]*git.Commit, error)

func (*Gist) NbCommits

func (gist *Gist) NbCommits() (string, error)

func (*Gist) RPC

func (gist *Gist) RPC(service string) ([]byte, error)

func (*Gist) RemoveFromIndex added in v1.6.0

func (gist *Gist) RemoveFromIndex()

func (*Gist) RemoveUserLike

func (gist *Gist) RemoveUserLike(user *User) error

func (*Gist) SetLastActiveNow

func (gist *Gist) SetLastActiveNow() error

func (*Gist) ToIndexedGist added in v1.6.0

func (gist *Gist) ToIndexedGist() (*index.Gist, error)

func (*Gist) Update

func (gist *Gist) Update() error

func (*Gist) UpdateNoTimestamps added in v1.6.0

func (gist *Gist) UpdateNoTimestamps() error

func (*Gist) UpdatePreviewAndCount

func (gist *Gist) UpdatePreviewAndCount(withTimestampUpdate bool) error

func (*Gist) UpdateServerInfo

func (gist *Gist) UpdateServerInfo() error

func (*Gist) VisibilityStr added in v1.6.0

func (gist *Gist) VisibilityStr() string

type GistDTO

type GistDTO struct {
	Title       string    `validate:"max=250" form:"title"`
	Description string    `validate:"max=1000" form:"description"`
	URL         string    `validate:"max=32,alphanumdashorempty" form:"url"`
	Files       []FileDTO `validate:"min=1,dive"`
	Name        []string  `form:"name"`
	Content     []string  `form:"content"`
	VisibilityDTO
}

func (*GistDTO) ToExistingGist

func (dto *GistDTO) ToExistingGist(gist *Gist) *Gist

func (*GistDTO) ToGist

func (dto *GistDTO) ToGist() *Gist

type Invitation added in v1.7.0

type Invitation struct {
	ID        uint `gorm:"primaryKey"`
	Code      string
	ExpiresAt int64
	NbUsed    uint
	NbMax     uint
}

func GetAllInvitations added in v1.7.0

func GetAllInvitations() ([]*Invitation, error)

func GetInvitationByCode added in v1.7.0

func GetInvitationByCode(code string) (*Invitation, error)

func GetInvitationByID added in v1.7.0

func GetInvitationByID(id uint) (*Invitation, error)

func (*Invitation) Create added in v1.7.0

func (i *Invitation) Create() error

func (*Invitation) Delete added in v1.7.0

func (i *Invitation) Delete() error

func (*Invitation) IsExpired added in v1.7.0

func (i *Invitation) IsExpired() bool

func (*Invitation) IsMaxedOut added in v1.7.0

func (i *Invitation) IsMaxedOut() bool

func (*Invitation) IsUsable added in v1.7.0

func (i *Invitation) IsUsable() bool

func (*Invitation) Update added in v1.7.0

func (i *Invitation) Update() error

func (*Invitation) Use added in v1.7.0

func (i *Invitation) Use() error

type Like

type Like struct {
	UserID    uint `gorm:"primaryKey"`
	GistID    uint `gorm:"primaryKey"`
	CreatedAt int64
}

type MigrationVersion

type MigrationVersion struct {
	ID      uint `gorm:"primaryKey"`
	Version uint
}

type SSHKey

type SSHKey struct {
	ID         uint `gorm:"primaryKey"`
	Title      string
	Content    string
	SHA        string
	CreatedAt  int64
	LastUsedAt int64
	UserID     uint
	User       User `validate:"-" `
}

func GetSSHKeyByID

func GetSSHKeyByID(sshKeyId uint) (*SSHKey, error)

func GetSSHKeysByUserID

func GetSSHKeysByUserID(userId uint) ([]*SSHKey, error)

func SSHKeyExistsForUser

func SSHKeyExistsForUser(sshKey string, userId uint) (*SSHKey, error)

func (*SSHKey) BeforeCreate

func (sshKey *SSHKey) BeforeCreate(*gorm.DB) error

func (*SSHKey) Create

func (sshKey *SSHKey) Create() error

func (*SSHKey) Delete

func (sshKey *SSHKey) Delete() error

type SSHKeyDTO

type SSHKeyDTO struct {
	Title   string `form:"title" validate:"required,max=50"`
	Content string `form:"content" validate:"required"`
}

func (*SSHKeyDTO) ToSSHKey

func (dto *SSHKeyDTO) ToSSHKey() *SSHKey

type TOTP added in v1.8.0

type TOTP struct {
	ID            uint `gorm:"primaryKey"`
	UserID        uint `gorm:"uniqueIndex"`
	User          User
	Secret        string
	RecoveryCodes jsonData `gorm:"type:json"`
	CreatedAt     int64
	LastUsedAt    int64
}

func GetTOTPByUserID added in v1.8.0

func GetTOTPByUserID(userID uint) (*TOTP, error)

func (*TOTP) Create added in v1.8.0

func (totp *TOTP) Create() error

func (*TOTP) Delete added in v1.8.0

func (totp *TOTP) Delete() error

func (*TOTP) GenerateRecoveryCodes added in v1.8.0

func (totp *TOTP) GenerateRecoveryCodes() ([]string, error)

func (*TOTP) StoreSecret added in v1.8.0

func (totp *TOTP) StoreSecret(secret string) error

func (*TOTP) ValidateCode added in v1.8.0

func (totp *TOTP) ValidateCode(code string) (bool, error)

func (*TOTP) ValidateRecoveryCode added in v1.8.0

func (totp *TOTP) ValidateRecoveryCode(code string) (bool, error)

type TOTPDTO added in v1.8.0

type TOTPDTO struct {
	Code string `form:"code" validate:"max=50"`
}

type User

type User struct {
	ID        uint   `gorm:"primaryKey"`
	Username  string `gorm:"uniqueIndex,size:191"`
	Password  string
	IsAdmin   bool
	CreatedAt int64
	Email     string
	MD5Hash   string // for gravatar, if no Email is specified, the value is random
	AvatarURL string
	GithubID  string
	GitlabID  string
	GiteaID   string
	OIDCID    string `gorm:"column:oidc_id"`

	Gists               []Gist               `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;foreignKey:UserID"`
	SSHKeys             []SSHKey             `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;foreignKey:UserID"`
	Liked               []Gist               `gorm:"many2many:likes;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
	WebAuthnCredentials []WebAuthnCredential `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;foreignKey:UserID"`
}

func GetAllUsers

func GetAllUsers(offset int) ([]*User, error)

func GetUserByCredentialID added in v1.8.0

func GetUserByCredentialID(credID binaryData) (*User, error)

func GetUserById

func GetUserById(userId uint) (*User, error)

func GetUserByProvider

func GetUserByProvider(id string, provider string) (*User, error)

func GetUserByUsername

func GetUserByUsername(username string) (*User, error)

func GetUserFromSSHKey added in v1.7.3

func GetUserFromSSHKey(sshKey string) (*User, error)

func (*User) BeforeDelete

func (user *User) BeforeDelete(tx *gorm.DB) error

func (*User) Create

func (user *User) Create() error

func (*User) Delete

func (user *User) Delete() error

func (*User) DeleteProviderID

func (user *User) DeleteProviderID(provider string) error

func (*User) HasLiked

func (user *User) HasLiked(gist *Gist) (bool, error)

func (*User) HasMFA added in v1.8.0

func (user *User) HasMFA() (bool, bool, error)

func (*User) SetAdmin

func (user *User) SetAdmin() error

func (*User) Update

func (user *User) Update() error

type UserDTO

type UserDTO struct {
	Username string `form:"username" validate:"required,max=24,alphanumdash,notreserved"`
	Password string `form:"password" validate:"required"`
}

func (*UserDTO) ToUser

func (dto *UserDTO) ToUser() *User

type Visibility added in v1.6.0

type Visibility int
const (
	PublicVisibility Visibility = iota
	UnlistedVisibility
	PrivateVisibility
)

func ParseVisibility added in v1.6.0

func ParseVisibility[T string | int](v T) (Visibility, error)

func (Visibility) Next added in v1.6.0

func (v Visibility) Next() Visibility

func (Visibility) String added in v1.7.3

func (v Visibility) String() string

type VisibilityDTO added in v1.7.3

type VisibilityDTO struct {
	Private Visibility `validate:"number,min=0,max=2" form:"private"`
}

type WebAuthnCredential added in v1.8.0

type WebAuthnCredential struct {
	ID                 uint `gorm:"primaryKey"`
	Name               string
	UserID             uint
	User               User
	CredentialID       binaryData `gorm:"type:binary_data"`
	PublicKey          binaryData `gorm:"type:binary_data"`
	AttestationType    string
	AAGUID             binaryData `gorm:"type:binary_data"`
	SignCount          uint32
	CloneWarning       bool
	FlagUserPresent    bool
	FlagUserVerified   bool
	FlagBackupEligible bool
	FlagBackupState    bool
	CreatedAt          int64
	LastUsedAt         int64
}

func CreateFromCrendential added in v1.8.0

func CreateFromCrendential(userID uint, name string, cred *webauthn.Credential) (*WebAuthnCredential, error)

func GetAllCredentialsForUser added in v1.8.0

func GetAllCredentialsForUser(userID uint) ([]WebAuthnCredential, error)

func GetCredentialByID added in v1.8.0

func GetCredentialByID(id binaryData) (*WebAuthnCredential, error)

func GetCredentialByIDDB added in v1.8.0

func GetCredentialByIDDB(id uint) (*WebAuthnCredential, error)

func (*WebAuthnCredential) Delete added in v1.8.0

func (w *WebAuthnCredential) Delete() error

func (*WebAuthnCredential) TableName added in v1.8.0

func (*WebAuthnCredential) TableName() string

func (*WebAuthnCredential) UpdateLastUsedAt added in v1.8.0

func (w *WebAuthnCredential) UpdateLastUsedAt() error

func (*WebAuthnCredential) UpdateSignCount added in v1.8.0

func (w *WebAuthnCredential) UpdateSignCount() error

Jump to

Keyboard shortcuts

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