♻️ Split edit channel profile and channel notify level API

This commit is contained in:
LittleSheep 2024-11-29 23:45:35 +08:00
parent 275fe80286
commit 519d570041
2 changed files with 49 additions and 7 deletions

View File

@ -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
}
@ -155,7 +155,6 @@ func editMyChannelMembership(c *fiber.Ctx) error {
var data struct {
Nick string `json:"nick"`
NotifyLevel int8 `json:"notify_level"`
}
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

View File

@ -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)