💥 Move remove member api arguments from payload to query string

This commit is contained in:
LittleSheep 2024-12-01 01:23:04 +08:00
parent e82f100b67
commit aac8a3eb54
3 changed files with 22 additions and 26 deletions

View File

@ -3,6 +3,7 @@ package api
import ( import (
"fmt" "fmt"
"git.solsynth.dev/hypernet/messaging/pkg/internal/gap" "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/nexus/pkg/nex/sec"
"git.solsynth.dev/hypernet/passport/pkg/authkit" "git.solsynth.dev/hypernet/passport/pkg/authkit"
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models" 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) user := c.Locals("user").(authm.Account)
alias := c.Params("channel") alias := c.Params("channel")
memberId, _ := c.ParamsInt("memberId", 0)
var data struct {
Target string `json:"target" validate:"required"`
}
if err := exts.BindAndValidate(c, &data); err != nil {
return err
}
var channel models.Channel var channel models.Channel
if err := database.C.Where(&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") return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of a channel to remove member into it")
} }
var account authm.Account var member models.ChannelMember
if err := database.C.Where("name = ?", data.Target).First(&account).Error; err != nil { 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()) 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()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else { } else {
return c.SendStatus(fiber.StatusOK) 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") 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()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else { } else {
return c.SendStatus(fiber.StatusOK) return c.SendStatus(fiber.StatusOK)

View File

@ -26,7 +26,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
channels.Put("/:channel/members/me/notify", editChannelNotifyLevelOfMyself) channels.Put("/:channel/members/me/notify", editChannelNotifyLevelOfMyself)
channels.Post("/:channel/members", addChannelMember) channels.Post("/:channel/members", addChannelMember)
channels.Post("/:channel/members/me", joinChannel) channels.Post("/:channel/members/me", joinChannel)
channels.Delete("/:channel/members", removeChannelMember) channels.Delete("/:channel/members/:memberId", removeChannelMember)
channels.Delete("/:channel/members/me", leaveChannel) channels.Delete("/:channel/members/me", leaveChannel)
channels.Get("/:channel/events", listEvent) channels.Get("/:channel/events", listEvent)

View File

@ -110,28 +110,19 @@ func EditChannelMember(membership models.ChannelMember) (models.ChannelMember, e
return membership, nil return membership, nil
} }
func RemoveChannelMember(user authm.Account, target models.Channel) error { func RemoveChannelMember(member models.ChannelMember, 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
}
if err := database.C.Delete(&member).Error; err == nil { if err := database.C.Delete(&member).Error; err == nil {
database.C.Where("sender_id = ?").Delete(&models.Event{}) database.C.Where("sender_id = ?").Delete(&models.Event{})
cacheManager := cache.New[any](localCache.S) cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager) marshal := marshaler.New(cacheManager)
contx := context.Background() ctx := context.Background()
_ = marshal.Invalidate( _ = marshal.Invalidate(
contx, ctx,
store.WithInvalidateTags([]string{ store.WithInvalidateTags([]string{
fmt.Sprintf("channel#%d", target.ID), fmt.Sprintf("channel#%d", target.ID),
fmt.Sprintf("user#%d", user.ID), fmt.Sprintf("user#%d", target.AccountID),
}), }),
) )