From f601476e6bafcf1e530260542c64e2f6fbc11bd3 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 16 Jul 2024 14:41:51 +0800 Subject: [PATCH] :bug: Fix wont link account to local model --- pkg/internal/models/accounts.go | 60 +++++--------------------------- pkg/internal/server/exts/auth.go | 21 +++++++++++ pkg/internal/server/server.go | 1 + 3 files changed, 31 insertions(+), 51 deletions(-) diff --git a/pkg/internal/models/accounts.go b/pkg/internal/models/accounts.go index 0e9da28..4adc4c0 100644 --- a/pkg/internal/models/accounts.go +++ b/pkg/internal/models/accounts.go @@ -1,63 +1,21 @@ package models import ( - "fmt" "time" - "github.com/samber/lo" - "github.com/spf13/viper" "gorm.io/datatypes" ) type Account struct { BaseModel - Name string `json:"name" gorm:"uniqueIndex"` - Nick string `json:"nick"` - Description string `json:"description"` - Avatar *uint `json:"avatar"` - Banner *uint `json:"banner"` - ConfirmedAt *time.Time `json:"confirmed_at"` - SuspendedAt *time.Time `json:"suspended_at"` - PermNodes datatypes.JSONMap `json:"perm_nodes"` - - Contacts []AccountContact `json:"contacts"` -} - -func (v Account) GetAvatar() *string { - if v.Avatar != nil { - return lo.ToPtr(fmt.Sprintf("%s/api/attachments/%d", viper.GetString("content_endpoint"), *v.Avatar)) - } - return nil -} - -func (v Account) GetBanner() *string { - if v.Banner != nil { - return lo.ToPtr(fmt.Sprintf("%s/api/attachments/%d", viper.GetString("content_endpoint"), *v.Banner)) - } - return nil -} - -func (v Account) GetPrimaryEmail() AccountContact { - val, _ := lo.Find(v.Contacts, func(item AccountContact) bool { - return item.Type == EmailAccountContact && item.IsPrimary - }) - return val -} - -type AccountContactType = int8 - -const ( - EmailAccountContact = AccountContactType(iota) -) - -type AccountContact struct { - BaseModel - - Type int8 `json:"type"` - Content string `json:"content" gorm:"uniqueIndex"` - IsPublic bool `json:"is_public"` - IsPrimary bool `json:"is_primary"` - VerifiedAt *time.Time `json:"verified_at"` - AccountID uint `json:"account_id"` + Name string `json:"name" gorm:"uniqueIndex"` + Nick string `json:"nick"` + Description string `json:"description"` + Avatar string `json:"avatar"` + Banner string `json:"banner"` + EmailAddress string `json:"email"` + ConfirmedAt *time.Time `json:"confirmed_at"` + SuspendedAt *time.Time `json:"suspended_at"` + PermNodes datatypes.JSONMap `json:"perm_nodes"` } diff --git a/pkg/internal/server/exts/auth.go b/pkg/internal/server/exts/auth.go index 435067b..3437550 100644 --- a/pkg/internal/server/exts/auth.go +++ b/pkg/internal/server/exts/auth.go @@ -8,6 +8,7 @@ import ( "git.solsynth.dev/hydrogen/dealer/pkg/hyper" "git.solsynth.dev/hydrogen/dealer/pkg/internal/directory" + "git.solsynth.dev/hydrogen/dealer/pkg/internal/models" "git.solsynth.dev/hydrogen/dealer/pkg/proto" "github.com/gofiber/fiber/v2" jsoniter "github.com/json-iterator/go" @@ -117,3 +118,23 @@ func EnsureGrantedPerm(c *fiber.Ctx, key string, val any) error { } return nil } + +func LinkAccountMiddleware(c *fiber.Ctx) error { + if val, ok := c.Locals("p_user").(*proto.UserInfo); ok { + account := models.Account{ + BaseModel: models.BaseModel{ + ID: uint(val.Id), + }, + Name: val.Name, + Nick: val.Nick, + Avatar: val.Avatar, + Banner: val.Banner, + Description: val.GetDescription(), + EmailAddress: val.GetEmail(), + } + + c.Locals("user", account) + } + + return c.Next() +} diff --git a/pkg/internal/server/server.go b/pkg/internal/server/server.go index 5ced5b1..d90f354 100644 --- a/pkg/internal/server/server.go +++ b/pkg/internal/server/server.go @@ -53,6 +53,7 @@ func NewServer() *HTTPApp { })) app.Use(exts.AuthMiddleware) + app.Use(exts.LinkAccountMiddleware) api.MapAPIs(app)