Realm devkit

This commit is contained in:
LittleSheep 2024-09-11 22:58:28 +08:00
parent 395f97f3b5
commit d734d617bf
5 changed files with 67 additions and 15 deletions

View File

@ -1,6 +1,7 @@
package hyper package hyper
import ( import (
"gorm.io/gorm"
"strings" "strings"
"time" "time"
@ -51,10 +52,10 @@ func (v *HyperConn) AuthMiddleware(c *fiber.Ctx) error {
return c.Next() return c.Next()
} }
func LinkAccountMiddleware[T any](model any, adaptor func(u BaseUser) T) func(c *fiber.Ctx) error { func LinkAccountMiddleware[T any](tx *gorm.DB, model any, adaptor func(u BaseUser) T) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error { return func(c *fiber.Ctx) error {
if val, ok := c.Locals("p_user").(*proto.UserInfo); ok { if val, ok := c.Locals("p_user").(*proto.UserInfo); ok {
if account, err := LinkAccount(model, val); err != nil { if account, err := LinkAccount(tx, model, val); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else { } else {
c.Locals("user", adaptor(account)) c.Locals("user", adaptor(account))

View File

@ -3,7 +3,6 @@ package hyper
import ( import (
"errors" "errors"
"fmt" "fmt"
"git.solsynth.dev/hydrogen/dealer/pkg/internal/database"
"git.solsynth.dev/hydrogen/dealer/pkg/proto" "git.solsynth.dev/hydrogen/dealer/pkg/proto"
"github.com/samber/lo" "github.com/samber/lo"
"gorm.io/gorm" "gorm.io/gorm"
@ -23,12 +22,14 @@ type BaseUser struct {
AutomatedBy *uint `json:"automated_by"` AutomatedBy *uint `json:"automated_by"`
} }
func LinkAccount(model any, userinfo *proto.UserInfo) (BaseUser, error) { // LinkAccount will help you build a BaseUser model from proto.UserInfo
// And also will help you to update the info in your database, so that this function requires a database context
func LinkAccount(tx *gorm.DB, model any, userinfo *proto.UserInfo) (BaseUser, error) {
var account BaseUser var account BaseUser
if userinfo == nil { if userinfo == nil {
return account, fmt.Errorf("remote userinfo was not found") return account, fmt.Errorf("remote userinfo was not found")
} }
if err := database.C.Where("id = ?", userinfo.GetId()).Model(model).First(&account).Error; err != nil { if err := tx.Where("id = ?", userinfo.GetId()).Model(model).First(&account).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
account = BaseUser{ account = BaseUser{
BaseModel: BaseModel{ BaseModel: BaseModel{
@ -47,7 +48,7 @@ func LinkAccount(model any, userinfo *proto.UserInfo) (BaseUser, error) {
if userinfo.AutomatedBy != nil { if userinfo.AutomatedBy != nil {
account.AutomatedBy = lo.ToPtr(uint(*userinfo.AutomatedBy)) account.AutomatedBy = lo.ToPtr(uint(*userinfo.AutomatedBy))
} }
return account, database.C.Model(model).Save(&account).Error return account, tx.Model(model).Save(&account).Error
} }
return account, err return account, err
} }
@ -68,7 +69,7 @@ func LinkAccount(model any, userinfo *proto.UserInfo) (BaseUser, error) {
var err error var err error
if !reflect.DeepEqual(prev, account) { if !reflect.DeepEqual(prev, account) {
err = database.C.Model(model).Save(&account).Error err = tx.Model(model).Save(&account).Error
} }
return account, err return account, err

29
pkg/hyper/realm.go Normal file
View File

@ -0,0 +1,29 @@
package hyper
import "git.solsynth.dev/hydrogen/dealer/pkg/proto"
type BaseRealm struct {
BaseModel
Alias string `json:"alias"`
Name string `json:"name"`
Description string `json:"description"`
Avatar string `json:"avatar"`
Banner string `json:"banner"`
IsPublic bool `json:"is_public"`
IsCommunity bool `json:"is_community"`
}
// LinkRealm will help you build a BaseRealm model from proto.RealmInfo
// WARNING This function doesn't like the LinkAccount, it will not help you deal the database stuff
func LinkRealm(info *proto.RealmInfo) BaseRealm {
return BaseRealm{
Alias: info.Alias,
Name: info.Name,
Description: info.Description,
Avatar: info.Avatar,
Banner: info.Banner,
IsPublic: info.IsPublic,
IsCommunity: info.IsCommunity,
}
}

View File

@ -29,8 +29,10 @@ type RealmInfo struct {
Alias string `protobuf:"bytes,2,opt,name=alias,proto3" json:"alias,omitempty"` Alias string `protobuf:"bytes,2,opt,name=alias,proto3" json:"alias,omitempty"`
Name string `protobuf:"bytes,3,opt,name=name,proto3" json:"name,omitempty"` Name string `protobuf:"bytes,3,opt,name=name,proto3" json:"name,omitempty"`
Description string `protobuf:"bytes,4,opt,name=description,proto3" json:"description,omitempty"` Description string `protobuf:"bytes,4,opt,name=description,proto3" json:"description,omitempty"`
IsPublic bool `protobuf:"varint,5,opt,name=is_public,json=isPublic,proto3" json:"is_public,omitempty"` Avatar string `protobuf:"bytes,6,opt,name=avatar,proto3" json:"avatar,omitempty"`
IsCommunity bool `protobuf:"varint,6,opt,name=is_community,json=isCommunity,proto3" json:"is_community,omitempty"` Banner string `protobuf:"bytes,7,opt,name=banner,proto3" json:"banner,omitempty"`
IsPublic bool `protobuf:"varint,9,opt,name=is_public,json=isPublic,proto3" json:"is_public,omitempty"`
IsCommunity bool `protobuf:"varint,10,opt,name=is_community,json=isCommunity,proto3" json:"is_community,omitempty"`
} }
func (x *RealmInfo) Reset() { func (x *RealmInfo) Reset() {
@ -93,6 +95,20 @@ func (x *RealmInfo) GetDescription() string {
return "" return ""
} }
func (x *RealmInfo) GetAvatar() string {
if x != nil {
return x.Avatar
}
return ""
}
func (x *RealmInfo) GetBanner() string {
if x != nil {
return x.Banner
}
return ""
}
func (x *RealmInfo) GetIsPublic() bool { func (x *RealmInfo) GetIsPublic() bool {
if x != nil { if x != nil {
return x.IsPublic return x.IsPublic
@ -589,16 +605,19 @@ var File_realm_proto protoreflect.FileDescriptor
var file_realm_proto_rawDesc = []byte{ var file_realm_proto_rawDesc = []byte{
0x0a, 0x0b, 0x72, 0x65, 0x61, 0x6c, 0x6d, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x70, 0x0a, 0x0b, 0x72, 0x65, 0x61, 0x6c, 0x6d, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x22, 0xa7, 0x01, 0x0a, 0x09, 0x52, 0x65, 0x61, 0x6c, 0x6d, 0x49, 0x6e, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xd7, 0x01, 0x0a, 0x09, 0x52, 0x65, 0x61, 0x6c, 0x6d, 0x49, 0x6e,
0x66, 0x6f, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x02, 0x66, 0x6f, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x02,
0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x61, 0x6c, 0x69, 0x61, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x61, 0x6c, 0x69, 0x61, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28,
0x09, 0x52, 0x05, 0x61, 0x6c, 0x69, 0x61, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x09, 0x52, 0x05, 0x61, 0x6c, 0x69, 0x61, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65,
0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x20, 0x0a, 0x0b,
0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x04, 0x20, 0x01, 0x28,
0x09, 0x52, 0x0b, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x1b, 0x09, 0x52, 0x0b, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x16,
0x0a, 0x09, 0x69, 0x73, 0x5f, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0a, 0x06, 0x61, 0x76, 0x61, 0x74, 0x61, 0x72, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06,
0x61, 0x76, 0x61, 0x74, 0x61, 0x72, 0x12, 0x16, 0x0a, 0x06, 0x62, 0x61, 0x6e, 0x6e, 0x65, 0x72,
0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x62, 0x61, 0x6e, 0x6e, 0x65, 0x72, 0x12, 0x1b,
0x0a, 0x09, 0x69, 0x73, 0x5f, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x18, 0x09, 0x20, 0x01, 0x28,
0x08, 0x52, 0x08, 0x69, 0x73, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x12, 0x21, 0x0a, 0x0c, 0x69, 0x08, 0x52, 0x08, 0x69, 0x73, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x12, 0x21, 0x0a, 0x0c, 0x69,
0x73, 0x5f, 0x63, 0x6f, 0x6d, 0x6d, 0x75, 0x6e, 0x69, 0x74, 0x79, 0x18, 0x06, 0x20, 0x01, 0x28, 0x73, 0x5f, 0x63, 0x6f, 0x6d, 0x6d, 0x75, 0x6e, 0x69, 0x74, 0x79, 0x18, 0x0a, 0x20, 0x01, 0x28,
0x08, 0x52, 0x0b, 0x69, 0x73, 0x43, 0x6f, 0x6d, 0x6d, 0x75, 0x6e, 0x69, 0x74, 0x79, 0x22, 0x12, 0x08, 0x52, 0x0b, 0x69, 0x73, 0x43, 0x6f, 0x6d, 0x6d, 0x75, 0x6e, 0x69, 0x74, 0x79, 0x22, 0x12,
0x0a, 0x10, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x61, 0x6c, 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65, 0x0a, 0x10, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x61, 0x6c, 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65,
0x73, 0x74, 0x22, 0x31, 0x0a, 0x16, 0x4c, 0x6f, 0x6f, 0x6b, 0x75, 0x70, 0x55, 0x73, 0x65, 0x72, 0x73, 0x74, 0x22, 0x31, 0x0a, 0x16, 0x4c, 0x6f, 0x6f, 0x6b, 0x75, 0x70, 0x55, 0x73, 0x65, 0x72,

View File

@ -19,8 +19,10 @@ message RealmInfo {
string alias = 2; string alias = 2;
string name = 3; string name = 3;
string description = 4; string description = 4;
bool is_public = 5; string avatar = 6;
bool is_community = 6; string banner = 7;
bool is_public = 9;
bool is_community = 10;
} }
message ListRealmRequest { message ListRealmRequest {