models

package
v0.0.0-...-e282ec7 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2022 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SearchOrderByID                      = "id ASC"
	SearchOrderByIDReverse               = "id DESC"
	SearchOrderByOldest                  = "created_at ASC"
	SearchOrderByNewest                  = "created_at DESC"
	SearchOrderByLeastUpdated            = "updated_at ASC"
	SearchOrderByRecentUpdated           = "updated_at DESC"
	SearchOrderByAlphabetically          = "nickname ASC"
	SearchOrderByAlphabeticallyReverse   = "nickname DESC"
	SearchOrderByMaxFileCapacity         = "max_file_capacity ASC"
	SearchOrderByMaxFileCapacityReverse  = "max_file_capacity DESC"
	SearchOrderByUsedFileCapacity        = "used_file_capacity ASC"
	SearchOrderByUsedFileCapacityReverse = "used_file_capacity DESC"
)
View Source
const (
	MaxUserMessageCount = 32
)

Variables

View Source
var (
	CacheUserKey = "user_%d"
)

Functions

func CountFileSize

func CountFileSize() (int64, error)

func CountModel

func CountModel(m interface{}) (count int64, err error)

func CountUser

func CountUser() (int64, error)

func CreateAuthToken

func CreateAuthToken(t *AuthToken) error

func CreateFile

func CreateFile(f *File) error

func CreateMessage

func CreateMessage(msg *Message) error

func CreateOAuth2Application

func CreateOAuth2Application(app *OAuth2Application) error

func CreateOAuth2Token

func CreateOAuth2Token(t *OAuth2Token) error

func CreateUser

func CreateUser(u *User) error

func DeleteAuthTokenByID

func DeleteAuthTokenByID(id, userID uint) error

func DeleteAuthTokenByUserID

func DeleteAuthTokenByUserID(userID uint) error

func DeleteFile

func DeleteFile(f *File) error

func DeleteOAuth2Application

func DeleteOAuth2Application(app *OAuth2Application) error

func DeleteOAuth2TokenByAccessToken

func DeleteOAuth2TokenByAccessToken(accessToken string) error

func DeleteOAuth2TokenByCode

func DeleteOAuth2TokenByCode(code string) error

func DeleteOAuth2TokenByRefreshToken

func DeleteOAuth2TokenByRefreshToken(refreshToken string) error

func DeleteUser

func DeleteUser(u *User) error

func GetNicknamesByID

func GetNicknamesByID(uids []uint) (map[uint]string, error)

func IsEmailUsed

func IsEmailUsed(email string) (bool, error)

func IsErrAuthTokenEmpty

func IsErrAuthTokenEmpty(err error) bool

func IsErrAuthTokenNotExist

func IsErrAuthTokenNotExist(err error) bool

func IsErrEmailAlreadyUsed

func IsErrEmailAlreadyUsed(err error) bool

func IsErrFileAlreadyExist

func IsErrFileAlreadyExist(err error) bool

func IsErrFileLocked

func IsErrFileLocked(err error) bool

func IsErrFileMaxSizeLimit

func IsErrFileMaxSizeLimit(err error) bool

func IsErrFileNotDirectory

func IsErrFileNotDirectory(err error) bool

func IsErrFileNotExist

func IsErrFileNotExist(err error) bool

func IsErrFileParentNotDirectory

func IsErrFileParentNotDirectory(err error) bool

func IsErrMessageReceiverNotExist

func IsErrMessageReceiverNotExist(err error) bool

func IsErrMessageSendNotExist

func IsErrMessageSendNotExist(err error) bool

func IsErrModifyRootFile

func IsErrModifyRootFile(err error) bool

func IsErrOAuth2ApplicationNotExist

func IsErrOAuth2ApplicationNotExist(err error) bool

func IsErrOAuth2TokenNotExist

func IsErrOAuth2TokenNotExist(err error) bool

func IsErrUserNotExist

func IsErrUserNotExist(err error) bool

func IsErrUsernameAlreadyUsed

func IsErrUsernameAlreadyUsed(err error) bool

func IsFileUnlockFailed

func IsFileUnlockFailed(err error) bool

func IsUserExists

func IsUserExists(uid int64) (bool, error)

func IsUsernameUsed

func IsUsernameUsed(username string) (bool, error)

func LockUserFile

func LockUserFile(ctx context.Context, uid uint) (id string, err error)

func LoginTypeToString

func LoginTypeToString(t LoginType) string

func MoveFile

func MoveFile(f *File, dir *File) error

func NewEngine

func NewEngine(ctx context.Context, migrateFunc func(*gorm.DB) error) error

func RenameFile

func RenameFile(f *File) error

func SaveUser

func SaveUser(u *User) error

func UnlockUserFile

func UnlockUserFile(ctx context.Context, uid uint, id string) error

func UpdateAuthToken

func UpdateAuthToken(t *AuthToken) error

Types

type AuthToken

type AuthToken struct {
	ID        uint `gorm:"primary_key"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time `sql:"index"`

	Code              string `gorm:"unique_index;not null"`
	UserID            uint   `gorm:"index"`
	User              *User  `gorm:"-"`
	HasRecentActivity bool   `gorm:"-"`
}

func GetAuthTokenByCode

func GetAuthTokenByCode(code string) (*AuthToken, error)

func ListAuthTokens

func ListAuthTokens(uid uint, listOpts ListOptions) ([]*AuthToken, error)

func (*AuthToken) AfterFind

func (t *AuthToken) AfterFind()

func (*AuthToken) LoadUser

func (t *AuthToken) LoadUser() (err error)

type ErrAuthTokenEmpty

type ErrAuthTokenEmpty struct {
}

func (ErrAuthTokenEmpty) Error

func (e ErrAuthTokenEmpty) Error() string

type ErrAuthTokenNotExist

type ErrAuthTokenNotExist struct {
	Code   string
	ID     uint
	UserID uint
}

func (ErrAuthTokenNotExist) Error

func (e ErrAuthTokenNotExist) Error() string

type ErrEmailAlreadyUsed

type ErrEmailAlreadyUsed struct {
	Email string
}

func (ErrEmailAlreadyUsed) Error

func (err ErrEmailAlreadyUsed) Error() string

type ErrFileAlreadyExist

type ErrFileAlreadyExist struct {
	ID     uint
	Path   string
	Owner  uint
	FileID string
}

func (ErrFileAlreadyExist) Error

func (err ErrFileAlreadyExist) Error() string

type ErrFileLocked

type ErrFileLocked struct {
	ID uint
}

func (ErrFileLocked) Error

func (err ErrFileLocked) Error() string

type ErrFileNotDirectory

type ErrFileNotDirectory struct {
	ID   uint
	Path string
}

func (ErrFileNotDirectory) Error

func (err ErrFileNotDirectory) Error() string

type ErrFileNotExist

type ErrFileNotExist struct {
	ID     uint
	Path   string
	Owner  uint
	FileID string
}

func (ErrFileNotExist) Error

func (err ErrFileNotExist) Error() string

type ErrFileParentNotDirectory

type ErrFileParentNotDirectory struct {
	ID   uint
	Path string
}

func (ErrFileParentNotDirectory) Error

func (err ErrFileParentNotDirectory) Error() string

type ErrFileUnlockFailed

type ErrFileUnlockFailed struct {
	ID     uint
	LockID string
}

func (ErrFileUnlockFailed) Error

func (err ErrFileUnlockFailed) Error() string

type ErrMessageReceiverNotExist

type ErrMessageReceiverNotExist struct {
	ID   uint
	Role MessageRoleType
}

func (ErrMessageReceiverNotExist) Error

type ErrMessageSenderNotExist

type ErrMessageSenderNotExist struct {
	ID   uint
	Role MessageRoleType
}

func (ErrMessageSenderNotExist) Error

func (e ErrMessageSenderNotExist) Error() string

type ErrModifyRootFile

type ErrModifyRootFile struct {
	ID    uint
	Owner uint
}

func (ErrModifyRootFile) Error

func (err ErrModifyRootFile) Error() string

type ErrOAuth2ApplicationNotExist

type ErrOAuth2ApplicationNotExist struct {
	ID       uint
	ClientID string
	Name     string
	UserID   uint
}

func (ErrOAuth2ApplicationNotExist) Error

type ErrOAuth2TokenNotExist

type ErrOAuth2TokenNotExist struct {
	Code         string
	AccessToken  string
	RefreshToken string
}

func (ErrOAuth2TokenNotExist) Error

func (e ErrOAuth2TokenNotExist) Error() string

type ErrUserMaxFileCapacityLimit

type ErrUserMaxFileCapacityLimit struct {
	UserID uint
}

func (ErrUserMaxFileCapacityLimit) Error

func (err ErrUserMaxFileCapacityLimit) Error() string

type ErrUserNotExist

type ErrUserNotExist struct {
	ID       uint
	Email    string
	Username string
}

func (ErrUserNotExist) Error

func (err ErrUserNotExist) Error() string

type ErrUsernameAlreadyUsed

type ErrUsernameAlreadyUsed struct {
	Username string
}

func (ErrUsernameAlreadyUsed) Error

func (err ErrUsernameAlreadyUsed) Error() string

type File

type File struct {
	ID        uint `gorm:"primary_key"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time `sql:"index"`

	FileID   string `gorm:"unique_index"`
	FileDir  string
	FileName string

	FileType FileType
	FileSize int64

	Owner    uint
	ParentID uint
}

func CreateDirectory

func CreateDirectory(parent *File, dirName string) (*File, error)

func CreateUserRootFile

func CreateUserRootFile(u *User) *File

func GetFileByID

func GetFileByID(id uint, uid uint) (*File, error)

func GetUserRootFile

func GetUserRootFile(uid uint) (*File, error)

func SearchFile

func SearchFile(opts *SearchFileOptions) (files []*File, _ int64, _ error)

func TryUploadFile

func TryUploadFile(u *User, p *File, header *multipart.FileHeader) (*File, error)

func (*File) FilePath

func (f *File) FilePath() string

func (*File) IsDir

func (f *File) IsDir() bool

func (*File) IsRoot

func (f *File) IsRoot() bool

func (*File) LocalPath

func (f *File) LocalPath() string

func (*File) ReadDir

func (f *File) ReadDir(opts ReadDirOption) ([]*File, error)

type FileType

type FileType int
const (
	FileTypeNone FileType = iota
	FileTypeFile
	FileTypeDir
)

type FindUserMessageOption

type FindUserMessageOption struct {
	UserID      uint
	Status      UserMessageStatus
	Orientation TimestampOrientation
	Timestamp   time.Time
	Count       uint
}

type ListOptions

type ListOptions struct {
	Page     int
	PageSize int
}

func (ListOptions) SetDefaultOptions

func (opts ListOptions) SetDefaultOptions()

func (ListOptions) SetEnginePagination

func (opts ListOptions) SetEnginePagination(e *gorm.DB) *gorm.DB

type LoginType

type LoginType int
const (
	LoginNoType LoginType = iota
	LoginPlain
	LoginOAuth2
)

type Message

type Message struct {
	ID        uint      `gorm:"primary_key"`
	CreatedAt time.Time `sql:"index"`

	SenderID     uint `sql:"index"`
	SenderType   MessageRoleType
	ReceiverID   uint `sql:"index"`
	ReceiverType MessageRoleType

	ContentType string
	Content     []byte `gorm:"varchar(512)"`
}

type MessageRoleType

type MessageRoleType int
const (
	MessageRoleSystem MessageRoleType = iota
	MessageRoleUser
	MessageRoleGroup
)

type OAuth2Application

type OAuth2Application struct {
	ID        uint `gorm:"primary_key"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time `sql:"index"`

	UID  uint  `gorm:"index"`
	User *User `gorm:"-"`

	Name         string
	ClientID     string `gorm:"unique_index"`
	ClientSecret string
	Domain       string
}

func GetOAuth2ApplicationByClientID

func GetOAuth2ApplicationByClientID(clientID string) (*OAuth2Application, error)

func GetOAuth2ApplicationByUserID

func GetOAuth2ApplicationByUserID(userID uint) ([]*OAuth2Application, error)

func (*OAuth2Application) LoadUser

func (app *OAuth2Application) LoadUser() (err error)

type OAuth2Token

type OAuth2Token struct {
	ID               uint   `gorm:"primary_key"`
	ClientID         string `gorm:"index"`
	UserID           string `gorm:"index"`
	RedirectURI      string
	Scope            string
	Code             string `gorm:"index"`
	CodeExpiredAt    time.Time
	CodeExpiresIn    time.Duration
	Access           string `gorm:"index"`
	AccessExpiredAt  time.Time
	AccessExpiresIn  time.Duration
	Refresh          string `gorm:"index"`
	RefreshExpiredAt time.Time
	RefreshExpiresIn time.Duration
}

func GetOAuth2TokenByAccessToken

func GetOAuth2TokenByAccessToken(token string) (*OAuth2Token, error)

func GetOAuth2TokenByCode

func GetOAuth2TokenByCode(code string) (*OAuth2Token, error)

func GetOAuth2TokenByRefreshToken

func GetOAuth2TokenByRefreshToken(token string) (*OAuth2Token, error)

func (*OAuth2Token) GetAccess

func (t *OAuth2Token) GetAccess() string

func (*OAuth2Token) GetAccessCreateAt

func (t *OAuth2Token) GetAccessCreateAt() time.Time

func (*OAuth2Token) GetAccessExpiresIn

func (t *OAuth2Token) GetAccessExpiresIn() time.Duration

func (*OAuth2Token) GetClientID

func (t *OAuth2Token) GetClientID() string

func (*OAuth2Token) GetCode

func (t *OAuth2Token) GetCode() string

func (*OAuth2Token) GetCodeCreateAt

func (t *OAuth2Token) GetCodeCreateAt() time.Time

func (*OAuth2Token) GetCodeExpiresIn

func (t *OAuth2Token) GetCodeExpiresIn() time.Duration

func (*OAuth2Token) GetRedirectURI

func (t *OAuth2Token) GetRedirectURI() string

func (*OAuth2Token) GetRefresh

func (t *OAuth2Token) GetRefresh() string

func (*OAuth2Token) GetRefreshCreateAt

func (t *OAuth2Token) GetRefreshCreateAt() time.Time

func (*OAuth2Token) GetRefreshExpiresIn

func (t *OAuth2Token) GetRefreshExpiresIn() time.Duration

func (*OAuth2Token) GetScope

func (t *OAuth2Token) GetScope() string

func (*OAuth2Token) GetUserID

func (t *OAuth2Token) GetUserID() string

func (*OAuth2Token) Invalid

func (t *OAuth2Token) Invalid() error

func (*OAuth2Token) InvalidAccessToken

func (t *OAuth2Token) InvalidAccessToken() error

func (*OAuth2Token) InvalidCode

func (t *OAuth2Token) InvalidCode() error

func (*OAuth2Token) InvalidRefreshToken

func (t *OAuth2Token) InvalidRefreshToken() error

func (*OAuth2Token) New

func (t *OAuth2Token) New() oauth2.TokenInfo

func (*OAuth2Token) SetAccess

func (t *OAuth2Token) SetAccess(token string)

func (*OAuth2Token) SetAccessCreateAt

func (t *OAuth2Token) SetAccessCreateAt(createAt time.Time)

func (*OAuth2Token) SetAccessExpiresIn

func (t *OAuth2Token) SetAccessExpiresIn(d time.Duration)

func (*OAuth2Token) SetClientID

func (t *OAuth2Token) SetClientID(id string)

func (*OAuth2Token) SetCode

func (t *OAuth2Token) SetCode(code string)

func (*OAuth2Token) SetCodeCreateAt

func (t *OAuth2Token) SetCodeCreateAt(createAt time.Time)

func (*OAuth2Token) SetCodeExpiresIn

func (t *OAuth2Token) SetCodeExpiresIn(d time.Duration)

func (*OAuth2Token) SetRedirectURI

func (t *OAuth2Token) SetRedirectURI(uri string)

func (*OAuth2Token) SetRefresh

func (t *OAuth2Token) SetRefresh(token string)

func (*OAuth2Token) SetRefreshCreateAt

func (t *OAuth2Token) SetRefreshCreateAt(createAt time.Time)

func (*OAuth2Token) SetRefreshExpiresIn

func (t *OAuth2Token) SetRefreshExpiresIn(d time.Duration)

func (*OAuth2Token) SetScope

func (t *OAuth2Token) SetScope(scope string)

func (*OAuth2Token) SetUserID

func (t *OAuth2Token) SetUserID(id string)

type ReadDirOption

type ReadDirOption struct {
	OnlyDir bool
}

type SearchFileOptions

type SearchFileOptions struct {
	ListOptions
	Keyword string
	Type    FileType
	FID     uint
	Owner   uint
	OrderBy SearchOrderBy
	Actor   *File
}

func (*SearchFileOptions) Apply

func (opts *SearchFileOptions) Apply(e *gorm.DB) *gorm.DB

type SearchOrderBy

type SearchOrderBy string

func (SearchOrderBy) String

func (s SearchOrderBy) String() string

type SearchUserOptions

type SearchUserOptions struct {
	ListOptions
	Keyword       string
	Type          UserType
	UID           uint
	IsActive      sql.NullBool
	SearchByEmail bool
	OrderBy       SearchOrderBy
	Actor         *User
}

func (*SearchUserOptions) Apply

func (opts *SearchUserOptions) Apply(e *gorm.DB) *gorm.DB

type Statistic

type Statistic struct {
	Counter struct {
		User, File, TotalFileSize int64
	}
}

func GetStatistic

func GetStatistic() (stats Statistic)

type TimestampOrientation

type TimestampOrientation int
const (
	TimestampOrientationForward TimestampOrientation = iota // 向后查询
	TimestampOrientationBack                                // 向前查询
)

type User

type User struct {
	ID          uint       `gorm:"primary_key"`
	CreatedAt   time.Time  `sql:"index"`
	UpdatedAt   time.Time  `sql:"index"`
	DeletedAt   *time.Time `sql:"index"`
	LastLoginAt time.Time  `sql:"index"`

	Nickname string `gorm:"not null"`
	Username string `gorm:"unique_index,not null"`
	Email    string `gorm:"unique_index,not null"`
	Password string `gorm:"not null" json:"-"`

	MustChangePassword bool `gorm:"not null,default:false"`

	IsActive bool `sql:"index"`
	IsAdmin  bool

	LoginType LoginType
	Type      UserType

	Avatar string `gorm:"varchar(2048)"`

	UsedFileCapacity int64
	MaxFileCapacity  int64
}

func GetUserByEmail

func GetUserByEmail(email string) (*User, error)

func GetUserByID

func GetUserByID(uid uint) (*User, error)

func GetUserByUsername

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

func SearchUser

func SearchUser(opts *SearchUserOptions) (users []*User, _ int64, _ error)

func UserSignIn

func UserSignIn(username string, password string) (*User, error)
func (u *User) AvatarLink() string

func (*User) AvatarPath

func (u *User) AvatarPath() string

func (*User) CanIUploadFile

func (u *User) CanIUploadFile(size int64) bool

func (*User) DeleteAvatar

func (u *User) DeleteAvatar() error

func (*User) GetRootDir

func (u *User) GetRootDir() ([]*File, error)

func (*User) HashPassword

func (u *User) HashPassword(pwd string)

func (*User) IsOAuth2

func (u *User) IsOAuth2() bool

func (*User) IsPasswordSet

func (u *User) IsPasswordSet() bool

func (*User) IsPlain

func (u *User) IsPlain() bool

func (*User) RemainingFileSize

func (u *User) RemainingFileSize() int64

func (*User) UploadAvatar

func (u *User) UploadAvatar(data []byte) error

func (*User) ValidatePassword

func (u *User) ValidatePassword(pwd string) bool

type UserMessage

type UserMessage struct {
	ID        uint      `gorm:"primary_key"`
	CreatedAt time.Time `gorm:"index"`
	UpdatedAt time.Time
	DeletedAt *time.Time `sql:"index"`
	UserID    uint       `gorm:"index"`
	MessageID uint
	Status    UserMessageStatus
}

func FindUserMessages

func FindUserMessages(opts FindUserMessageOption) ([]*UserMessage, error)

type UserMessageStatus

type UserMessageStatus int
const (
	UserMessageStatusNone UserMessageStatus = iota
	UserMessageStatusRead
	UserMessageStatusUnread
	UserMessageStatusUndo
)

type UserType

type UserType int
const (
	UserTypeUser UserType = iota
)

Jump to

Keyboard shortcuts

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