♻️ Use the new dealer BaseUser and remove ExternalID

This commit is contained in:
LittleSheep 2024-09-11 23:49:20 +08:00
parent 78705e82a8
commit 6c5a99e867
7 changed files with 21 additions and 93 deletions

10
.idea/workspace.xml generated
View File

@ -4,12 +4,8 @@
<option name="autoReloadType" value="ALL" />
</component>
<component name="ChangeListManager">
<list default="true" id="18dd0d68-b4b8-40db-9734-9119b5c848bd" name="更改" comment=":bug: Fix account_id where clause causing indexing issue">
<change beforePath="$PROJECT_DIR$/.idea/dataSources.local.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/dataSources.local.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/shelf/Uncommitted_changes_before_Checkout_at_2024_8_20,_22_59_[更改]/shelved.patch" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/shelf/Uncommitted_changes_before_Checkout_at_2024_8_20__22_59___.xml" beforeDir="false" />
<list default="true" id="18dd0d68-b4b8-40db-9734-9119b5c848bd" name="更改" comment=":sparkles: Attachment API can edit metadata">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/attachments_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/attachments_api.go" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -120,7 +116,6 @@
</option>
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value=":sparkles: Self reference detection" />
<MESSAGE value=":sparkles: List attachment original filter" />
<MESSAGE value=":sparkles: Attachment pool basis" />
<MESSAGE value=":sparkles: Attachment has pool" />
@ -145,7 +140,8 @@
<MESSAGE value=":bug: Fix uploader still using old cache api" />
<MESSAGE value=":bug: Fix concurrent upload multipart cause incomplete" />
<MESSAGE value=":bug: Fix account_id where clause causing indexing issue" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Fix account_id where clause causing indexing issue" />
<MESSAGE value=":sparkles: Attachment API can edit metadata" />
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: Attachment API can edit metadata" />
</component>
<component name="VgoProject">
<settings-migrated>true</settings-migrated>

2
go.mod
View File

@ -3,7 +3,7 @@ module git.solsynth.dev/hydrogen/paperclip
go 1.21.6
require (
git.solsynth.dev/hydrogen/dealer v0.0.0-20240823113302-208da7e90fdb
git.solsynth.dev/hydrogen/dealer v0.0.0-20240911145828-d734d617bfc8
github.com/go-playground/validator/v10 v10.17.0
github.com/gofiber/fiber/v2 v2.52.4
github.com/google/uuid v1.6.0

2
go.sum
View File

@ -1,5 +1,7 @@
git.solsynth.dev/hydrogen/dealer v0.0.0-20240823113302-208da7e90fdb h1:dv4uVDMe53eBprW2Q8ocAhZuO+DKWlWyxGiJMiwE62E=
git.solsynth.dev/hydrogen/dealer v0.0.0-20240823113302-208da7e90fdb/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI=
git.solsynth.dev/hydrogen/dealer v0.0.0-20240911145828-d734d617bfc8 h1:kWheneSdSySG5tz9TAXrtr546JdMpQZTyWDFk4jeGwg=
git.solsynth.dev/hydrogen/dealer v0.0.0-20240911145828-d734d617bfc8/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=

View File

@ -1,19 +1,9 @@
package models
// Account profiles basically fetched from Hydrogen.Passport
// But cache at here for better usage
// At the same time this model can make relations between local models
type Account struct {
BaseModel
import "git.solsynth.dev/hydrogen/dealer/pkg/hyper"
Name string `json:"name"`
Nick string `json:"nick"`
Avatar string `json:"avatar"`
Banner string `json:"banner"`
Description string `json:"description"`
EmailAddress string `json:"email_address"`
PowerLevel int `json:"power_level"`
ExternalID uint `json:"external_id"`
type Account struct {
hyper.BaseUser
Attachments []Attachment `json:"attachments"`
Pools []AttachmentPool `json:"pools"`

View File

@ -1,19 +0,0 @@
package exts
import (
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/services"
"github.com/gofiber/fiber/v2"
)
func LinkAccountMiddleware(c *fiber.Ctx) error {
if val, ok := c.Locals("p_user").(*proto.UserInfo); ok {
if account, err := services.LinkAccount(val); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
c.Locals("user", account)
}
}
return c.Next()
}

View File

@ -3,9 +3,11 @@ package server
import (
"strings"
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/gap"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/server/api"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/server/exts"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
@ -54,7 +56,15 @@ func NewServer() {
}))
app.Use(gap.H.AuthMiddleware)
app.Use(exts.LinkAccountMiddleware)
app.Use(hyper.LinkAccountMiddleware(
database.C,
&models.Account{},
func(u hyper.BaseUser) models.Account {
return models.Account{
BaseUser: u,
}
},
))
api.MapAPIs(app, "/api")
}

View File

@ -1,51 +0,0 @@
package services
import (
"errors"
"fmt"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
"gorm.io/gorm"
"reflect"
)
func LinkAccount(userinfo *proto.UserInfo) (models.Account, error) {
var account models.Account
if userinfo == nil {
return account, fmt.Errorf("remote userinfo was not found")
}
if err := database.C.Where(&models.Account{
ExternalID: uint(userinfo.Id),
}).First(&account).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
account = models.Account{
Name: userinfo.Name,
Nick: userinfo.Nick,
Avatar: userinfo.Avatar,
Banner: userinfo.Banner,
Description: userinfo.GetDescription(),
EmailAddress: userinfo.Email,
PowerLevel: 0,
ExternalID: uint(userinfo.Id),
}
return account, database.C.Save(&account).Error
}
return account, err
}
prev := account
account.Name = userinfo.Name
account.Nick = userinfo.Nick
account.Avatar = userinfo.Avatar
account.Banner = userinfo.Banner
account.Description = userinfo.GetDescription()
account.EmailAddress = userinfo.Email
var err error
if !reflect.DeepEqual(prev, account) {
err = database.C.Save(&account).Error
}
return account, err
}