db

package
v0.0.0-...-d0e669d Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUserNotExists   = errors.New("账号不存在")
	ErrBadCredential   = errors.New("邮箱或密码错误")
	ErrDuplicateEmail  = errors.New("这个邮箱已经注册过账号了!")
	ErrDuplicateDomain = errors.New("个性域名重复了,换一个吧~")
)
View Source
var AllTables = []interface{}{
	&User{}, &Question{}, &CensorLog{}, &UploadImage{}, &UploadImageQuestion{},
}
View Source
var ErrCensorLogsNotFound = errors.New("censor logs dose not exist")
View Source
var ErrQuestionNotExist = errors.New("提问不存在")

Functions

func Init

func Init() (*gorm.DB, error)

Types

type CensorLog

type CensorLog struct {
	gorm.Model
	SourceName  string
	Input       string
	InputHash   string `gorm:"index"`
	Pass        bool
	RawResponse json.RawMessage
}

type CensorLogsStore

type CensorLogsStore interface {
	GetByText(ctx context.Context, sourceName, text string, noLongerThan ...time.Time) (*CensorLog, error)
	Create(ctx context.Context, options CreateCensorLogOptions) error
}
var CensorLogs CensorLogsStore

func NewCensorLogsStore

func NewCensorLogsStore(db *gorm.DB) CensorLogsStore

type CreateCensorLogOptions

type CreateCensorLogOptions struct {
	SourceName  string
	Input       string
	Pass        bool
	RawResponse json.RawMessage
}

type CreateQuestionOptions

type CreateQuestionOptions struct {
	FromIP            string
	UserID            uint
	Content           string
	ReceiveReplyEmail string
	AskerUserID       uint
}

type CreateUploadImageOptions

type CreateUploadImageOptions struct {
	Type               UploadImageQuestionType
	QuestionID         uint
	UploaderUserID     uint
	Name               string
	FileSize           int64
	Md5                string
	Key                string
	PublicURLs         map[string]string
	IsDeletingPrevious bool
}

type CreateUserOptions

type CreateUserOptions struct {
	Name       string
	Password   string
	Email      string
	Avatar     string
	Domain     string
	Background string
	Intro      string
}

type GetQuestionsByAskUserIDOptions

type GetQuestionsByAskUserIDOptions struct {
	*dbutil.Cursor
	FilterAnswered bool
	ShowPrivate    bool
}

type GetQuestionsByUserIDOptions

type GetQuestionsByUserIDOptions struct {
	*dbutil.Cursor
	FilterAnswered bool
	ShowPrivate    bool
}

type GetQuestionsCountOptions

type GetQuestionsCountOptions struct {
	FilterAnswered bool
	ShowPrivate    bool
}

type HarassmentSettingType

type HarassmentSettingType string
const (
	HarassmentSettingNone             HarassmentSettingType = "none"
	HarassmentSettingTypeRegisterOnly HarassmentSettingType = "register_only"
)

type NotifyType

type NotifyType string
const (
	NotifyTypeEmail NotifyType = "email"
	NotifyTypeNone  NotifyType = "none"
)

type Question

type Question struct {
	dbutil.Model
	FromIP                string         `json:"-"`
	UserID                uint           `gorm:"index:idx_question_user_id" json:"-"`
	Content               string         `json:"content"`
	ContentCensorMetadata datatypes.JSON `json:"-"`
	ContentCensorPass     bool           `gorm:"->;type:boolean GENERATED ALWAYS AS (IFNULL(content_censor_metadata->'$.pass' = true, false)) STORED NOT NULL" json:"-"`
	Token                 string         `json:"-"`
	Answer                string         `json:"answer"`
	AnswerCensorMetadata  datatypes.JSON `json:"-"`
	AnswerCensorPass      bool           `gorm:"->;type:boolean GENERATED ALWAYS AS (IFNULL(answer_censor_metadata->'$.pass' = true, false)) STORED NOT NULL" json:"-"`
	ReceiveReplyEmail     string         `json:"-"`
	AskerUserID           uint           `json:"-"`
	IsPrivate             bool           `gorm:"default: FALSE; NOT NULL" json:"-"`
}

type QuestionsStore

type QuestionsStore interface {
	Create(ctx context.Context, opts CreateQuestionOptions) (*Question, error)
	GetByID(ctx context.Context, id uint) (*Question, error)
	GetByUserID(ctx context.Context, userID uint, opts GetQuestionsByUserIDOptions) ([]*Question, error)
	GetByAskUserID(ctx context.Context, userID uint, opts GetQuestionsByAskUserIDOptions) ([]*Question, error)
	AnswerByID(ctx context.Context, id uint, answer string) error
	DeleteByID(ctx context.Context, id uint) error
	UpdateCensor(ctx context.Context, id uint, opts UpdateQuestionCensorOptions) error
	Count(ctx context.Context, userID uint, opts GetQuestionsCountOptions) (int64, error)
	SetPrivate(ctx context.Context, id uint) error
	SetPublic(ctx context.Context, id uint) error
}
var Questions QuestionsStore

func NewQuestionsStore

func NewQuestionsStore(db *gorm.DB) QuestionsStore

type UpdateQuestionCensorOptions

type UpdateQuestionCensorOptions struct {
	ContentCensorMetadata json.RawMessage
	AnswerCensorMetadata  json.RawMessage
}

type UpdateUserOptions

type UpdateUserOptions struct {
	Name       string
	Avatar     string
	Background string
	Intro      string
	Notify     NotifyType
}

type UploadImage

type UploadImage struct {
	gorm.Model
	UploaderUserID uint
	Name           string
	FileSize       int64
	Md5            string
	Key            string
	PublicURLs     datatypes.JSON
}

type UploadImageQuestion

type UploadImageQuestion struct {
	Type          UploadImageQuestionType
	UploadImageID uint
	QuestionID    uint
}

type UploadImageQuestionType

type UploadImageQuestionType string
const (
	UploadImageQuestionTypeAsk    UploadImageQuestionType = "ask"
	UploadImageQuestionTypeAnswer UploadImageQuestionType = "answer"
)

type UploadImagesStore

type UploadImagesStore interface {
	Create(ctx context.Context, opts CreateUploadImageOptions) (*UploadImage, error)
	GetByQuestionID(ctx context.Context, questionID uint) ([]*UploadImage, error)
	GetByTypeQuestionID(ctx context.Context, typ UploadImageQuestionType, questionID uint) ([]*UploadImage, error)
}
var UploadImgaes UploadImagesStore

func NewUploadImagesStore

func NewUploadImagesStore(db *gorm.DB) UploadImagesStore

type User

type User struct {
	gorm.Model        `json:"-"`
	Name              string                `json:"name"`
	Password          string                `json:"-"`
	Email             string                `json:"email"`
	Avatar            string                `json:"avatar"`
	Domain            string                `json:"domain"`
	Background        string                `json:"background"`
	Intro             string                `json:"intro"`
	Notify            NotifyType            `json:"notify"`
	HarassmentSetting HarassmentSettingType `json:"harassment_setting"`
}

func (*User) Authenticate

func (u *User) Authenticate(password string) bool

func (*User) EncodePassword

func (u *User) EncodePassword()

type UsersStore

type UsersStore interface {
	Create(ctx context.Context, opts CreateUserOptions) error
	GetByID(ctx context.Context, id uint) (*User, error)
	GetByEmail(ctx context.Context, email string) (*User, error)
	GetByDomain(ctx context.Context, domain string) (*User, error)
	Update(ctx context.Context, id uint, opts UpdateUserOptions) error
	UpdateHarassmentSetting(ctx context.Context, id uint, typ HarassmentSettingType) error
	Authenticate(ctx context.Context, email, password string) (*User, error)
	ChangePassword(ctx context.Context, id uint, oldPassword, newPassword string) error
	UpdatePassword(ctx context.Context, id uint, newPassword string) error
	Deactivate(ctx context.Context, id uint) error
}
var Users UsersStore

func NewUsersStore

func NewUsersStore(db *gorm.DB) UsersStore

Jump to

Keyboard shortcuts

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