2024-03-26 15:05:13 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-05-04 16:39:59 +00:00
|
|
|
"fmt"
|
2024-05-26 15:01:20 +00:00
|
|
|
|
2024-03-26 15:05:13 +00:00
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/database"
|
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/models"
|
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/services"
|
|
|
|
"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
|
|
|
|
if val, ok := c.Locals("realm").(models.Realm); ok {
|
|
|
|
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-05-04 16:39:59 +00:00
|
|
|
func addChannelMember(c *fiber.Ctx) error {
|
2024-03-26 15:05:13 +00:00
|
|
|
user := c.Locals("principal").(models.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
|
|
|
}
|
|
|
|
|
|
|
|
if err := BindAndValidate(c, &data); err != nil {
|
|
|
|
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-26 15:01:20 +00:00
|
|
|
} else if member, err := services.GetChannelMember(user, channel.ID); err != nil {
|
|
|
|
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(&models.Account{
|
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-03-26 15:05:13 +00:00
|
|
|
user := c.Locals("principal").(models.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
|
|
|
}
|
|
|
|
|
|
|
|
if err := BindAndValidate(c, &data); err != nil {
|
|
|
|
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-26 15:01:20 +00:00
|
|
|
} else if member, err := services.GetChannelMember(user, channel.ID); err != nil {
|
|
|
|
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(&models.Account{
|
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-04-06 08:08:33 +00:00
|
|
|
func editChannelMembership(c *fiber.Ctx) error {
|
|
|
|
user := c.Locals("principal").(models.Account)
|
2024-04-26 12:18:49 +00:00
|
|
|
alias := c.Params("channel")
|
2024-04-06 08:08:33 +00:00
|
|
|
|
|
|
|
var data struct {
|
|
|
|
NotifyLevel int8 `json:"notify_level"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-26 12:18:49 +00:00
|
|
|
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{
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
membership.Notify = data.NotifyLevel
|
|
|
|
|
|
|
|
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 {
|
|
|
|
user := c.Locals("principal").(models.Account)
|
|
|
|
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")
|
|
|
|
} else if channel.RealmID == nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "you was impossible to join a channel without related realm")
|
|
|
|
}
|
|
|
|
|
2024-05-06 15:28:15 +00:00
|
|
|
if realm, err := services.GetRealm(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))
|
2024-05-06 15:28:15 +00:00
|
|
|
} else if _, err := services.GetRealmMember(realm.ExternalID, user.ExternalID); 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 {
|
|
|
|
user := c.Locals("principal").(models.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)
|
|
|
|
}
|
|
|
|
}
|