100 lines
2.4 KiB
Go
100 lines
2.4 KiB
Go
package models
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/nose7en/ToyBoomServer/defs"
|
|
"github.com/spf13/cast"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var _ defs.UserGettable = (*User)(nil)
|
|
|
|
func NewUserGettable(opt ...func(*User)) defs.UserGettable {
|
|
u := &User{}
|
|
for _, o := range opt {
|
|
o(u)
|
|
}
|
|
return u
|
|
}
|
|
|
|
type User struct {
|
|
gorm.Model
|
|
AppleUserID string `json:"apple_user_id" gorm:"uniqueIndex"` // user id from apple
|
|
Name string `json:"name"` // ToyBoom's user name
|
|
Username string `json:"username"` // user name from apple
|
|
Email string `json:"email"` // email from apple
|
|
IsPrivateEmail bool `json:"is_private_email"`
|
|
EmailVerified bool `json:"email_verified"`
|
|
}
|
|
|
|
func (u *User) FromUserInfo(userInfo defs.UserGettable) {
|
|
u.AppleUserID = userInfo.GetAppleUserID()
|
|
u.Name = userInfo.GetName()
|
|
u.Username = userInfo.GetUsername()
|
|
u.Email = userInfo.GetEmail()
|
|
u.IsPrivateEmail = userInfo.GetIsPrivateEmail()
|
|
u.EmailVerified = userInfo.GetEmailVerified()
|
|
}
|
|
|
|
func (u *User) FromJWTClaims(claims jwt.MapClaims) {
|
|
u.ID = cast.ToUint(claims["user_id"])
|
|
u.AppleUserID = cast.ToString(claims["apple_user_id"])
|
|
u.Name = cast.ToString(claims["name"])
|
|
u.Username = cast.ToString(claims["username"])
|
|
u.Email = cast.ToString(claims["email"])
|
|
u.IsPrivateEmail = cast.ToBool(claims["is_private_email"])
|
|
u.EmailVerified = cast.ToBool(claims["email_verified"])
|
|
}
|
|
|
|
func (u *User) ToMap() map[string]string {
|
|
return map[string]string{
|
|
"user_id": fmt.Sprint(u.ID),
|
|
"apple_user_id": u.AppleUserID,
|
|
"name": u.Name,
|
|
"username": u.Username,
|
|
"email": u.Email,
|
|
"is_private_email": fmt.Sprint(u.IsPrivateEmail),
|
|
"email_verified": fmt.Sprint(u.EmailVerified),
|
|
}
|
|
}
|
|
|
|
func (u *User) GetName() string {
|
|
return u.Name
|
|
}
|
|
|
|
func (u *User) GetUsername() string {
|
|
return u.Username
|
|
}
|
|
|
|
func (u *User) GetAppleUserID() string {
|
|
return u.AppleUserID
|
|
}
|
|
|
|
func (u *User) GetUserID() int64 {
|
|
return int64(u.ID)
|
|
}
|
|
|
|
func (u *User) GetEmail() string {
|
|
return u.Email
|
|
}
|
|
|
|
func (u *User) GetIsPrivateEmail() bool {
|
|
return u.IsPrivateEmail
|
|
}
|
|
|
|
func (u *User) GetEmailVerified() bool {
|
|
return u.EmailVerified
|
|
}
|
|
|
|
func (u *User) ToUser() defs.User {
|
|
return defs.User{
|
|
ID: int64(u.ID),
|
|
AppleUserID: u.AppleUserID,
|
|
Name: u.Name,
|
|
Username: u.Username,
|
|
Email: u.Email,
|
|
}
|
|
}
|