133 lines
3.0 KiB
Go
133 lines
3.0 KiB
Go
package user
|
|
|
|
import (
|
|
"code.mrmelon54.com/melon/summer/pkg/utils"
|
|
"github.com/sec51/twofactor"
|
|
"time"
|
|
)
|
|
|
|
type ProfileVisibility byte
|
|
|
|
const (
|
|
ProfileVisibilityPrivate = ProfileVisibility(iota)
|
|
ProfileVisibilityFriends
|
|
ProfileVisibilityPublic
|
|
)
|
|
|
|
func ParseVisibility(a *byte) ProfileVisibility {
|
|
b := ProfileVisibility(utils.SByte(a))
|
|
if b > ProfileVisibilityPublic {
|
|
return ProfileVisibilityPrivate
|
|
}
|
|
return b
|
|
}
|
|
|
|
func (v ProfileVisibility) AsByte() *byte {
|
|
return utils.PByte(byte(v))
|
|
}
|
|
|
|
type ProfilePronouns byte
|
|
|
|
const (
|
|
ProfilePronounsUnknown = ProfilePronouns(iota)
|
|
ProfilePronounsHeHim
|
|
ProfilePronounsSheHer
|
|
ProfilePronounsTheyThem
|
|
)
|
|
|
|
func ParsePronouns(a *byte) ProfilePronouns {
|
|
b := ProfilePronouns(utils.SByte(a))
|
|
if b > ProfilePronounsTheyThem {
|
|
return ProfilePronounsUnknown
|
|
}
|
|
return b
|
|
}
|
|
|
|
func (p ProfilePronouns) AsByte() *byte {
|
|
return utils.PByte(byte(p))
|
|
}
|
|
|
|
type User struct {
|
|
Id uint64 `json:"id" xorm:"pk autoincr"`
|
|
Email string `json:"email"`
|
|
Username string `json:"username"`
|
|
Password string `json:"-"`
|
|
Visibility *byte `json:"visibility" xorm:"unsigned tinyint(3)"`
|
|
Gender *string `json:"gender"`
|
|
Pronouns *byte `json:"pronouns" xorm:"unsigned tinyint(3)"`
|
|
Birthday time.Time `json:"birthday"`
|
|
DisplayName string `json:"display_name"`
|
|
Created time.Time `json:"created"`
|
|
LastOnline time.Time `json:"last_online"`
|
|
Icon string `json:"icon"`
|
|
MfaActive *bool `json:"-"`
|
|
MfaBytes []byte `json:"-"`
|
|
MfaObj *twofactor.Totp `json:"-" xorm:"-"`
|
|
EmailVerified *bool `json:"email_verified"`
|
|
EmailToken string `json:"-"`
|
|
EnableRefresh *bool `json:"-"`
|
|
Enabled *bool `json:"enabled"`
|
|
}
|
|
|
|
func (u User) GetId() uint64 {
|
|
return u.Id
|
|
}
|
|
|
|
func (u User) SetEnabled(v bool) User {
|
|
u.Enabled = utils.PBool(v)
|
|
return u
|
|
}
|
|
|
|
func (u User) ClearForNew() User {
|
|
u.Id = 0
|
|
return u
|
|
}
|
|
|
|
func (u User) ClearForPut() User {
|
|
return User{
|
|
Id: 0,
|
|
Email: "",
|
|
Username: "",
|
|
Password: "",
|
|
Visibility: u.Visibility,
|
|
Gender: u.Gender,
|
|
Pronouns: u.Pronouns,
|
|
Birthday: u.Birthday,
|
|
DisplayName: u.DisplayName,
|
|
Created: time.Time{},
|
|
LastOnline: time.Time{},
|
|
Icon: u.Icon,
|
|
MfaActive: nil,
|
|
MfaBytes: nil,
|
|
MfaObj: nil,
|
|
EmailVerified: nil,
|
|
EmailToken: "",
|
|
EnableRefresh: nil,
|
|
Enabled: nil,
|
|
}
|
|
}
|
|
|
|
func (u User) ClearForAdminPut() User {
|
|
return User{
|
|
Id: 0,
|
|
Email: "",
|
|
Username: "",
|
|
Password: "",
|
|
Visibility: nil,
|
|
Gender: nil,
|
|
Pronouns: nil,
|
|
Birthday: time.Time{},
|
|
DisplayName: "",
|
|
Created: time.Time{},
|
|
LastOnline: time.Time{},
|
|
Icon: "",
|
|
MfaActive: nil,
|
|
MfaBytes: nil,
|
|
MfaObj: nil,
|
|
EmailVerified: nil,
|
|
EmailToken: "",
|
|
EnableRefresh: u.EnableRefresh,
|
|
Enabled: u.Enabled,
|
|
}
|
|
}
|