⬆️ Upgrade dealer

This commit is contained in:
LittleSheep 2024-09-17 16:52:42 +08:00
parent f6482225ab
commit ee16094855
7 changed files with 20 additions and 60 deletions

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.22
toolchain go1.22.1 toolchain go1.22.1
require ( require (
git.solsynth.dev/hydrogen/dealer v0.0.0-20240914133539-4a48b980f0b3 git.solsynth.dev/hydrogen/dealer v0.0.0-20240917083841-b14c0240a75f
github.com/go-playground/validator/v10 v10.17.0 github.com/go-playground/validator/v10 v10.17.0
github.com/gofiber/fiber/v2 v2.52.4 github.com/gofiber/fiber/v2 v2.52.4
github.com/google/uuid v1.6.0 github.com/google/uuid v1.6.0

2
go.sum
View File

@ -2,6 +2,8 @@ git.solsynth.dev/hydrogen/dealer v0.0.0-20240911145828-d734d617bfc8 h1:kWheneSdS
git.solsynth.dev/hydrogen/dealer v0.0.0-20240911145828-d734d617bfc8/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI= git.solsynth.dev/hydrogen/dealer v0.0.0-20240911145828-d734d617bfc8/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI=
git.solsynth.dev/hydrogen/dealer v0.0.0-20240914133539-4a48b980f0b3 h1:ucko8l9rC/XD/Gfk9lcQftyqb9tvHjL5f+aW3ydf1OI= git.solsynth.dev/hydrogen/dealer v0.0.0-20240914133539-4a48b980f0b3 h1:ucko8l9rC/XD/Gfk9lcQftyqb9tvHjL5f+aW3ydf1OI=
git.solsynth.dev/hydrogen/dealer v0.0.0-20240914133539-4a48b980f0b3/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI= git.solsynth.dev/hydrogen/dealer v0.0.0-20240914133539-4a48b980f0b3/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI=
git.solsynth.dev/hydrogen/dealer v0.0.0-20240917083841-b14c0240a75f h1:3jLpcws4/zmNUA60w1RtAtGNjcQd5NZCcbW5HQcUcvw=
git.solsynth.dev/hydrogen/dealer v0.0.0-20240917083841-b14c0240a75f/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= 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-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=

View File

@ -5,13 +5,7 @@ import "git.solsynth.dev/hydrogen/dealer/pkg/hyper"
// Realm profiles basically fetched from Hydrogen.Passport // Realm profiles basically fetched from Hydrogen.Passport
// But cache at here for better usage and database relations // But cache at here for better usage and database relations
type Realm struct { type Realm struct {
hyper.BaseModel hyper.BaseRealm
Alias string `json:"alias"`
Name string `json:"name"`
Description string `json:"description"`
Channels []Channel `json:"channels"` Channels []Channel `json:"channels"`
IsPublic bool `json:"is_public"`
IsCommunity bool `json:"is_community"`
ExternalID uint `json:"external_id"`
} }

View File

@ -216,9 +216,9 @@ func joinChannel(c *fiber.Ctx) error {
} }
if channel.RealmID != nil { if channel.RealmID != nil {
if realm, err := services.GetRealmWithExtID(channel.Realm.ExternalID); err != nil { if realm, err := services.GetRealmWithExtID(channel.Realm.ID); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("invalid channel, related realm was not found: %v", err)) return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("invalid channel, related realm was not found: %v", err))
} else if _, err := services.GetRealmMember(realm.ExternalID, user.ID); err != nil { } else if _, err := services.GetRealmMember(realm.ID, user.ID); err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you are not a part of the realm: %v", err)) return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you are not a part of the realm: %v", err))
} }
} }

View File

@ -134,7 +134,7 @@ func createChannel(c *fiber.Ctx) error {
var realm *models.Realm var realm *models.Realm
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(models.Realm); ok {
if info, err := services.GetRealmMember(val.ExternalID, user.ID); err != nil { if info, err := services.GetRealmMember(val.ID, user.ID); err != nil {
return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can create channel related to it") return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can create channel related to it")
} else if info.GetPowerLevel() < 50 { } else if info.GetPowerLevel() < 50 {
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can create channel related to it") return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can create channel related to it")
@ -190,7 +190,7 @@ func editChannel(c *fiber.Ctx) error {
tx := database.C.Where(&models.Channel{BaseModel: hyper.BaseModel{ID: uint(id)}}) tx := database.C.Where(&models.Channel{BaseModel: hyper.BaseModel{ID: uint(id)}})
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(models.Realm); ok {
if info, err := services.GetRealmMember(val.ExternalID, user.ID); err != nil { if info, err := services.GetRealmMember(val.ID, user.ID); err != nil {
return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can edit channel related to it") return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can edit channel related to it")
} else if info.GetPowerLevel() < 50 { } else if info.GetPowerLevel() < 50 {
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can edit channel related to it") return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can edit channel related to it")
@ -232,7 +232,7 @@ func deleteChannel(c *fiber.Ctx) error {
tx := database.C.Where(&models.Channel{BaseModel: hyper.BaseModel{ID: uint(id)}}) tx := database.C.Where(&models.Channel{BaseModel: hyper.BaseModel{ID: uint(id)}})
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(models.Realm); ok {
if info, err := services.GetRealmMember(val.ExternalID, user.ID); err != nil { if info, err := services.GetRealmMember(val.ID, user.ID); err != nil {
return fmt.Errorf("you must be a part of that realm then can delete channel related to it") return fmt.Errorf("you must be a part of that realm then can delete channel related to it")
} else if info.GetPowerLevel() < 50 { } else if info.GetPowerLevel() < 50 {
return fmt.Errorf("you must be a moderator of that realm then can delete channel related to it") return fmt.Errorf("you must be a moderator of that realm then can delete channel related to it")

View File

@ -34,7 +34,7 @@ func createDirectChannel(c *fiber.Ctx) error {
var realm *models.Realm var realm *models.Realm
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(models.Realm); ok {
if info, err := services.GetRealmMember(val.ExternalID, user.ID); err != nil { if info, err := services.GetRealmMember(val.ID, user.ID); err != nil {
return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can create channel related to it") return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can create channel related to it")
} else if info.GetPowerLevel() < 50 { } else if info.GetPowerLevel() < 50 {
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can create channel related to it") return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can create channel related to it")

View File

@ -2,9 +2,6 @@ package services
import ( import (
"context" "context"
"errors"
"fmt"
"reflect"
"git.solsynth.dev/hydrogen/dealer/pkg/hyper" "git.solsynth.dev/hydrogen/dealer/pkg/hyper"
"git.solsynth.dev/hydrogen/dealer/pkg/proto" "git.solsynth.dev/hydrogen/dealer/pkg/proto"
@ -12,7 +9,7 @@ import (
"git.solsynth.dev/hydrogen/messaging/pkg/internal/gap" "git.solsynth.dev/hydrogen/messaging/pkg/internal/gap"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models" "git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
"github.com/samber/lo" "github.com/samber/lo"
"gorm.io/gorm" "github.com/spf13/viper"
) )
func GetRealmWithExtID(id uint) (models.Realm, error) { func GetRealmWithExtID(id uint) (models.Realm, error) {
@ -27,7 +24,9 @@ func GetRealmWithExtID(id uint) (models.Realm, error) {
if err != nil { if err != nil {
return realm, err return realm, err
} }
return LinkRealm(response) prefix := viper.GetString("database.prefix")
rm, err := hyper.LinkRealm(database.C, prefix+"realms", response)
return models.Realm{BaseRealm: rm}, err
} }
func GetRealmWithAlias(alias string) (models.Realm, error) { func GetRealmWithAlias(alias string) (models.Realm, error) {
@ -42,7 +41,9 @@ func GetRealmWithAlias(alias string) (models.Realm, error) {
if err != nil { if err != nil {
return realm, err return realm, err
} }
return LinkRealm(response) prefix := viper.GetString("database.prefix")
rm, err := hyper.LinkRealm(database.C, prefix+"realms", response)
return models.Realm{BaseRealm: rm}, err
} }
func GetRealmMember(realmId uint, userId uint) (*proto.RealmMemberInfo, error) { func GetRealmMember(realmId uint, userId uint) (*proto.RealmMemberInfo, error) {
@ -51,7 +52,7 @@ func GetRealmMember(realmId uint, userId uint) (*proto.RealmMemberInfo, error) {
return nil, err return nil, err
} }
response, err := proto.NewRealmClient(pc).GetRealmMember(context.Background(), &proto.RealmMemberLookupRequest{ response, err := proto.NewRealmClient(pc).GetRealmMember(context.Background(), &proto.RealmMemberLookupRequest{
RealmId: uint64(realmId), RealmId: lo.ToPtr(uint64(realmId)),
UserId: lo.ToPtr(uint64(userId)), UserId: lo.ToPtr(uint64(userId)),
}) })
if err != nil { if err != nil {
@ -67,7 +68,7 @@ func ListRealmMember(realmId uint) ([]*proto.RealmMemberInfo, error) {
return nil, err return nil, err
} }
response, err := proto.NewRealmClient(pc).ListRealmMember(context.Background(), &proto.RealmMemberLookupRequest{ response, err := proto.NewRealmClient(pc).ListRealmMember(context.Background(), &proto.RealmMemberLookupRequest{
RealmId: uint64(realmId), RealmId: lo.ToPtr(uint64(realmId)),
}) })
if err != nil { if err != nil {
return nil, err return nil, err
@ -75,40 +76,3 @@ func ListRealmMember(realmId uint) ([]*proto.RealmMemberInfo, error) {
return response.Data, nil return response.Data, nil
} }
} }
func LinkRealm(info *proto.RealmInfo) (models.Realm, error) {
var realm models.Realm
if info == nil {
return realm, fmt.Errorf("remote realm info was not found")
}
if err := database.C.Where(&models.Realm{
ExternalID: uint(info.Id),
}).First(&realm).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
realm = models.Realm{
Alias: info.Alias,
Name: info.Name,
Description: info.Description,
IsPublic: info.IsPublic,
IsCommunity: info.IsCommunity,
ExternalID: uint(info.Id),
}
return realm, database.C.Save(&realm).Error
}
return realm, err
}
prev := realm
realm.Alias = info.Alias
realm.Name = info.Name
realm.Description = info.Description
realm.IsPublic = info.IsPublic
realm.IsCommunity = info.IsCommunity
var err error
if !reflect.DeepEqual(prev, realm) {
err = database.C.Save(&realm).Error
}
return realm, err
}