diff --git a/go.mod b/go.mod index 434ca26..1c7bd25 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.22 toolchain go1.22.1 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/gofiber/fiber/v2 v2.52.4 github.com/google/uuid v1.6.0 diff --git a/go.sum b/go.sum index 16ba408..7e7773c 100644 --- a/go.sum +++ b/go.sum @@ -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-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-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/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= diff --git a/pkg/internal/models/realms.go b/pkg/internal/models/realms.go index 940dd36..b5a55e6 100644 --- a/pkg/internal/models/realms.go +++ b/pkg/internal/models/realms.go @@ -5,13 +5,7 @@ import "git.solsynth.dev/hydrogen/dealer/pkg/hyper" // Realm profiles basically fetched from Hydrogen.Passport // But cache at here for better usage and database relations type Realm struct { - hyper.BaseModel + hyper.BaseRealm - Alias string `json:"alias"` - Name string `json:"name"` - Description string `json:"description"` - Channels []Channel `json:"channels"` - IsPublic bool `json:"is_public"` - IsCommunity bool `json:"is_community"` - ExternalID uint `json:"external_id"` + Channels []Channel `json:"channels"` } diff --git a/pkg/internal/server/api/channel_members_api.go b/pkg/internal/server/api/channel_members_api.go index 1cfb6d1..7c02af5 100644 --- a/pkg/internal/server/api/channel_members_api.go +++ b/pkg/internal/server/api/channel_members_api.go @@ -216,9 +216,9 @@ func joinChannel(c *fiber.Ctx) error { } 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)) - } 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)) } } diff --git a/pkg/internal/server/api/channels_api.go b/pkg/internal/server/api/channels_api.go index 0f9aa25..0003a2f 100644 --- a/pkg/internal/server/api/channels_api.go +++ b/pkg/internal/server/api/channels_api.go @@ -134,7 +134,7 @@ func createChannel(c *fiber.Ctx) error { var realm *models.Realm 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") } 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") @@ -190,7 +190,7 @@ func editChannel(c *fiber.Ctx) error { tx := database.C.Where(&models.Channel{BaseModel: hyper.BaseModel{ID: uint(id)}}) 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") } 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") @@ -232,7 +232,7 @@ func deleteChannel(c *fiber.Ctx) error { tx := database.C.Where(&models.Channel{BaseModel: hyper.BaseModel{ID: uint(id)}}) 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") } else if info.GetPowerLevel() < 50 { return fmt.Errorf("you must be a moderator of that realm then can delete channel related to it") diff --git a/pkg/internal/server/api/direct_channels_api.go b/pkg/internal/server/api/direct_channels_api.go index ec06491..a884137 100644 --- a/pkg/internal/server/api/direct_channels_api.go +++ b/pkg/internal/server/api/direct_channels_api.go @@ -34,7 +34,7 @@ func createDirectChannel(c *fiber.Ctx) error { var realm *models.Realm 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") } 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") diff --git a/pkg/internal/services/realms.go b/pkg/internal/services/realms.go index b1b9e53..0c0e559 100644 --- a/pkg/internal/services/realms.go +++ b/pkg/internal/services/realms.go @@ -2,9 +2,6 @@ package services import ( "context" - "errors" - "fmt" - "reflect" "git.solsynth.dev/hydrogen/dealer/pkg/hyper" "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/models" "github.com/samber/lo" - "gorm.io/gorm" + "github.com/spf13/viper" ) func GetRealmWithExtID(id uint) (models.Realm, error) { @@ -27,7 +24,9 @@ func GetRealmWithExtID(id uint) (models.Realm, error) { if err != nil { 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) { @@ -42,7 +41,9 @@ func GetRealmWithAlias(alias string) (models.Realm, error) { if err != nil { 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) { @@ -51,7 +52,7 @@ func GetRealmMember(realmId uint, userId uint) (*proto.RealmMemberInfo, error) { return nil, err } response, err := proto.NewRealmClient(pc).GetRealmMember(context.Background(), &proto.RealmMemberLookupRequest{ - RealmId: uint64(realmId), + RealmId: lo.ToPtr(uint64(realmId)), UserId: lo.ToPtr(uint64(userId)), }) if err != nil { @@ -67,7 +68,7 @@ func ListRealmMember(realmId uint) ([]*proto.RealmMemberInfo, error) { return nil, err } response, err := proto.NewRealmClient(pc).ListRealmMember(context.Background(), &proto.RealmMemberLookupRequest{ - RealmId: uint64(realmId), + RealmId: lo.ToPtr(uint64(realmId)), }) if err != nil { return nil, err @@ -75,40 +76,3 @@ func ListRealmMember(realmId uint) ([]*proto.RealmMemberInfo, error) { 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 -}