From 519d570041fb0a1f34b02669d3b398afb188e77b Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 29 Nov 2024 23:45:35 +0800 Subject: [PATCH] :recycle: Split edit channel profile and channel notify level API --- pkg/internal/http/api/channel_members_api.go | 51 ++++++++++++++++++-- pkg/internal/http/api/index.go | 5 +- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/pkg/internal/http/api/channel_members_api.go b/pkg/internal/http/api/channel_members_api.go index 0668fb6..719b8e7 100644 --- a/pkg/internal/http/api/channel_members_api.go +++ b/pkg/internal/http/api/channel_members_api.go @@ -36,7 +36,7 @@ func listChannelMembers(c *fiber.Ctx) error { } } -func getMyChannelMembership(c *fiber.Ctx) error { +func getChannelProfileOfMyself(c *fiber.Ctx) error { alias := c.Params("channel") if err := sec.EnsureAuthenticated(c); err != nil { return err @@ -146,7 +146,7 @@ func removeChannelMember(c *fiber.Ctx) error { } } -func editMyChannelMembership(c *fiber.Ctx) error { +func editChannelProfileOfMyself(c *fiber.Ctx) error { if err := sec.EnsureAuthenticated(c); err != nil { return err } @@ -154,8 +154,7 @@ func editMyChannelMembership(c *fiber.Ctx) error { alias := c.Params("channel") var data struct { - Nick string `json:"nick"` - NotifyLevel int8 `json:"notify_level"` + Nick string `json:"nick"` } if err := exts.BindAndValidate(c, &data); err != nil { @@ -182,7 +181,6 @@ func editMyChannelMembership(c *fiber.Ctx) error { } membership.Name = user.Name - membership.Notify = data.NotifyLevel if len(data.Nick) > 0 { membership.Nick = data.Nick } else { @@ -196,6 +194,49 @@ func editMyChannelMembership(c *fiber.Ctx) error { } } +func editChannelNotifyLevelOfMyself(c *fiber.Ctx) error { + if err := sec.EnsureAuthenticated(c); err != nil { + return err + } + user := c.Locals("user").(authm.Account) + alias := c.Params("channel") + + var data struct { + NotifyLevel int8 `json:"notify_level"` + } + + if err := exts.BindAndValidate(c, &data); err != nil { + return err + } + + var err error + var channel models.Channel + if val, ok := c.Locals("realm").(authm.Realm); ok { + channel, err = services.GetChannelWithAlias(alias, val.ID) + } else { + 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: channel.ID, + AccountID: user.ID, + }).First(&membership).Error; err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } + + membership.Notify = data.NotifyLevel + + if membership, err := services.EditChannelMember(membership); err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } else { + return c.JSON(membership) + } +} + func joinChannel(c *fiber.Ctx) error { if err := sec.EnsureAuthenticated(c); err != nil { return err diff --git a/pkg/internal/http/api/index.go b/pkg/internal/http/api/index.go index 26abe45..a242fc3 100644 --- a/pkg/internal/http/api/index.go +++ b/pkg/internal/http/api/index.go @@ -21,8 +21,9 @@ func MapAPIs(app *fiber.App, baseURL string) { channels.Delete("/:channelId", deleteChannel) channels.Get("/:channel/members", listChannelMembers) - channels.Get("/:channel/members/me", getMyChannelMembership) - channels.Put("/:channel/members/me", editMyChannelMembership) + channels.Get("/:channel/members/me", getChannelProfileOfMyself) + channels.Put("/:channel/members/me", editChannelProfileOfMyself) + channels.Put("/:channel/members/me/notify", editChannelNotifyLevelOfMyself) channels.Post("/:channel/members", addChannelMember) channels.Post("/:channel/members/me", joinChannel) channels.Delete("/:channel/members", removeChannelMember)