From aac8a3eb54e3a19e375536783726a21501aa93e8 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 1 Dec 2024 01:23:04 +0800 Subject: [PATCH] :boom: Move remove member api arguments from payload to query string --- pkg/internal/http/api/channel_members_api.go | 29 ++++++++++++-------- pkg/internal/http/api/index.go | 2 +- pkg/internal/services/channel_members.go | 17 +++--------- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/pkg/internal/http/api/channel_members_api.go b/pkg/internal/http/api/channel_members_api.go index 147b59c..03e1697 100644 --- a/pkg/internal/http/api/channel_members_api.go +++ b/pkg/internal/http/api/channel_members_api.go @@ -3,6 +3,7 @@ package api import ( "fmt" "git.solsynth.dev/hypernet/messaging/pkg/internal/gap" + "git.solsynth.dev/hypernet/nexus/pkg/nex/cruda" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "git.solsynth.dev/hypernet/passport/pkg/authkit" authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models" @@ -119,14 +120,7 @@ func removeChannelMember(c *fiber.Ctx) error { } user := c.Locals("user").(authm.Account) alias := c.Params("channel") - - var data struct { - Target string `json:"target" validate:"required"` - } - - if err := exts.BindAndValidate(c, &data); err != nil { - return err - } + memberId, _ := c.ParamsInt("memberId", 0) var channel models.Channel if err := database.C.Where(&models.Channel{ @@ -144,12 +138,15 @@ func removeChannelMember(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of a channel to remove member into it") } - var account authm.Account - if err := database.C.Where("name = ?", data.Target).First(&account).Error; err != nil { + var member models.ChannelMember + if err := database.C.Where(&models.ChannelMember{ + BaseModel: cruda.BaseModel{ID: uint(memberId)}, + ChannelID: channel.ID, + }).First(&member).Error; err != nil { return fiber.NewError(fiber.StatusNotFound, err.Error()) } - if err := services.RemoveChannelMember(account, channel); err != nil { + if err := services.RemoveChannelMember(member, channel); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { return c.SendStatus(fiber.StatusOK) @@ -294,7 +291,15 @@ func leaveChannel(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, "you cannot leave your own channel") } - if err := services.RemoveChannelMember(user, channel); err != nil { + var member models.ChannelMember + if err := database.C.Where(&models.ChannelMember{ + ChannelID: channel.ID, + AccountID: user.ID, + }).First(&member).Error; err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } + + if err := services.RemoveChannelMember(member, channel); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { return c.SendStatus(fiber.StatusOK) diff --git a/pkg/internal/http/api/index.go b/pkg/internal/http/api/index.go index a242fc3..e75fd0e 100644 --- a/pkg/internal/http/api/index.go +++ b/pkg/internal/http/api/index.go @@ -26,7 +26,7 @@ func MapAPIs(app *fiber.App, baseURL string) { channels.Put("/:channel/members/me/notify", editChannelNotifyLevelOfMyself) channels.Post("/:channel/members", addChannelMember) channels.Post("/:channel/members/me", joinChannel) - channels.Delete("/:channel/members", removeChannelMember) + channels.Delete("/:channel/members/:memberId", removeChannelMember) channels.Delete("/:channel/members/me", leaveChannel) channels.Get("/:channel/events", listEvent) diff --git a/pkg/internal/services/channel_members.go b/pkg/internal/services/channel_members.go index 11ebcd8..d7215a6 100644 --- a/pkg/internal/services/channel_members.go +++ b/pkg/internal/services/channel_members.go @@ -110,28 +110,19 @@ func EditChannelMember(membership models.ChannelMember) (models.ChannelMember, e return membership, nil } -func RemoveChannelMember(user authm.Account, target models.Channel) error { - var member models.ChannelMember - - if err := database.C.Where(&models.ChannelMember{ - ChannelID: target.ID, - AccountID: user.ID, - }).First(&member).Error; err != nil { - return err - } - +func RemoveChannelMember(member models.ChannelMember, target models.Channel) error { if err := database.C.Delete(&member).Error; err == nil { database.C.Where("sender_id = ?").Delete(&models.Event{}) cacheManager := cache.New[any](localCache.S) marshal := marshaler.New(cacheManager) - contx := context.Background() + ctx := context.Background() _ = marshal.Invalidate( - contx, + ctx, store.WithInvalidateTags([]string{ fmt.Sprintf("channel#%d", target.ID), - fmt.Sprintf("user#%d", user.ID), + fmt.Sprintf("user#%d", target.AccountID), }), )