2024-06-22 10:05:41 +00:00
|
|
|
package api
|
2024-03-26 15:05:13 +00:00
|
|
|
|
|
|
|
import (
|
2024-05-04 16:39:59 +00:00
|
|
|
"fmt"
|
2024-11-02 05:40:37 +00:00
|
|
|
"git.solsynth.dev/hypernet/messaging/pkg/internal/gap"
|
2024-11-02 05:23:27 +00:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
2024-11-02 05:40:37 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit"
|
2024-11-02 05:23:27 +00:00
|
|
|
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
2024-09-11 15:54:18 +00:00
|
|
|
|
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 {
|
2024-04-26 12:18:49 +00:00
|
|
|
alias := c.Params("channel")
|
2024-03-26 15:05:13 +00:00
|
|
|
|
2024-05-08 13:57:57 +00:00
|
|
|
var err error
|
|
|
|
var channel models.Channel
|
2024-11-02 05:40:37 +00:00
|
|
|
if val, ok := c.Locals("realm").(authm.Realm); ok {
|
2024-05-08 13:57:57 +00:00
|
|
|
channel, err = services.GetChannelWithAlias(alias, val.ID)
|
|
|
|
} else {
|
|
|
|
channel, err = services.GetChannelWithAlias(alias)
|
|
|
|
}
|
2024-04-26 12:18:49 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-08 13:46:06 +00:00
|
|
|
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)
|
2024-06-08 13:46:06 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
var channel models.Channel
|
2024-11-02 05:40:37 +00:00
|
|
|
if val, ok := c.Locals("realm").(authm.Realm); ok {
|
2024-06-08 13:46:06 +00:00
|
|
|
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)
|
2024-04-26 12:18:49 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-06-22 10:05:41 +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())
|
2024-05-28 12:24:40 +00:00
|
|
|
} 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
|
2024-11-02 05:40:37 +00:00
|
|
|
if err := database.C.Where("name = ?", data.Target).First(&account).Error; err != nil {
|
2024-03-26 15:05:13 +00:00
|
|
|
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)
|
2024-04-26 12:18:49 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-06-22 10:05:41 +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-04-26 12:18:49 +00:00
|
|
|
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())
|
2024-05-28 12:24:40 +00:00
|
|
|
} 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
|
2024-11-02 05:40:37 +00:00
|
|
|
if err := database.C.Where("name = ?", data.Target).First(&account).Error; err != nil {
|
2024-03-26 15:05:13 +00:00
|
|
|
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)
|
2024-04-26 12:18:49 +00:00
|
|
|
alias := c.Params("channel")
|
2024-04-06 08:08:33 +00:00
|
|
|
|
|
|
|
var data struct {
|
2024-06-08 13:46:06 +00:00
|
|
|
Nick string `json:"nick"`
|
|
|
|
NotifyLevel int8 `json:"notify_level"`
|
2024-04-06 08:08:33 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 10:05:41 +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
|
2024-11-02 05:40:37 +00:00
|
|
|
if val, ok := c.Locals("realm").(authm.Realm); ok {
|
2024-06-08 15:59:25 +00:00
|
|
|
channel, err = services.GetChannelWithAlias(alias, val.ID)
|
|
|
|
} else {
|
|
|
|
channel, err = services.GetChannelWithAlias(alias)
|
|
|
|
}
|
2024-04-26 12:18:49 +00:00
|
|
|
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{
|
2024-04-26 12:18:49 +00:00
|
|
|
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
|
2024-06-08 13:46:06 +00:00
|
|
|
if len(data.Nick) > 0 {
|
2024-11-02 05:23:27 +00:00
|
|
|
membership.Nick = data.Nick
|
2024-06-08 13:46:06 +00:00
|
|
|
} else {
|
2024-11-02 05:23:27 +00:00
|
|
|
membership.Nick = user.Nick
|
2024-06-08 13:46:06 +00:00
|
|
|
}
|
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,
|
2024-05-06 15:28:15 +00:00
|
|
|
}).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-11-02 05:40:37 +00:00
|
|
|
if _, err := authkit.GetRealmMember(gap.Nx, *channel.RealmID, 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)
|
2024-04-26 12:18:49 +00:00
|
|
|
alias := c.Params("channel")
|
2024-04-06 06:36:36 +00:00
|
|
|
|
|
|
|
var channel models.Channel
|
|
|
|
if err := database.C.Where(&models.Channel{
|
2024-04-26 12:18:49 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|