From 198583a7a3bbe904a614a331ce18e41fbd71ccf1 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 26 Apr 2024 20:18:49 +0800 Subject: [PATCH] :boom: Now uses channel alias to perform member actions --- pkg/server/channel_members_api.go | 30 ++++++++++++++++++++---------- pkg/server/startup.go | 10 +++++----- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/pkg/server/channel_members_api.go b/pkg/server/channel_members_api.go index ed976a2..13ffd40 100644 --- a/pkg/server/channel_members_api.go +++ b/pkg/server/channel_members_api.go @@ -8,9 +8,14 @@ import ( ) func listChannelMembers(c *fiber.Ctx) error { - channelId, _ := c.ParamsInt("channelId", 0) + alias := c.Params("channel") - if members, err := services.ListChannelMember(uint(channelId)); err != nil { + channel, err := services.GetChannelWithAlias(alias) + if err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } + + if members, err := services.ListChannelMember(channel.ID); err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } else { return c.JSON(members) @@ -19,7 +24,7 @@ func listChannelMembers(c *fiber.Ctx) error { func inviteChannel(c *fiber.Ctx) error { user := c.Locals("principal").(models.Account) - channelId, _ := c.ParamsInt("channelId", 0) + alias := c.Params("channel") var data struct { AccountName string `json:"account_name" validate:"required"` @@ -31,7 +36,7 @@ func inviteChannel(c *fiber.Ctx) error { var channel models.Channel if err := database.C.Where(&models.Channel{ - BaseModel: models.BaseModel{ID: uint(channelId)}, + Alias: alias, AccountID: user.ID, }).First(&channel).Error; err != nil { return fiber.NewError(fiber.StatusNotFound, err.Error()) @@ -53,7 +58,7 @@ func inviteChannel(c *fiber.Ctx) error { func kickChannel(c *fiber.Ctx) error { user := c.Locals("principal").(models.Account) - channelId, _ := c.ParamsInt("channelId", 0) + alias := c.Params("channel") var data struct { AccountName string `json:"account_name" validate:"required"` @@ -65,7 +70,7 @@ func kickChannel(c *fiber.Ctx) error { var channel models.Channel if err := database.C.Where(&models.Channel{ - BaseModel: models.BaseModel{ID: uint(channelId)}, + Alias: alias, AccountID: user.ID, }).First(&channel).Error; err != nil { return fiber.NewError(fiber.StatusNotFound, err.Error()) @@ -87,7 +92,7 @@ func kickChannel(c *fiber.Ctx) error { func editChannelMembership(c *fiber.Ctx) error { user := c.Locals("principal").(models.Account) - channelId, _ := c.ParamsInt("channelId", 0) + alias := c.Params("channel") var data struct { NotifyLevel int8 `json:"notify_level"` @@ -97,9 +102,14 @@ func editChannelMembership(c *fiber.Ctx) error { return err } + channel, err := services.GetChannelWithAlias(alias) + if err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } + var membership models.ChannelMember if err := database.C.Where(&models.ChannelMember{ - ChannelID: uint(channelId), + ChannelID: channel.ID, AccountID: user.ID, }).First(&membership).Error; err != nil { return fiber.NewError(fiber.StatusNotFound, err.Error()) @@ -116,7 +126,7 @@ func editChannelMembership(c *fiber.Ctx) error { func leaveChannel(c *fiber.Ctx) error { user := c.Locals("principal").(models.Account) - channelId, _ := c.ParamsInt("channelId", 0) + alias := c.Params("channel") var data struct { AccountName string `json:"account_name" validate:"required"` @@ -128,7 +138,7 @@ func leaveChannel(c *fiber.Ctx) error { var channel models.Channel if err := database.C.Where(&models.Channel{ - BaseModel: models.BaseModel{ID: uint(channelId)}, + Alias: alias, }).First(&channel).Error; err != nil { return fiber.NewError(fiber.StatusNotFound, err.Error()) } else if user.ID == channel.AccountID { diff --git a/pkg/server/startup.go b/pkg/server/startup.go index f7b1274..de7f6ce 100644 --- a/pkg/server/startup.go +++ b/pkg/server/startup.go @@ -86,11 +86,11 @@ func NewServer() { channels.Put("/:channelId", authMiddleware, editChannel) channels.Delete("/:channelId", authMiddleware, deleteChannel) - channels.Get("/:channelId/members", listChannelMembers) - channels.Put("/:channelId/members", authMiddleware, editChannelMembership) - channels.Post("/:channelId/invite", authMiddleware, inviteChannel) - channels.Post("/:channelId/kick", authMiddleware, kickChannel) - channels.Post("/:channelId/leave", authMiddleware, leaveChannel) + channels.Get("/:channel/members", listChannelMembers) + channels.Put("/:channel/members", authMiddleware, editChannelMembership) + channels.Post("/:channel/invite", authMiddleware, inviteChannel) + channels.Post("/:channel/kick", authMiddleware, kickChannel) + channels.Post("/:channel/leave", authMiddleware, leaveChannel) channels.Get("/:channel/messages", listMessage) channels.Post("/:channel/messages", authMiddleware, newTextMessage)