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

257 lines
7.5 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"
2024-11-02 05:23:27 +00:00
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
2024-11-02 05:24:37 +00:00
"git.solsynth.dev/hypernet/messaging/pkg/internal/http/exts"
2024-05-26 15:01:20 +00:00
2024-11-02 05:24:37 +00:00
"git.solsynth.dev/hypernet/messaging/pkg/internal/database"
"git.solsynth.dev/hypernet/messaging/pkg/internal/models"
"git.solsynth.dev/hypernet/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-11-02 05:23:27 +00:00
if err := sec.EnsureAuthenticated(c); err != nil {
2024-06-22 10:29:41 +00:00
return err
}
2024-11-02 05:23:27 +00:00
user := c.Locals("user").(authm.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-11-02 05:23:27 +00:00
if err := sec.EnsureAuthenticated(c); err != nil {
2024-06-22 10:29:41 +00:00
return err
}
2024-11-02 05:23:27 +00:00
user := c.Locals("user").(authm.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
}
2024-11-02 05:23:27 +00:00
var account authm.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-11-02 05:23:27 +00:00
if err := sec.EnsureAuthenticated(c); err != nil {
2024-06-22 10:29:41 +00:00
return err
}
2024-11-02 05:23:27 +00:00
user := c.Locals("user").(authm.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
}
2024-11-02 05:23:27 +00:00
var account authm.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-11-02 05:23:27 +00:00
if err := sec.EnsureAuthenticated(c); err != nil {
2024-06-22 10:29:41 +00:00
return err
}
2024-11-02 05:23:27 +00:00
user := c.Locals("user").(authm.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())
}
2024-11-02 05:23:27 +00:00
membership.Name = user.Name
2024-04-06 08:08:33 +00:00
membership.Notify = data.NotifyLevel
if len(data.Nick) > 0 {
2024-11-02 05:23:27 +00:00
membership.Nick = data.Nick
} else {
2024-11-02 05:23:27 +00:00
membership.Nick = user.Nick
}
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-11-02 05:23:27 +00:00
if err := sec.EnsureAuthenticated(c); err != nil {
2024-06-22 10:29:41 +00:00
return err
}
2024-11-02 05:23:27 +00:00
user := c.Locals("user").(authm.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")
2024-09-17 04:22:53 +00:00
} else if channel.RealmID == nil && !channel.IsCommunity {
return fiber.NewError(fiber.StatusBadRequest, "you were impossible to join a channel without related realm and non-community")
2024-05-04 16:39:59 +00:00
}
2024-09-17 04:22:53 +00:00
if channel.RealmID != nil {
2024-09-17 08:52:42 +00:00
if realm, err := services.GetRealmWithExtID(channel.Realm.ID); err != nil {
2024-09-17 04:22:53 +00:00
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("invalid channel, related realm was not found: %v", err))
2024-09-17 08:52:42 +00:00
} else if _, err := services.GetRealmMember(realm.ID, user.ID); err != nil {
2024-09-17 04:22:53 +00:00
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you are not a part of the realm: %v", err))
}
2024-05-04 16:39:59 +00:00
}
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-11-02 05:23:27 +00:00
if err := sec.EnsureAuthenticated(c); err != nil {
2024-06-22 10:29:41 +00:00
return err
}
2024-11-02 05:23:27 +00:00
user := c.Locals("user").(authm.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)
}
}