💥 Now uses channel alias to perform member actions
This commit is contained in:
parent
b4f24f0ae0
commit
198583a7a3
@ -8,9 +8,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func listChannelMembers(c *fiber.Ctx) error {
|
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())
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||||
} else {
|
} else {
|
||||||
return c.JSON(members)
|
return c.JSON(members)
|
||||||
@ -19,7 +24,7 @@ func listChannelMembers(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
func inviteChannel(c *fiber.Ctx) error {
|
func inviteChannel(c *fiber.Ctx) error {
|
||||||
user := c.Locals("principal").(models.Account)
|
user := c.Locals("principal").(models.Account)
|
||||||
channelId, _ := c.ParamsInt("channelId", 0)
|
alias := c.Params("channel")
|
||||||
|
|
||||||
var data struct {
|
var data struct {
|
||||||
AccountName string `json:"account_name" validate:"required"`
|
AccountName string `json:"account_name" validate:"required"`
|
||||||
@ -31,7 +36,7 @@ func inviteChannel(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
var channel models.Channel
|
var channel models.Channel
|
||||||
if err := database.C.Where(&models.Channel{
|
if err := database.C.Where(&models.Channel{
|
||||||
BaseModel: models.BaseModel{ID: uint(channelId)},
|
Alias: alias,
|
||||||
AccountID: user.ID,
|
AccountID: user.ID,
|
||||||
}).First(&channel).Error; err != nil {
|
}).First(&channel).Error; err != nil {
|
||||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||||
@ -53,7 +58,7 @@ func inviteChannel(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
func kickChannel(c *fiber.Ctx) error {
|
func kickChannel(c *fiber.Ctx) error {
|
||||||
user := c.Locals("principal").(models.Account)
|
user := c.Locals("principal").(models.Account)
|
||||||
channelId, _ := c.ParamsInt("channelId", 0)
|
alias := c.Params("channel")
|
||||||
|
|
||||||
var data struct {
|
var data struct {
|
||||||
AccountName string `json:"account_name" validate:"required"`
|
AccountName string `json:"account_name" validate:"required"`
|
||||||
@ -65,7 +70,7 @@ func kickChannel(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
var channel models.Channel
|
var channel models.Channel
|
||||||
if err := database.C.Where(&models.Channel{
|
if err := database.C.Where(&models.Channel{
|
||||||
BaseModel: models.BaseModel{ID: uint(channelId)},
|
Alias: alias,
|
||||||
AccountID: user.ID,
|
AccountID: user.ID,
|
||||||
}).First(&channel).Error; err != nil {
|
}).First(&channel).Error; err != nil {
|
||||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||||
@ -87,7 +92,7 @@ func kickChannel(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
func editChannelMembership(c *fiber.Ctx) error {
|
func editChannelMembership(c *fiber.Ctx) error {
|
||||||
user := c.Locals("principal").(models.Account)
|
user := c.Locals("principal").(models.Account)
|
||||||
channelId, _ := c.ParamsInt("channelId", 0)
|
alias := c.Params("channel")
|
||||||
|
|
||||||
var data struct {
|
var data struct {
|
||||||
NotifyLevel int8 `json:"notify_level"`
|
NotifyLevel int8 `json:"notify_level"`
|
||||||
@ -97,9 +102,14 @@ func editChannelMembership(c *fiber.Ctx) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
channel, err := services.GetChannelWithAlias(alias)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
var membership models.ChannelMember
|
var membership models.ChannelMember
|
||||||
if err := database.C.Where(&models.ChannelMember{
|
if err := database.C.Where(&models.ChannelMember{
|
||||||
ChannelID: uint(channelId),
|
ChannelID: channel.ID,
|
||||||
AccountID: user.ID,
|
AccountID: user.ID,
|
||||||
}).First(&membership).Error; err != nil {
|
}).First(&membership).Error; err != nil {
|
||||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||||
@ -116,7 +126,7 @@ func editChannelMembership(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
func leaveChannel(c *fiber.Ctx) error {
|
func leaveChannel(c *fiber.Ctx) error {
|
||||||
user := c.Locals("principal").(models.Account)
|
user := c.Locals("principal").(models.Account)
|
||||||
channelId, _ := c.ParamsInt("channelId", 0)
|
alias := c.Params("channel")
|
||||||
|
|
||||||
var data struct {
|
var data struct {
|
||||||
AccountName string `json:"account_name" validate:"required"`
|
AccountName string `json:"account_name" validate:"required"`
|
||||||
@ -128,7 +138,7 @@ func leaveChannel(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
var channel models.Channel
|
var channel models.Channel
|
||||||
if err := database.C.Where(&models.Channel{
|
if err := database.C.Where(&models.Channel{
|
||||||
BaseModel: models.BaseModel{ID: uint(channelId)},
|
Alias: alias,
|
||||||
}).First(&channel).Error; err != nil {
|
}).First(&channel).Error; err != nil {
|
||||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||||
} else if user.ID == channel.AccountID {
|
} else if user.ID == channel.AccountID {
|
||||||
|
@ -86,11 +86,11 @@ func NewServer() {
|
|||||||
channels.Put("/:channelId", authMiddleware, editChannel)
|
channels.Put("/:channelId", authMiddleware, editChannel)
|
||||||
channels.Delete("/:channelId", authMiddleware, deleteChannel)
|
channels.Delete("/:channelId", authMiddleware, deleteChannel)
|
||||||
|
|
||||||
channels.Get("/:channelId/members", listChannelMembers)
|
channels.Get("/:channel/members", listChannelMembers)
|
||||||
channels.Put("/:channelId/members", authMiddleware, editChannelMembership)
|
channels.Put("/:channel/members", authMiddleware, editChannelMembership)
|
||||||
channels.Post("/:channelId/invite", authMiddleware, inviteChannel)
|
channels.Post("/:channel/invite", authMiddleware, inviteChannel)
|
||||||
channels.Post("/:channelId/kick", authMiddleware, kickChannel)
|
channels.Post("/:channel/kick", authMiddleware, kickChannel)
|
||||||
channels.Post("/:channelId/leave", authMiddleware, leaveChannel)
|
channels.Post("/:channel/leave", authMiddleware, leaveChannel)
|
||||||
|
|
||||||
channels.Get("/:channel/messages", listMessage)
|
channels.Get("/:channel/messages", listMessage)
|
||||||
channels.Post("/:channel/messages", authMiddleware, newTextMessage)
|
channels.Post("/:channel/messages", authMiddleware, newTextMessage)
|
||||||
|
Loading…
Reference in New Issue
Block a user