From d734d617bfc8b3ac19954c187241451b50152126 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 11 Sep 2024 22:58:28 +0800 Subject: [PATCH] :sparkles: Realm devkit --- pkg/hyper/auth_adaptor.go | 5 +++-- pkg/hyper/identity.go | 11 ++++++----- pkg/hyper/realm.go | 29 +++++++++++++++++++++++++++++ pkg/proto/realm.pb.go | 31 +++++++++++++++++++++++++------ pkg/proto/realm.proto | 6 ++++-- 5 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 pkg/hyper/realm.go diff --git a/pkg/hyper/auth_adaptor.go b/pkg/hyper/auth_adaptor.go index 8322585..a7e4473 100644 --- a/pkg/hyper/auth_adaptor.go +++ b/pkg/hyper/auth_adaptor.go @@ -1,6 +1,7 @@ package hyper import ( + "gorm.io/gorm" "strings" "time" @@ -51,10 +52,10 @@ func (v *HyperConn) AuthMiddleware(c *fiber.Ctx) error { 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 { 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()) } else { c.Locals("user", adaptor(account)) diff --git a/pkg/hyper/identity.go b/pkg/hyper/identity.go index d46a717..a6b4334 100644 --- a/pkg/hyper/identity.go +++ b/pkg/hyper/identity.go @@ -3,7 +3,6 @@ package hyper import ( "errors" "fmt" - "git.solsynth.dev/hydrogen/dealer/pkg/internal/database" "git.solsynth.dev/hydrogen/dealer/pkg/proto" "github.com/samber/lo" "gorm.io/gorm" @@ -23,12 +22,14 @@ type BaseUser struct { 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 if userinfo == nil { 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) { account = BaseUser{ BaseModel: BaseModel{ @@ -47,7 +48,7 @@ func LinkAccount(model any, userinfo *proto.UserInfo) (BaseUser, error) { if userinfo.AutomatedBy != nil { 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 } @@ -68,7 +69,7 @@ func LinkAccount(model any, userinfo *proto.UserInfo) (BaseUser, error) { var err error if !reflect.DeepEqual(prev, account) { - err = database.C.Model(model).Save(&account).Error + err = tx.Model(model).Save(&account).Error } return account, err diff --git a/pkg/hyper/realm.go b/pkg/hyper/realm.go new file mode 100644 index 0000000..6f4dd5f --- /dev/null +++ b/pkg/hyper/realm.go @@ -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, + } +} diff --git a/pkg/proto/realm.pb.go b/pkg/proto/realm.pb.go index 8246d85..15c8b87 100644 --- a/pkg/proto/realm.pb.go +++ b/pkg/proto/realm.pb.go @@ -29,8 +29,10 @@ type RealmInfo struct { Alias string `protobuf:"bytes,2,opt,name=alias,proto3" json:"alias,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"` - IsPublic bool `protobuf:"varint,5,opt,name=is_public,json=isPublic,proto3" json:"is_public,omitempty"` - IsCommunity bool `protobuf:"varint,6,opt,name=is_community,json=isCommunity,proto3" json:"is_community,omitempty"` + Avatar string `protobuf:"bytes,6,opt,name=avatar,proto3" json:"avatar,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() { @@ -93,6 +95,20 @@ func (x *RealmInfo) GetDescription() string { 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 { if x != nil { return x.IsPublic @@ -589,16 +605,19 @@ var File_realm_proto protoreflect.FileDescriptor var file_realm_proto_rawDesc = []byte{ 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, 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, 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, - 0x09, 0x52, 0x0b, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x1b, - 0x0a, 0x09, 0x69, 0x73, 0x5f, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x18, 0x05, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x0b, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x16, + 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, - 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, 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, diff --git a/pkg/proto/realm.proto b/pkg/proto/realm.proto index 6542982..6ed5cd6 100644 --- a/pkg/proto/realm.proto +++ b/pkg/proto/realm.proto @@ -19,8 +19,10 @@ message RealmInfo { string alias = 2; string name = 3; string description = 4; - bool is_public = 5; - bool is_community = 6; + string avatar = 6; + string banner = 7; + bool is_public = 9; + bool is_community = 10; } message ListRealmRequest {