From a268b7958ca14b25ace5adb982b9870a90ddbb5a Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 16 Feb 2025 20:27:04 +0800 Subject: [PATCH] :sparkles: Able to transfer channel between realms --- pkg/internal/http/api/channels_api.go | 39 ++++++++++++++++++++++----- pkg/internal/services/channels.go | 8 +----- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/pkg/internal/http/api/channels_api.go b/pkg/internal/http/api/channels_api.go index 44e3143..ca906ee 100644 --- a/pkg/internal/http/api/channels_api.go +++ b/pkg/internal/http/api/channels_api.go @@ -2,6 +2,7 @@ package api import ( "fmt" + "git.solsynth.dev/hypernet/messaging/pkg/internal/gap" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "git.solsynth.dev/hypernet/passport/pkg/authkit" @@ -203,11 +204,12 @@ func editChannel(c *fiber.Ctx) error { id, _ := c.ParamsInt("channelId", 0) var data struct { - Alias string `json:"alias" validate:"required,min=4,max=32"` - Name string `json:"name" validate:"required"` - Description string `json:"description"` - IsPublic bool `json:"is_public"` - IsCommunity bool `json:"is_community"` + Alias string `json:"alias" validate:"required,min=4,max=32"` + Name string `json:"name" validate:"required"` + Description string `json:"description"` + IsPublic bool `json:"is_public"` + IsCommunity bool `json:"is_community"` + NewBelongsRealm *string `json:"new_belongs_realm"` } if err := exts.BindAndValidate(c, &data); err != nil { @@ -241,7 +243,32 @@ func editChannel(c *fiber.Ctx) error { } } - channel, err := services.EditChannel(channel, data.Alias, data.Name, data.Description, data.IsPublic, data.IsCommunity) + if data.NewBelongsRealm != nil { + if *data.NewBelongsRealm == "global" { + channel.RealmID = nil + } else { + realm, err := authkit.GetRealmByAlias(gap.Nx, *data.NewBelongsRealm) + if err != nil { + return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("requested channel with realm, but realm was not found: %v", err)) + } else { + if info, err := authkit.GetRealmMember(gap.Nx, realm.ID, user.ID); err != nil { + return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can transfer channel related to it") + } else if info.PowerLevel < 50 { + return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can transfer channel related to it") + } else { + channel.RealmID = &realm.ID + } + } + } + } + + channel.Alias = data.Alias + channel.Name = data.Name + channel.Description = data.Description + channel.IsPublic = data.IsPublic + channel.IsCommunity = data.IsCommunity + + channel, err := services.EditChannel(channel) if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } diff --git a/pkg/internal/services/channels.go b/pkg/internal/services/channels.go index 9f14ed1..8ee7b46 100644 --- a/pkg/internal/services/channels.go +++ b/pkg/internal/services/channels.go @@ -274,13 +274,7 @@ func NewChannel(channel models.Channel) (models.Channel, error) { return channel, err } -func EditChannel(channel models.Channel, alias, name, description string, isPublic, isCommunity bool) (models.Channel, error) { - channel.Alias = alias - channel.Name = name - channel.Description = description - channel.IsPublic = isPublic - channel.IsCommunity = isCommunity - +func EditChannel(channel models.Channel) (models.Channel, error) { err := database.C.Save(&channel).Error if err == nil {