Get myself channel identity, and edit

This commit is contained in:
LittleSheep 2024-06-08 21:46:06 +08:00
parent c366b1cbe3
commit c9d3de0022
4 changed files with 33 additions and 1 deletions

View File

@ -38,6 +38,7 @@ type ChannelMember struct {
ChannelID uint `json:"channel_id"` ChannelID uint `json:"channel_id"`
AccountID uint `json:"account_id"` AccountID uint `json:"account_id"`
Nick *string `json:"nick"`
Channel Channel `json:"channel"` Channel Channel `json:"channel"`
Account Account `json:"account"` Account Account `json:"account"`
Notify NotifyLevel `json:"notify"` Notify NotifyLevel `json:"notify"`

View File

@ -30,6 +30,28 @@ func listChannelMembers(c *fiber.Ctx) error {
} }
} }
func getMyChannelMembership(c *fiber.Ctx) error {
alias := c.Params("channel")
user := c.Locals("principal").(models.Account)
var err error
var channel models.Channel
if val, ok := c.Locals("realm").(models.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())
}
if member, err := services.GetChannelMember(user, channel.ID); err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
} else {
return c.JSON(member)
}
}
func addChannelMember(c *fiber.Ctx) error { func addChannelMember(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("principal").(models.Account)
alias := c.Params("channel") alias := c.Params("channel")
@ -118,6 +140,7 @@ func editChannelMembership(c *fiber.Ctx) error {
alias := c.Params("channel") alias := c.Params("channel")
var data struct { var data struct {
Nick string `json:"nick"`
NotifyLevel int8 `json:"notify_level"` NotifyLevel int8 `json:"notify_level"`
} }
@ -139,6 +162,11 @@ func editChannelMembership(c *fiber.Ctx) error {
} }
membership.Notify = data.NotifyLevel membership.Notify = data.NotifyLevel
if len(data.Nick) > 0 {
membership.Nick = &data.Nick
} else {
membership.Nick = nil
}
if membership, err := services.EditChannelMember(membership); err != nil { if membership, err := services.EditChannelMember(membership); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) return fiber.NewError(fiber.StatusBadRequest, err.Error())

View File

@ -86,6 +86,8 @@ func newMessage(c *fiber.Ctx) error {
channel, member, err = services.GetAvailableChannelWithAlias(alias, user) channel, member, err = services.GetAvailableChannelWithAlias(alias, user)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error()) return fiber.NewError(fiber.StatusNotFound, err.Error())
} else if member.PowerLevel < 0 {
return fiber.NewError(fiber.StatusForbidden, "you have not enough permission to send message")
} }
} }

View File

@ -81,6 +81,7 @@ func NewServer() {
channels.Get("/:channel/members", listChannelMembers) channels.Get("/:channel/members", listChannelMembers)
channels.Put("/:channel/members", authMiddleware, editChannelMembership) channels.Put("/:channel/members", authMiddleware, editChannelMembership)
channels.Get("/:channel/members/me", authMiddleware, getMyChannelMembership)
channels.Post("/:channel/members", authMiddleware, addChannelMember) channels.Post("/:channel/members", authMiddleware, addChannelMember)
channels.Post("/:channel/members/me", authMiddleware, joinChannel) channels.Post("/:channel/members/me", authMiddleware, joinChannel)
channels.Delete("/:channel/members", authMiddleware, removeChannelMember) channels.Delete("/:channel/members", authMiddleware, removeChannelMember)