Messaging/pkg/internal/server/api/channel_members_api.go

253 lines
7.4 KiB
Go
Raw Normal View History

package api
2024-03-26 15:05:13 +00:00
import (
2024-05-04 16:39:59 +00:00
"fmt"
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
2024-06-22 10:29:41 +00:00
"git.solsynth.dev/hydrogen/messaging/pkg/internal/gap"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/server/exts"
2024-05-26 15:01:20 +00:00
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/services"
2024-03-26 15:05:13 +00:00
"github.com/gofiber/fiber/v2"
)
func listChannelMembers(c *fiber.Ctx) error {
alias := c.Params("channel")
2024-03-26 15:05:13 +00:00
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 members, err := services.ListChannelMember(channel.ID); err != nil {
2024-03-26 15:05:13 +00:00
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
return c.JSON(members)
}
}
func getMyChannelMembership(c *fiber.Ctx) error {
alias := c.Params("channel")
2024-06-22 10:29:41 +00:00
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
2024-06-22 12:15:01 +00:00
user := c.Locals("user").(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)
}
}
2024-05-04 16:39:59 +00:00
func addChannelMember(c *fiber.Ctx) error {
2024-06-22 10:29:41 +00:00
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
2024-06-22 12:15:01 +00:00
user := c.Locals("user").(models.Account)
alias := c.Params("channel")
2024-03-26 15:05:13 +00:00
var data struct {
2024-05-05 15:43:27 +00:00
Target string `json:"target" validate:"required"`
2024-03-26 15:05:13 +00:00
}
if err := exts.BindAndValidate(c, &data); err != nil {
2024-03-26 15:05:13 +00:00
return err
}
var channel models.Channel
if err := database.C.Where(&models.Channel{
2024-05-26 15:01:20 +00:00
Alias: alias,
2024-03-26 15:05:13 +00:00
}).First(&channel).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
} else if channel.Type == models.ChannelTypeDirect {
return fiber.NewError(fiber.StatusBadRequest, "direct message member changes was not allowed")
}
if member, err := services.GetChannelMember(user, channel.ID); err != nil {
2024-05-26 15:01:20 +00:00
return fiber.NewError(fiber.StatusForbidden, err.Error())
} else if member.PowerLevel < 50 {
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of a channel to add member into it")
2024-03-26 15:05:13 +00:00
}
var account models.Account
if err := database.C.Where(&hyper.BaseUser{
2024-05-05 15:43:27 +00:00
Name: data.Target,
2024-03-26 15:05:13 +00:00
}).First(&account).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
2024-05-04 16:39:59 +00:00
if err := services.AddChannelMemberWithCheck(account, channel); err != nil {
2024-03-26 15:05:13 +00:00
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.SendStatus(fiber.StatusOK)
}
}
2024-05-04 16:39:59 +00:00
func removeChannelMember(c *fiber.Ctx) error {
2024-06-22 10:29:41 +00:00
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
2024-06-22 12:15:01 +00:00
user := c.Locals("user").(models.Account)
alias := c.Params("channel")
2024-03-26 15:05:13 +00:00
var data struct {
2024-05-05 15:43:27 +00:00
Target string `json:"target" validate:"required"`
2024-03-26 15:05:13 +00:00
}
if err := exts.BindAndValidate(c, &data); err != nil {
2024-03-26 15:05:13 +00:00
return err
}
var channel models.Channel
if err := database.C.Where(&models.Channel{
Alias: alias,
2024-03-26 15:05:13 +00:00
AccountID: user.ID,
}).First(&channel).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
} else if channel.Type == models.ChannelTypeDirect {
return fiber.NewError(fiber.StatusBadRequest, "direct message member changes was not allowed")
}
if member, err := services.GetChannelMember(user, channel.ID); err != nil {
2024-05-26 15:01:20 +00:00
return fiber.NewError(fiber.StatusForbidden, err.Error())
} else if member.PowerLevel < 50 {
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of a channel to remove member into it")
2024-03-26 15:05:13 +00:00
}
var account models.Account
if err := database.C.Where(&hyper.BaseUser{
2024-05-05 15:43:27 +00:00
Name: data.Target,
2024-03-26 15:05:13 +00:00
}).First(&account).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
2024-03-30 09:10:36 +00:00
if err := services.RemoveChannelMember(account, channel); err != nil {
2024-03-26 15:05:13 +00:00
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.SendStatus(fiber.StatusOK)
}
}
2024-04-06 06:36:36 +00:00
2024-06-08 15:59:25 +00:00
func editMyChannelMembership(c *fiber.Ctx) error {
2024-06-22 10:29:41 +00:00
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
2024-06-22 12:15:01 +00:00
user := c.Locals("user").(models.Account)
alias := c.Params("channel")
2024-04-06 08:08:33 +00:00
var data struct {
Nick string `json:"nick"`
NotifyLevel int8 `json:"notify_level"`
2024-04-06 08:08:33 +00:00
}
if err := exts.BindAndValidate(c, &data); err != nil {
2024-04-06 08:08:33 +00:00
return err
}
2024-06-08 15:59:25 +00:00
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())
}
2024-04-06 08:08:33 +00:00
var membership models.ChannelMember
if err := database.C.Where(&models.ChannelMember{
ChannelID: channel.ID,
2024-04-06 08:08:33 +00:00
AccountID: user.ID,
}).First(&membership).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
membership.Notify = data.NotifyLevel
if len(data.Nick) > 0 {
membership.Nick = &data.Nick
} else {
membership.Nick = nil
}
2024-04-06 08:08:33 +00:00
if membership, err := services.EditChannelMember(membership); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.JSON(membership)
}
}
2024-05-04 16:39:59 +00:00
func joinChannel(c *fiber.Ctx) error {
2024-06-22 10:29:41 +00:00
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
2024-06-22 12:15:01 +00:00
user := c.Locals("user").(models.Account)
2024-05-04 16:39:59 +00:00
alias := c.Params("channel")
var channel models.Channel
if err := database.C.Where(&models.Channel{
Alias: alias,
}).Preload("Realm").First(&channel).Error; err != nil {
2024-05-04 16:39:59 +00:00
return fiber.NewError(fiber.StatusNotFound, err.Error())
} else if _, _, err := services.GetAvailableChannel(channel.ID, user); err == nil {
return fiber.NewError(fiber.StatusBadRequest, "you already joined the channel")
} else if channel.RealmID == nil {
return fiber.NewError(fiber.StatusBadRequest, "you was impossible to join a channel without related realm")
}
if realm, err := services.GetRealmWithExtID(channel.Realm.ExternalID); err != nil {
2024-05-04 16:39:59 +00:00
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("invalid channel, related realm was not found: %v", err))
} else if _, err := services.GetRealmMember(realm.ExternalID, user.ID); err != nil {
2024-05-04 16:39:59 +00:00
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you are not a part of the realm: %v", err))
}
if err := services.AddChannelMember(user, channel); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.SendStatus(fiber.StatusOK)
}
}
2024-04-06 06:36:36 +00:00
func leaveChannel(c *fiber.Ctx) error {
2024-06-22 10:29:41 +00:00
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
2024-06-22 12:15:01 +00:00
user := c.Locals("user").(models.Account)
alias := c.Params("channel")
2024-04-06 06:36:36 +00:00
var channel models.Channel
if err := database.C.Where(&models.Channel{
Alias: alias,
2024-04-06 06:36:36 +00:00
}).First(&channel).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
} else if user.ID == channel.AccountID {
return fiber.NewError(fiber.StatusBadRequest, "you cannot leave your own channel")
}
if err := services.RemoveChannelMember(user, channel); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.SendStatus(fiber.StatusOK)
}
}