2024-06-22 10:05:41 +00:00
|
|
|
package api
|
2024-03-26 15:05:13 +00:00
|
|
|
|
|
|
|
import (
|
2024-05-04 17:04:14 +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"
|
2024-09-11 15:58:02 +00:00
|
|
|
|
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
2024-11-02 05:23:27 +00:00
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/internal/http/exts"
|
2024-05-26 15:01:20 +00:00
|
|
|
|
2024-06-22 10:05:41 +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"
|
2024-09-17 04:22:53 +00:00
|
|
|
"github.com/samber/lo"
|
2024-03-26 15:05:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getChannel(c *fiber.Ctx) error {
|
2024-03-30 10:21:17 +00:00
|
|
|
alias := c.Params("channel")
|
2024-03-26 15:05:13 +00:00
|
|
|
|
2024-05-04 17:04:14 +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-25 13:00:45 +00:00
|
|
|
if err != nil {
|
2024-03-26 15:05:13 +00:00
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(channel)
|
|
|
|
}
|
|
|
|
|
2024-06-01 02:43:21 +00:00
|
|
|
func getChannelIdentity(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-06 13:54:01 +00:00
|
|
|
alias := c.Params("channel")
|
|
|
|
|
|
|
|
var err error
|
2024-06-01 02:43:21 +00:00
|
|
|
var member models.ChannelMember
|
2024-10-09 15:56:24 +00:00
|
|
|
|
2024-05-06 13:54:01 +00:00
|
|
|
if val, ok := c.Locals("realm").(models.Realm); ok {
|
2024-10-09 15:56:24 +00:00
|
|
|
_, member, err = services.GetChannelIdentity(alias, user.ID, val)
|
2024-05-06 13:54:01 +00:00
|
|
|
} else {
|
2024-10-09 15:56:24 +00:00
|
|
|
_, member, err = services.GetChannelIdentity(alias, user.ID)
|
2024-05-06 13:54:01 +00:00
|
|
|
}
|
2024-10-09 15:56:24 +00:00
|
|
|
|
2024-05-06 13:54:01 +00:00
|
|
|
if err != nil {
|
2024-06-01 02:43:21 +00:00
|
|
|
return c.SendStatus(fiber.StatusForbidden)
|
2024-05-06 13:54:01 +00:00
|
|
|
}
|
|
|
|
|
2024-06-01 02:43:21 +00:00
|
|
|
return c.JSON(member)
|
2024-05-06 13:54:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 15:05:13 +00:00
|
|
|
func listChannel(c *fiber.Ctx) error {
|
2024-11-02 05:23:27 +00:00
|
|
|
var user *authm.Account
|
|
|
|
if err := sec.EnsureAuthenticated(c); err == nil {
|
|
|
|
user = lo.ToPtr(c.Locals("user").(authm.Account))
|
2024-09-17 04:22:53 +00:00
|
|
|
}
|
|
|
|
|
2024-05-04 17:04:14 +00:00
|
|
|
var err error
|
|
|
|
var channels []models.Channel
|
|
|
|
if val, ok := c.Locals("realm").(models.Realm); ok {
|
2024-09-17 04:22:53 +00:00
|
|
|
channels, err = services.ListChannel(user, val.ID)
|
2024-05-04 17:04:14 +00:00
|
|
|
} else {
|
2024-09-17 04:22:53 +00:00
|
|
|
channels, err = services.ListChannel(user)
|
2024-05-04 17:04:14 +00:00
|
|
|
}
|
2024-03-26 15:05:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(channels)
|
|
|
|
}
|
|
|
|
|
|
|
|
func listOwnedChannel(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-03-26 15:05:13 +00:00
|
|
|
|
2024-05-05 14:56:37 +00:00
|
|
|
var err error
|
|
|
|
var channels []models.Channel
|
|
|
|
if val, ok := c.Locals("realm").(models.Realm); ok {
|
|
|
|
channels, err = services.ListChannelWithUser(user, val.ID)
|
|
|
|
} else {
|
|
|
|
channels, err = services.ListChannelWithUser(user)
|
|
|
|
}
|
2024-03-26 15:05:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(channels)
|
|
|
|
}
|
|
|
|
|
|
|
|
func listAvailableChannel(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-03-26 15:05:13 +00:00
|
|
|
|
2024-10-05 09:46:39 +00:00
|
|
|
tx := database.C
|
|
|
|
isDirect := c.QueryBool("direct", false)
|
|
|
|
if isDirect {
|
|
|
|
tx = tx.Where("type = ?", models.ChannelTypeDirect)
|
|
|
|
} else {
|
|
|
|
tx = tx.Where("type = ?", models.ChannelTypeCommon)
|
|
|
|
}
|
|
|
|
|
2024-05-05 14:56:37 +00:00
|
|
|
var err error
|
|
|
|
var channels []models.Channel
|
|
|
|
if val, ok := c.Locals("realm").(models.Realm); ok {
|
2024-10-05 09:46:39 +00:00
|
|
|
channels, err = services.ListAvailableChannel(tx, user, val.ID)
|
2024-05-05 14:56:37 +00:00
|
|
|
} else {
|
2024-10-05 09:46:39 +00:00
|
|
|
channels, err = services.ListAvailableChannel(tx, user)
|
2024-05-05 14:56:37 +00:00
|
|
|
}
|
2024-03-26 15:05:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(channels)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createChannel(c *fiber.Ctx) error {
|
2024-11-02 05:23:27 +00:00
|
|
|
if err := sec.EnsureGrantedPerm(c, "CreateChannels", true); 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-03-26 15:05:13 +00:00
|
|
|
|
|
|
|
var data struct {
|
2024-04-26 16:04:01 +00:00
|
|
|
Alias string `json:"alias" validate:"required,lowercase,min=4,max=32"`
|
2024-03-26 15:05:13 +00:00
|
|
|
Name string `json:"name" validate:"required"`
|
|
|
|
Description string `json:"description"`
|
2024-09-17 03:45:44 +00:00
|
|
|
IsPublic bool `json:"is_public"`
|
|
|
|
IsCommunity bool `json:"is_community"`
|
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
|
2024-04-26 16:04:01 +00:00
|
|
|
} else if err = services.GetChannelAliasAvailability(data.Alias); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
2024-03-26 15:05:13 +00:00
|
|
|
}
|
|
|
|
|
2024-05-04 17:04:14 +00:00
|
|
|
var realm *models.Realm
|
|
|
|
if val, ok := c.Locals("realm").(models.Realm); ok {
|
2024-09-17 08:52:42 +00:00
|
|
|
if info, err := services.GetRealmMember(val.ID, user.ID); err != nil {
|
2024-05-26 15:01:20 +00:00
|
|
|
return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can create channel related to it")
|
2024-05-04 17:04:14 +00:00
|
|
|
} else if info.GetPowerLevel() < 50 {
|
2024-05-26 15:01:20 +00:00
|
|
|
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can create channel related to it")
|
2024-05-04 17:04:14 +00:00
|
|
|
} else {
|
|
|
|
realm = &val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-26 15:01:20 +00:00
|
|
|
channel := models.Channel{
|
|
|
|
Alias: data.Alias,
|
|
|
|
Name: data.Name,
|
|
|
|
Description: data.Description,
|
|
|
|
AccountID: user.ID,
|
|
|
|
Type: models.ChannelTypeCommon,
|
2024-09-17 03:45:44 +00:00
|
|
|
IsPublic: data.IsPublic,
|
|
|
|
IsCommunity: data.IsCommunity,
|
2024-05-26 15:01:20 +00:00
|
|
|
Members: []models.ChannelMember{
|
|
|
|
{AccountID: user.ID, PowerLevel: 100},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-05-04 17:04:14 +00:00
|
|
|
if realm != nil {
|
2024-05-26 15:01:20 +00:00
|
|
|
channel.RealmID = &realm.ID
|
2024-05-04 17:04:14 +00:00
|
|
|
}
|
|
|
|
|
2024-05-26 15:01:20 +00:00
|
|
|
channel, err := services.NewChannel(channel)
|
2024-03-26 15:05:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func editChannel(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-03-26 15:05:13 +00:00
|
|
|
id, _ := c.ParamsInt("channelId", 0)
|
|
|
|
|
|
|
|
var data struct {
|
2024-03-30 09:10:36 +00:00
|
|
|
Alias string `json:"alias" validate:"required,min=4,max=32"`
|
2024-03-26 15:05:13 +00:00
|
|
|
Name string `json:"name" validate:"required"`
|
|
|
|
Description string `json:"description"`
|
2024-09-17 03:45:44 +00:00
|
|
|
IsPublic bool `json:"is_public"`
|
|
|
|
IsCommunity bool `json:"is_community"`
|
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
|
|
|
|
}
|
|
|
|
|
2024-09-11 15:58:02 +00:00
|
|
|
tx := database.C.Where(&models.Channel{BaseModel: hyper.BaseModel{ID: uint(id)}})
|
2024-05-04 17:04:14 +00:00
|
|
|
|
|
|
|
if val, ok := c.Locals("realm").(models.Realm); ok {
|
2024-09-17 08:52:42 +00:00
|
|
|
if info, err := services.GetRealmMember(val.ID, user.ID); err != nil {
|
2024-05-26 15:01:20 +00:00
|
|
|
return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can edit channel related to it")
|
2024-05-04 17:04:14 +00:00
|
|
|
} else if info.GetPowerLevel() < 50 {
|
2024-05-26 15:01:20 +00:00
|
|
|
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can edit channel related to it")
|
2024-05-04 17:04:14 +00:00
|
|
|
} else {
|
|
|
|
tx = tx.Where("realm_id = ?", val.ID)
|
|
|
|
}
|
|
|
|
} else {
|
2024-05-26 15:01:20 +00:00
|
|
|
tx = tx.Where("realm_id IS NULL")
|
2024-05-04 17:04:14 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 15:05:13 +00:00
|
|
|
var channel models.Channel
|
2024-05-04 17:04:14 +00:00
|
|
|
if err := tx.First(&channel).Error; err != nil {
|
2024-03-26 15:05:13 +00:00
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-05-26 15:01:20 +00:00
|
|
|
if channel.RealmID != nil {
|
|
|
|
if member, err := services.GetChannelMember(user, channel.ID); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusForbidden, "you must be a part of this channel to edit it")
|
|
|
|
} else if member.PowerLevel < 100 {
|
|
|
|
return fiber.NewError(fiber.StatusForbidden, "you must be channel admin to edit it")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-17 03:45:44 +00:00
|
|
|
channel, err := services.EditChannel(channel, data.Alias, data.Name, data.Description, data.IsPublic, data.IsCommunity)
|
2024-03-26 15:05:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteChannel(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-03-26 15:05:13 +00:00
|
|
|
id, _ := c.ParamsInt("channelId", 0)
|
|
|
|
|
2024-09-11 15:58:02 +00:00
|
|
|
tx := database.C.Where(&models.Channel{BaseModel: hyper.BaseModel{ID: uint(id)}})
|
2024-05-04 17:04:14 +00:00
|
|
|
|
|
|
|
if val, ok := c.Locals("realm").(models.Realm); ok {
|
2024-09-17 08:52:42 +00:00
|
|
|
if info, err := services.GetRealmMember(val.ID, user.ID); err != nil {
|
2024-05-04 17:04:14 +00:00
|
|
|
return fmt.Errorf("you must be a part of that realm then can delete channel related to it")
|
|
|
|
} else if info.GetPowerLevel() < 50 {
|
|
|
|
return fmt.Errorf("you must be a moderator of that realm then can delete channel related to it")
|
|
|
|
} else {
|
|
|
|
tx = tx.Where("realm_id = ?", val.ID)
|
|
|
|
}
|
|
|
|
} else {
|
2024-05-28 15:44:45 +00:00
|
|
|
tx = tx.Where("(account_id = ? OR type = ?) AND realm_id IS NULL", user.ID, models.ChannelTypeDirect)
|
2024-05-04 17:04:14 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 15:05:13 +00:00
|
|
|
var channel models.Channel
|
2024-05-04 17:04:14 +00:00
|
|
|
if err := tx.First(&channel).Error; err != nil {
|
2024-03-26 15:05:13 +00:00
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-05-28 15:44:45 +00:00
|
|
|
if channel.Type == models.ChannelTypeDirect {
|
|
|
|
if member, err := services.GetChannelMember(user, channel.ID); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusForbidden, "you must related to this direct message if you want delete it")
|
|
|
|
} else if member.PowerLevel < 100 {
|
|
|
|
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of this direct message if you want delete it")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-26 15:05:13 +00:00
|
|
|
if err := services.DeleteChannel(channel); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|