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

85 lines
2.6 KiB
Go
Raw Permalink Normal View History

package api
2024-05-26 15:01:20 +00:00
import (
"fmt"
"git.solsynth.dev/hypernet/messaging/pkg/internal/gap"
2024-11-02 05:23:27 +00:00
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"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:58:02 +00:00
2024-11-02 05:24:37 +00:00
"git.solsynth.dev/hypernet/messaging/pkg/internal/http/exts"
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-05-26 15:01:20 +00:00
"github.com/gofiber/fiber/v2"
)
func createDirectChannel(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-26 15:01:20 +00:00
var data struct {
Alias string `json:"alias" validate:"required,lowercase,min=4,max=32"`
Name string `json:"name" validate:"required"`
Description string `json:"description"`
RelatedUser uint `json:"related_user"`
2024-05-26 15:01:20 +00:00
IsEncrypted bool `json:"is_encrypted"`
}
if err := exts.BindAndValidate(c, &data); err != nil {
2024-05-26 15:01:20 +00:00
return err
} else if err = services.GetChannelAliasAvailability(data.Alias); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
var realm *authm.Realm
if val, ok := c.Locals("realm").(authm.Realm); ok {
if info, err := authkit.GetRealmMember(gap.Nx, 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")
} else if info.PowerLevel < 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")
} else {
realm = &val
}
}
2024-11-02 05:23:27 +00:00
var relatedUser authm.Account
2024-05-28 15:44:45 +00:00
if err := database.C.
Where("external_id = ?", data.RelatedUser).
First(&relatedUser).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to find related user: %v", err))
2024-05-26 15:01:20 +00:00
}
if ch, err := services.GetDirectChannelByUser(user, relatedUser); err == nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you already have a direct with that user #%d", ch.ID))
}
2024-05-26 15:01:20 +00:00
channel := models.Channel{
Alias: data.Alias,
Name: data.Name,
Description: data.Description,
2024-09-17 04:22:53 +00:00
IsPublic: false,
IsCommunity: false,
2024-05-26 15:01:20 +00:00
AccountID: user.ID,
Type: models.ChannelTypeDirect,
Members: []models.ChannelMember{
2024-05-26 15:01:20 +00:00
{AccountID: user.ID, PowerLevel: 100},
{AccountID: relatedUser.ID, PowerLevel: 100},
},
2024-05-26 15:01:20 +00:00
}
if realm != nil {
channel.RealmID = &realm.ID
}
channel, err := services.NewChannel(channel)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(channel)
}