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

159 lines
4.3 KiB
Go
Raw Normal View History

package api
import (
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-08-07 10:34:44 +00:00
"strings"
2024-11-02 05:23:27 +00:00
"git.solsynth.dev/hydrogen/messaging/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/services"
"github.com/gofiber/fiber/v2"
jsoniter "github.com/json-iterator/go"
)
func newMessageEvent(c *fiber.Ctx) error {
2024-11-02 05:23:27 +00:00
if err := sec.EnsureAuthenticated(c); err != nil {
return err
}
2024-11-02 05:23:27 +00:00
user := c.Locals("user").(authm.Account)
alias := c.Params("channel")
var data struct {
Uuid string `json:"uuid" validate:"required"`
Type string `json:"type" validate:"required"`
Body models.EventMessageBody `json:"body"`
}
if err := exts.BindAndValidate(c, &data); err != nil {
return err
} else if len(data.Uuid) < 36 {
return fiber.NewError(fiber.StatusBadRequest, "message uuid was not valid")
}
2024-08-07 10:39:38 +00:00
data.Body.Text = strings.TrimSpace(data.Body.Text)
2024-06-27 14:54:35 +00:00
if len(data.Body.Text) == 0 && len(data.Body.Attachments) == 0 {
2024-08-07 10:39:38 +00:00
return fiber.NewError(fiber.StatusBadRequest, "empty message was not allowed")
2024-06-27 14:54:35 +00:00
}
var err error
var channel models.Channel
var member models.ChannelMember
2024-10-09 15:56:24 +00:00
if val, ok := c.Locals("realm").(models.Realm); ok {
2024-10-09 15:56:24 +00:00
channel, member, err = services.GetChannelIdentity(alias, user.ID, val)
} else {
2024-10-09 15:56:24 +00:00
channel, member, err = services.GetChannelIdentity(alias, user.ID)
}
2024-10-09 15:56:24 +00:00
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
} else if member.PowerLevel < 0 {
return fiber.NewError(fiber.StatusForbidden, "unable to send message, access denied")
}
var parsed map[string]any
raw, _ := jsoniter.Marshal(data.Body)
_ = jsoniter.Unmarshal(raw, &parsed)
event := models.Event{
Uuid: data.Uuid,
Body: parsed,
Type: data.Type,
Sender: member,
Channel: channel,
ChannelID: channel.ID,
SenderID: member.ID,
}
if event, err = services.NewEvent(event); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(event)
}
func editMessageEvent(c *fiber.Ctx) error {
2024-11-02 05:23:27 +00:00
if err := sec.EnsureAuthenticated(c); err != nil {
return err
}
2024-11-02 05:23:27 +00:00
user := c.Locals("user").(authm.Account)
alias := c.Params("channel")
messageId, _ := c.ParamsInt("messageId", 0)
var data struct {
Uuid string `json:"uuid" validate:"required"`
Type string `json:"type" validate:"required"`
Body models.EventMessageBody `json:"body"`
}
if err := exts.BindAndValidate(c, &data); err != nil {
return err
}
2024-06-27 14:54:35 +00:00
if len(data.Body.Text) == 0 && len(data.Body.Attachments) == 0 {
return fiber.NewError(fiber.StatusBadRequest, "you cannot send an empty message")
}
var err error
var channel models.Channel
var member models.ChannelMember
2024-10-09 15:56:24 +00:00
if val, ok := c.Locals("realm").(models.Realm); ok {
2024-10-09 15:56:24 +00:00
channel, member, err = services.GetChannelIdentity(alias, user.ID, val)
} else {
2024-10-09 15:56:24 +00:00
channel, member, err = services.GetChannelIdentity(alias, user.ID)
}
2024-10-09 15:56:24 +00:00
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
var event models.Event
if event, err = services.GetEventWithSender(channel, member, uint(messageId)); err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
event, err = services.EditMessage(event, data.Body)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(event)
}
func deleteMessageEvent(c *fiber.Ctx) error {
2024-11-02 05:23:27 +00:00
if err := sec.EnsureAuthenticated(c); err != nil {
return err
}
2024-11-02 05:23:27 +00:00
user := c.Locals("user").(authm.Account)
alias := c.Params("channel")
messageId, _ := c.ParamsInt("messageId", 0)
var err error
var channel models.Channel
var member models.ChannelMember
2024-10-09 15:56:24 +00:00
if val, ok := c.Locals("realm").(models.Realm); ok {
2024-10-09 15:56:24 +00:00
channel, member, err = services.GetChannelIdentity(alias, user.ID, val)
} else {
2024-10-09 15:56:24 +00:00
channel, member, err = services.GetChannelIdentity(alias, user.ID)
}
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
var event models.Event
if event, err = services.GetEventWithSender(channel, member, uint(messageId)); err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
event, err = services.DeleteMessage(event)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(event)
}