69 lines
1.2 KiB
Go
69 lines
1.2 KiB
Go
package shared
|
|
|
|
import (
|
|
"context"
|
|
"fusenapi/model/gmodel"
|
|
"fusenapi/utils/auth"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/lni/dragonboat/v4"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type SharedState struct {
|
|
shardID uint64
|
|
replicaID uint64
|
|
nh *dragonboat.NodeHost
|
|
}
|
|
|
|
func GetUserState(UserId int64, gdb *gorm.DB) (us *UserState, err error) {
|
|
models := gmodel.NewAllModels(gdb)
|
|
user, err := models.FsUser.FindUserById(context.TODO(), UserId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
userState := &UserState{
|
|
UserId: UserId,
|
|
PwdHash: auth.StringToHash(*user.PasswordHash),
|
|
UpdateAt: time.Now().UTC(),
|
|
}
|
|
|
|
return userState, nil
|
|
}
|
|
|
|
func (ss *SharedState) GetUserState(Userid int64) (us *UserState, err error) {
|
|
|
|
ius, err := ss.nh.SyncRead(context.TODO(), ss.shardID, Userid)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return nil, err
|
|
}
|
|
|
|
if ius != nil {
|
|
return ius.(*UserState), nil
|
|
}
|
|
|
|
cs := ss.nh.GetNoOPSession(128)
|
|
|
|
cmd := &CmdUpdate{UserId: Userid}
|
|
cmdBuf, err := FsPasser.PackToBytes(cmd)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result, err := ss.nh.SyncPropose(context.TODO(), cs, cmdBuf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
us = &UserState{}
|
|
err = us.Decode(result.Data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return us, err
|
|
}
|