2024-05-26 15:01:20 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-05-28 12:24:40 +00:00
|
|
|
"fmt"
|
|
|
|
|
2024-05-26 15:01:20 +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 createDirectChannel(c *fiber.Ctx) error {
|
|
|
|
user := c.Locals("principal").(models.Account)
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Alias string `json:"alias" validate:"required,lowercase,min=4,max=32"`
|
|
|
|
Name string `json:"name" validate:"required"`
|
|
|
|
Description string `json:"description"`
|
2024-05-28 12:24:40 +00:00
|
|
|
RelatedUser uint `json:"related_user"`
|
2024-05-26 15:01:20 +00:00
|
|
|
IsEncrypted bool `json:"is_encrypted"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
|
|
|
} else if err = services.GetChannelAliasAvailability(data.Alias); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var realm *models.Realm
|
|
|
|
if val, ok := c.Locals("realm").(models.Realm); ok {
|
|
|
|
if info, err := services.GetRealmMember(val.ExternalID, user.ExternalID); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can create channel related to it")
|
|
|
|
} else if info.GetPowerLevel() < 50 {
|
|
|
|
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can create channel related to it")
|
|
|
|
} else {
|
|
|
|
realm = &val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-28 12:24:40 +00:00
|
|
|
var relatedUser models.Account
|
|
|
|
if err := database.C.Where("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
|
|
|
}
|
|
|
|
|
|
|
|
channel := models.Channel{
|
|
|
|
Alias: data.Alias,
|
|
|
|
Name: data.Name,
|
|
|
|
Description: data.Description,
|
|
|
|
IsEncrypted: data.IsEncrypted,
|
|
|
|
AccountID: user.ID,
|
|
|
|
Type: models.ChannelTypeDirect,
|
2024-05-28 12:24:40 +00:00
|
|
|
Members: []models.ChannelMember{
|
2024-05-26 15:01:20 +00:00
|
|
|
{AccountID: user.ID, PowerLevel: 100},
|
2024-05-28 12:24:40 +00:00
|
|
|
{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)
|
|
|
|
}
|