diff --git a/pkg/models/channels.go b/pkg/models/channels.go index 7dc7ddb..fa40264 100644 --- a/pkg/models/channels.go +++ b/pkg/models/channels.go @@ -38,6 +38,7 @@ type ChannelMember struct { ChannelID uint `json:"channel_id"` AccountID uint `json:"account_id"` + Nick *string `json:"nick"` Channel Channel `json:"channel"` Account Account `json:"account"` Notify NotifyLevel `json:"notify"` diff --git a/pkg/server/channel_members_api.go b/pkg/server/channel_members_api.go index 8865297..abdd8c7 100644 --- a/pkg/server/channel_members_api.go +++ b/pkg/server/channel_members_api.go @@ -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 { user := c.Locals("principal").(models.Account) alias := c.Params("channel") @@ -118,7 +140,8 @@ func editChannelMembership(c *fiber.Ctx) error { alias := c.Params("channel") var data struct { - NotifyLevel int8 `json:"notify_level"` + Nick string `json:"nick"` + NotifyLevel int8 `json:"notify_level"` } if err := BindAndValidate(c, &data); err != nil { @@ -139,6 +162,11 @@ func editChannelMembership(c *fiber.Ctx) error { } 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 { return fiber.NewError(fiber.StatusBadRequest, err.Error()) diff --git a/pkg/server/messages_api.go b/pkg/server/messages_api.go index 30ad6ba..99150a6 100644 --- a/pkg/server/messages_api.go +++ b/pkg/server/messages_api.go @@ -86,6 +86,8 @@ func newMessage(c *fiber.Ctx) error { channel, member, err = services.GetAvailableChannelWithAlias(alias, user) if err != nil { 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") } } diff --git a/pkg/server/startup.go b/pkg/server/startup.go index 398350a..db022a6 100644 --- a/pkg/server/startup.go +++ b/pkg/server/startup.go @@ -81,6 +81,7 @@ func NewServer() { channels.Get("/:channel/members", listChannelMembers) channels.Put("/:channel/members", authMiddleware, editChannelMembership) + channels.Get("/:channel/members/me", authMiddleware, getMyChannelMembership) channels.Post("/:channel/members", authMiddleware, addChannelMember) channels.Post("/:channel/members/me", authMiddleware, joinChannel) channels.Delete("/:channel/members", authMiddleware, removeChannelMember)